Sample 100

よく見かけるVBサンプル集

BACK -Page-Next- NEXT

[00〜09] [10〜19] [20〜29] [30〜39] [40〜49]
[50〜59] [60〜69] [70〜79] [80〜89] [90〜99]


indexへ戻る

MENU 60〜69 

<2009/05/26>
いまだに業務用ソフトでVB6が使われてたりするので
今後も更新していきます。


60指定したプロセスからInteger型(2byte)読む API   

61指定したプロセスからLong型(4byte)読む API   

62指定したプロセスからSingle型(32bit浮動小数)を読む API   

63指定したプロセスからDouble型(64bit浮動小数)を読む API   

64指定したプロセスからString型(文字列)を読む API   

65欠番

66コード内に含まれる、特定KEYを含むリンクを抽出する New

67準備中

68準備中


共通定数

MAX_PATH = 260

指定したプロセスからInteger型(2byte)読む

使用API
Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, ByRef lpNumberOfBytesRead As Long) As Long

使用例

x =GetProcessInteger(zzz,&h006DA484)

以下を標準モジュールへ追加
Function GetProcessInteger(ByVal hProc As Long, ByVal Addr As Long) As Integer
'指定したプロセスから2byte読む
Dim bRet As Long
Dim dwRead As Long
Dim res As Integer

If hProc = 0 Then Exit Function
bRet = ReadProcessMemory(hProc, Addr, res, 4, dwRead)
GetProcessInteger = res

End Function

指定したプロセスからLong型(4byte)読む

使用API
Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, ByRef lpNumberOfBytesRead As Long) As Long

使用例

x =GetProcessLong(zzz,&h006DA484)

以下を標準モジュールへ追加
Function GetProcessLong(ByVal hProc As Long, ByVal Addr As Long) As Long
'指定したプロセスから4byte読む
Dim bRet As Long
Dim dwRead As Long
Dim res As Integer

If hProc = 0 Then Exit Function
bRet = ReadProcessMemory(hProc, Addr, res, 4, dwRead)
GetProcessLong= res

End Function

指定したプロセスからSingle型(32bit浮動小数)を読む

使用API
Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, ByRef lpNumberOfBytesRead As Long) As Long

使用例

x =GetProcessSingle(zzz,&h006DA484)

以下を標準モジュールへ追加
Function GetProcessSingle(ByVal hProc As Long, ByVal Addr As Long) As Single
'指定したプロセスから32bit浮動小数を読む
Dim bRet As Long
Dim dwRead As Long
Dim res As Single

If hProc = 0 Then Exit Function
bRet = ReadProcessMemory(hProc, Addr, res, 4, dwRead)
GetProcessSingle = res

End Function

指定したプロセスからDouble型(64bit浮動小数)を読む

使用API
Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, ByRef lpNumberOfBytesRead As Long) As Long

使用例

x =GetProcessDouble(zzz,&h006DA484)

以下を標準モジュールへ追加
Function GetProcessDouble(ByVal hProc As Long, ByVal Addr As Long) As Double
'指定したプロセスから64bit浮動小数を読む
Dim bRet As Long
Dim dwRead As Long
Dim res As Double, b(7) As Byte

If hProc = 0 Then Exit Function
bRet = ReadProcessMemory(hProc, Addr, b(0), 8, dwRead)
bRet = ReadProcessMemory(hProc, Addr, res, 8, dwRead)
GetProcessDouble = res

End Function

指定したプロセスからString型(文字列)を読む

使用API
Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, ByRef lpNumberOfBytesRead As Long) As Long

Const MAX_PATH As Long = 260

使用例

x =GetProcessString(zzz,&h006DA484)

以下を標準モジュールへ追加
Function GetProcessString(ByVal hProc As Long, ByVal Addr As Long) As String
'指定したプロセスから文字列を読む
Dim i As Integer
Dim bRet As Long, dwRead As Long
Dim Temp$, Temp2$
Dim
res As Long

If hProc = 0 Then Exit Function
Temp = String(MAX_PATH, " ")
bRet = ReadProcessMemory(hProc, Addr,
ByVal Temp, MAX_PATH - 1, dwRead)

i = InStr(Temp, vbNullChar)
If i > 1 Then GetProcessString = Left(Temp, i - 1)

End Function

用途

ゲーム系パラメータの取得なんかにも使え色々使える便利なAPI
ReadProcessMemory

敵のHPを表示する等

コード内に含まれる、特定KEYを含むリンクを抽出する

使用API
なし
含まれる機能
コード内に含まれる、特定KEYを含むリンクを抽出する。(単体用)
コード内に含まれる、特定KEYを含むリンクを抽出する。(複数用リスト化して返す)
開始文字列と終端文字列に囲まれた文字列を抽出する。


使用例

GetSearchKEYLinkAddress("http://list3.auctions.yahoo.co.jp/jp/2084240151-category-leaf.html", "NEC", False, strLst(), iIndex)

strLst()にリンクのリストが格納されます。

