Declare Function MyProc Lib "mylib.dll" (ByVal Param As String) As String
而这在Delphi中存根mylib.dll
:function MyProc(AParam: PChar): PChar; stdcall;var ReturnValue: string;begin ReturnValue := GetReturnValue(AParam); Result := ???;end;
我有什么回到这里?谁将会释放returnd PChar类型字符串? 编辑:我问了Delphi 2005(PChar
=PAnsiChar
)GetWindowText
函数,例如。函数不返回指针 CodeGo.net,除非他们是指针,调用者已经提供的东西。相反,调用者提供了自己的一切,不管它给该函数。你几乎从来没有看到一个输出缓冲区不是由另一个告诉缓冲区的大小。 进一步可以使该技术是让函数来告诉有多大的缓冲区需要被调用者。当输入指针为空指针,则该函数可以返回多少字节调用者需要提供。调用者将调用该函数两次。 你并不需要从头推导出你的API。使用已经加工的API作为示例如何暴露自己。unit DLL;interfaceuses SysUtils;function Execute(const Params: PChar): PChar; stdcall;procedure FreePointer(const P: PChar); stdcall;exports Execute;exports FreePointer;implementationfunction Execute(const Params: PChar): PChar; stdcall;var Size: Cardinal;begin Size := Calculate the size; GetMem(Result, Size); ...do something to fill the bufferend;procedure FreePointer(const P: PChar); stdcall;begin FreeMem(P);end;end.
'DLL routine expecting to be passed pointers to ANSI strings ''VB6 will allocate and deallocate the strings ''Its vital that VB6 allocates sufficient space for the return string 'Declare Sub MyProc Lib "mylib.dll" (ByVal Param As String, _ ByVal OutVal As String) Function DoMyProc(ByVal Param As String) As String Dim sResult As String sResult = Space$(255) ' create 255 bytes of space for the return string ' Call MyProc(Param, sResult) DoMyProc = sResultEnd Function
第二个选项。使用的BSTR。'DLL routine expecting to be passed two BSTRs. It will modify the second one. ''VB6 "owns" both BSTRs and will deallocate them when it has finished with them. 'Declare Sub MyProc(ByVal lpParam As Long, ByVal lpOutVal As Long)Function DoMyProc(ByVal Param As String) As String Dim sResult As String Call MyProc(StrPtr(Param), StrPtr(sResult)) DoMyProc = sResultEnd Function
我还建议寻找在编写C的DLL被从VB调用了微软的建议。最初发布与VB5,但仍适用于VB6的。 param = "aaaaaaaaaaaaaaaaaaaa" ' reserve 20 characters call myproc(byval param)
在调用的额外BYVAL会做一个VB字符串转换为PChar类型和背部的mojo。 (我希望这是正确的,它已经一段时间,因为我是被迫的VB。)联系客服