【问题标题】:in vb6, how do I retrieve a char* parameter from a C dll?在 vb6 中,如何从 C dll 中检索 char* 参数?
【发布时间】:2011-11-12 18:14:25
【问题描述】:

我正在从我的 VB6 应用程序中调用 C dll。该dll具有如下函数调用签名。

void WINAPI geterrstr(char* foo);

其中 foo 是一个必须返回的字符串。

在我的 VB6 应用程序中,我尝试使用以下语法调用我的 dll,但它返回一个空字符串。

Declare Sub geterrstr Lib "technopnp.dll" (ByRef lpbuffer As String)

有什么想法吗?

【问题讨论】:

    标签: string dll vb6 function-calls


    【解决方案1】:

    你应该可以;

    Declare Sub geterrstr Lib "technopnp.dll" (ByVal lpbuffer As String)
    ...
    dim buff as string
    buff=string$(n, vbnullchar)
    geterrstr buff
    
    //read upto 1st vbnullchar
    buff = left$(buff, instr(1, buff, vbnullchar) - 1)
    if (buff="") then
      //no data
    else
      msgbox buff
    end if
    

    n需要有合适的缓冲区大小,太短会崩溃。

    【讨论】:

    • 此声明暗示 ANSI 转换。如果数据是 Unicode,则使用 ByVal lpbuffer As Long 并改为传递 StrPtr(buff)。
    • 太好了,看来字符串传递正确。不幸的是,原来的 C dll 中有一个空终止符,并且后面有几个方形字符。我怎样才能摆脱它们?
    • 它们是在 VB 缓冲区字符串中分配且未被 dll 覆盖的空字符,更新了上面的示例以及如何剥离它们。
    • 非常感谢。这真的很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多