以下を標準モジュールへ追加
'--------------------------------------------------
'コード内に含まれる、特定KEYを含むリンクを抽出する。
'--------------------------------------------------
'strSRC :ソース
'strKEY:検索KEY 結果:抽出リンクアドレス
'bPerfectUnity:true=検索KEY完全一致/false=あいまい
'*失敗した場合には、空白が返される
'検索KEYは結果には含まれない。
'--------------------------------------------------

Public Function GetSearchKEYLinkAddress( _
ByVal strSRC As String, _
ByVal  strKEY As String, _
ByVal bPerfectUnity As Boolean, _
ByRef strLst() As String, _
ByRef intIndex As Integer, _
Optional ByVal bHitEndNotAllList As Boolean) As String

'Aタグ格納用の動的配列用
Dim SLINK() As String

Dim sTagStart As String
Dim sTagEnd As String

Dim i As Long, J As Long

Dim iStart As Long
Dim iNext As Long
Dim iEnd As Long
Dim iMax As Long

Dim n As Long
Dim intA As Long
Dim intB As Long

Dim S As String
Dim sCD As String

If 0 < InStr(1, strSRC, "<A ") Then
sTagStart = "<A "
sTagEnd = "</A>"
ElseIf 0 < InStr(1, strSRC, "<a ") Then
sTagStart = "<a "
sTagEnd = "</a>"
Else
Exit Function
End If


strSRC = Replace(strSRC, vbCrLf, " ")

intA = Len(sTagStart) 'GetStrByte(sTagStart)
intB = Len(sTagEnd) 'GetStrByte(sTagEnd)
iMax = Len(strSRC)

S = strSRC
If Not bPerfectUnity Then
S = LCase(S)
sTagStart = LCase(sTagStart)
sTagEnd = LCase(sTagEnd)
End If
iMax = Len(strSRC)

S = strSRC
strSRC = LCase(strSRC)

If bHitEndNotAllList Then
For i = 0 To 9999
intA = InStr(1, strSRC, sTagStart)
intB = InStr(1, strSRC, sTagEnd)
If intA = 0 Or intB = 0 Then Exit For
sCD =
GetSpaceCharacterAtoB(strSRC, sTagStart, sTagEnd, True)
If Len(sCD) = 0 Then Exit For

ReDim Preserve SLINK(J) As String

SLINK(J) = sCD

If 0 < InStr(1, strSRC, sTagStart & sCD & sTagEnd) Then
strSRC = Replace(strSRC, sTagStart & sCD & sTagEnd, "", , 1)
Else
'タグが不正のため、続行不可
Exit For
End If




If bPerfectUnity Then
n = InStr(1, SLINK(i), ">" & strKEY)
Else
strKEY = LCase(strKEY)
n = InStr(1, SLINK(i), strKEY)
End If

If n > 0 Then Exit For

J = J + 1
DoEvents
Next
i
strLst = SLINK()
'KEY HIT
Else
For
i = 0 To 9999
intA = InStr(1, strSRC, sTagStart)
intB = InStr(1, strSRC, sTagEnd)
If intA = 0 Or intB = 0 Then Exit For

sCD =
GetSpaceCharacterAtoB(strSRC, sTagStart, sTagEnd, True)
If Len(sCD) = 0 Then Exit For

ReDim Preserve SLINK(J) As String
SLINK(J) = sCD
If 0 < InStr(1, strSRC, sTagStart & sCD & sTagEnd) Then
strSRC = Replace(strSRC, sTagStart & sCD & sTagEnd, "", , 1)
Else
'タグが不正のため、続行不可
Exit For
End If

J = J + 1
DoEvents
Next
i
strLst = SLINK()
'KEY HIT
For i = 0 To UBound(SLINK)
If bPerfectUnity Then
J = InStr(1, SLINK(i), ">" & strKEY)
Else
strKEY = LCase(strKEY)
J = InStr(1, SLINK(i), strKEY)
End If
If
J > 0 Then Exit For
Next
End If



