出处:http://support.microsoft.com/kb/78304/zh-cn
因为 Microsoft Visual Basic 不支持指针数据类型,您不能直接从 Windows API 的 DLL 函数返回的值作为接收一个指针 (如一个 LPSTR)。
您可以解决这通过接收返回值为长整型数据类型。 然后使用 lstrcpy Windows API 函数将返回的字符串复制到一个 Visual Basic 字符串。
您可以解决这通过接收返回值为长整型数据类型。 然后使用 lstrcpy Windows API 函数将返回的字符串复制到一个 Visual Basic 字符串。
更多信息
此信息会包含与帮助文件为 Visual Basic 版本 1.0、 Microsoft Visual Basic 版本 2.0 和 Microsoft Visu...
<script type=text/javascript> loadTOCNode(1, 'moreinformation'); </script>
此信息会包含与帮助文件为 Visual Basic 版本 1.0、 Microsoft Visual Basic 版本 2.0 和 Microsoft Visual Basic 版本 3.0 提供与 Microsoft Professional Toolkit。
一个 LPSTR Windows API 数据类型是实际一个远指针为 NULL 结尾的字符串的字符。 因为 LPSTR 是一个远指针,它可以接收为一个四个字节的数据类型,比如一个 Visual Basic 长整型。 使用 Visual Basic ByVal 关键字,可以传递一个 Visual Basic 长整型值返回到 Windows API lstrcpy 例程将在该地址字符复制到一个 Visual Basic 字符串变量中存储地址。
because lstrcpy expects to be long enough to hold source string target string,should pad passed to lstrcpy to have enough large to before lstrcpy to passing hold source string size any Visual Basic string。 未能分配足够的空间在 Visual Basic 字符串中可能会导致一个 Unrecoverable 应用程序错误 (UAE) 或一般性保护 (GP) 错误调用 lstrcpy 时。
following is that demonstrates how to use lstrcpy to retrieve from Windows API GetDOSEnvironment routine returned LPSTR pointer example program。
注意: 在 Windows API GetDOSEnvironment 例程的功能已通过内置到 Visual Basic Environ 函数可用。 因此,因此程序并仅用于演示如何使用 lstrcpy。
一个 LPSTR Windows API 数据类型是实际一个远指针为 NULL 结尾的字符串的字符。 因为 LPSTR 是一个远指针,它可以接收为一个四个字节的数据类型,比如一个 Visual Basic 长整型。 使用 Visual Basic ByVal 关键字,可以传递一个 Visual Basic 长整型值返回到 Windows API lstrcpy 例程将在该地址字符复制到一个 Visual Basic 字符串变量中存储地址。
because lstrcpy expects to be long enough to hold source string target string,should pad passed to lstrcpy to have enough large to before lstrcpy to passing hold source string size any Visual Basic string。 未能分配足够的空间在 Visual Basic 字符串中可能会导致一个 Unrecoverable 应用程序错误 (UAE) 或一般性保护 (GP) 错误调用 lstrcpy 时。
following is that demonstrates how to use lstrcpy to retrieve from Windows API GetDOSEnvironment routine returned LPSTR pointer example program。
注意: 在 Windows API GetDOSEnvironment 例程的功能已通过内置到 Visual Basic Environ 函数可用。 因此,因此程序并仅用于演示如何使用 lstrcpy。
'*** General declarations ***
Declare Function GetDosEnvironment Lib "Kernel" () As Long
' Enter the following Declare statement as one, single line:
Declare Function lstrcpy Lib "Kernel" (ByVal lpString1 As Any,
ByVal lpString2 As Any) As Long
'*** Form Click event code ***
Sub Form_Click()
Dim lpStrAddress As Long, DOSEnv$
' Allocate space to copy LPSTR into
DOSEnv$ = Space$(4096)
' Get address of returned LPSTR into a long integer
lpStrAddress = GetDOSEnvironment()
' Copy LPSTR into a Visual Basic string
lpStrAddress = lstrcpy(DOSEnv$, lpStrAddress)
' Parse first entry in environment string and print
DOSEnv$ = Trim$(DOSEnv$)
DOSEnv$ = Left$(DOSEnv$, Len(DOSEnv$) - 1)
Form1.Print DOSEnv$
End Sub