[转]从其他 API 如何调用 lstrcpy 接收 LPSTR 返回

 出处:http://support.microsoft.com/kb/78304/zh-cn

 

因为 Microsoft Visual Basic 不支持指针数据类型,您不能直接从 Windows API 的 DLL 函数返回的值作为接收一个指针 (如一个 LPSTR)。

您可以解决这通过接收返回值为长整型数据类型。 然后使用 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。
'*** 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
				

这篇文章中的信息适用于:
  • Microsoft Visual Basic 2.0 Standard Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 2.0 Professional Edition
  • Microsoft Visual Basic 1.0 Standard Edition
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值