If i < 0 Then Exit Function
intIndex = i
'アドレス部分の抽出
GetSearchKEYLinkAddress =
GetSpaceCharacterAtoB(SLINK(intIndex), "href=""", """>", True, False)
End Function

'--------------------------------------------------
'コード内に含まれる、特定KEYを含むリンクを抽出する。
'--------------------------------------------------
'strSRC :ソース
'strKEY:検索KEY 結果:抽出リンクアドレス
'bPerfectUnity:true=検索KEY完全一致/false=あいまい
'*失敗した場合には、空白が返される
'検索KEYは結果には含まれない。
'--------------------------------------------------

Public Function GetSearchTAGKEYLIST( _
ByVal strSRC As String, _
ByVal strKEY As String, _
ByVal strTAG1 As String, _
ByVal  strTAG2 As String, _
ByVal  bPerfectUnity As Boolean, _
ByRef strLst() As String, _
ByRef  intIndex As Integer, _
Optional ByVal strTAG1D As String, _
Optional ByVal strTAG2D As String) As String

'Aタグ格納用の動的配列用
Dim SLINK() As String

Dim sTagStart As String
Dim sTagEnd As String

Dim intA As Long
Dim intB As Long

Dim i As Long, J As Long

Dim iStart As Long
Dim iNext As Long
Dim iEnd As Long
Dim iMax As Long

Dim S As String
Dim sCD As String

If 0 < InStr(1, strSRC, strTAG1) Then
sTagStart = strTAG1
sTagEnd = strTAG2
ElseIf 0 < Len(strTAG1D) Then
If 0 < InStr(1, strSRC, strTAG1D) Then
sTagStart = strTAG1D
sTagEnd = strTAG2D
Else
Exit Function
End If
Else
Exit Function
End If


strSRC = Replace(strSRC, vbCrLf, " ")

intA = GetStrByte(sTagStart)
intB = GetStrByte(sTagEnd)
iMax = Len(strSRC)

S = strSRC
If Not bPerfectUnity Then S = LCase(S)
iMax = Len(strSRC)

S = strSRC
For i = 0 To 9999
If intA = 0 Or intB = 0 Then Exit For
sCD =
GetSpaceCharacterAtoB(strSRC, sTagStart, sTagEnd, True)
If Len(sCD) = 0 Then Exit For

ReDim Preserve SLINK(J) As String
SLINK(J) = sCD

If 0 < InStr(1, strSRC, sTagStart & sCD & sTagEnd) Then
strSRC = Replace(strSRC, sTagStart & sCD & sTagEnd, "", , 1)
Else
'タグが不正のため、続行不可
Exit For
End If


'FrmMain.Text.Text = strSRC
J = J + 1
DoEvents
Next
i
strLst = SLINK() 'KEY HIT
For i = 0 To UBound(SLINK)
If bPerfectUnity Then
J = InStr(1, SLINK(i), ">" & strKEY)
Else
strKEY = LCase(strKEY)
J = InStr(1, SLINK(i), strKEY)
End If
If
J > 0 Then Exit For
Next
If i < 0 Then Exit Function
intIndex = i
'アドレス部分の抽出
GetSearchTAGKEYLIST = SLINK(intIndex)

End Function

'--------------------------------------------------
'開始文字列と終端文字列に囲まれた文字列を抽出する。
'--------------------------------------------------
'strSRC :ソース
'strA:開始文字列 strB:終端文字列 reSTR:抽出文字列
'*失敗した場合には、空白が返される
'bKeyVisible:false=開始文字列と終端文字列は結果には含まれない。
'--------------------------------------------------

Public Function GetSpaceCharacterAtoB(ByVal strSRC As String, _
ByVal strA As String, _
ByVal strB As String, _
Optional ByVal bCRLFspaceChange As Boolean = False, _
Optional ByVal bKeyVisible As Boolean = False) As String
Dim intA As Long
Dim intB As Long

Dim i As Long
Dim iStart As Long
Dim iEnd As Long
Dim iMax As Long

Dim S As String

'文字数取得
intA = Len(strA) 'GetStrByte(strA)
intB = Len(strB) 'GetStrByte(strB)
S = LCase(strSRC)
strA = LCase(strA)
strB = LCase(strB)
'改行を考慮する
If bCRLFspaceChange Then
S = Replace(S, vbCrLf, " ")
End If

iMax = Len(S)
If intA = 0 Or intB = 0 Then Exit Function

iStart = InStr(1, S, strA)
If iStart = 0 Then Exit Function
If Not bKeyVisible Then
iStart = iStart + intA
iEnd = InStr(iStart, S, strB)
If iEnd = 0 Then Exit Function
S = Mid(strSRC, iStart, iEnd - iStart)
Else
iEnd = InStr(iStart, S, strB)
If iEnd = 0 Then Exit Function
S = Mid(strSRC, iStart, iEnd - iStart + intB)
End If

i = InStr(iStart, S, vbCrLf)
If i > 0 Then
'改行を考慮する
If bCRLFspaceChange Then
S = Replace(S, vbCrLf, " ")
End If
End If

GetSpaceCharacterAtoB = S
End Function

’----------以下共通関数----------------
Public Function
GetStrByte(ByRef strA As String)
GetStrByte = LenB(StrConv(strA, vbFromUnicode))
End Function
Public Function ReplaceLink_TwoSpace(ByVal S As String) As String

Dim tmpS As String

Dim intStart As Long
Dim intEnd As Long
Dim intMax As Long

intMax = Len(S)
intStart = InStr(1, S, "<A")
intEnd = InStr(1, S, "</A>")
tmpS = S
If Len(S) > (Len("<A") + intStart) Then
tmpS = Mid(S, intStart + Len("<A"), intMax - Len("<A") - Len("</A>"))
tmpS = Replace(tmpS, " ", " ")
End If
ReplaceLink_TwoSpace = tmpS

End Function
Public Function
ReplaseUtoL(ByRef strSRC As String, ByVal S As String) As String
strSRC = Replace(strSRC, UCase(S), LCase(S))
End Function

 

 

 

 

TOP