0
在vba中,有一個地址由long類型持有,該地址指向以null結尾的字符串,但我找不到獲取字符串的方法從這個地址:如何從長指定的地址獲取字符串VB
long str_address = ...
string str = ?
請你對此有所瞭解嗎?
在vba中,有一個地址由long類型持有,該地址指向以null結尾的字符串,但我找不到獲取字符串的方法從這個地址:如何從長指定的地址獲取字符串VB
long str_address = ...
string str = ?
請你對此有所瞭解嗎?
我用CopyMemory
這樣:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function lstrlenA Lib "kernel32" (ByVal lpString As Long) As Long
Private Function pvToString(ByVal lPtr As Long) As String
If lPtr <> 0 Then
pvToString = String(lstrlenA(lPtr), 0)
Call CopyMemory(ByVal pvToString, ByVal lPtr, Len(pvToString))
End If
End Function
如果由於某種原因,你結束了一個指向VBA的ANSII 0結尾的字符串,則需要將內容複製到一個字符串:
Private Declare Function SysAllocStringByteLen Lib "oleaut32.dll" (ByVal m_pBase As Long, ByVal l As Long) As String
Private Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Long) As Long
...
dim s as string
s = SysAllocStringByteLen(str_address, lstrlen(str_address))
在VBA中,'long'是一個整數類型 - 請解釋一下它是如何指向一個以null結尾的字符串的?請參閱[MSDN](http://msdn.microsoft.com/en-us/library/aa164754.aspx) – Oded 2010-10-13 08:21:46
該整數的值實際上是一個以空字符結尾的字符串的地址 – 2010-10-13 08:38:16
@Oded我正在使用IPC,並獲取通過CreateFileMapping/MapViewOfFile的地址,它返回的地址長在VBA – 2010-10-13 08:40:20