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;
我有什么回到這里?誰(shuí)將會(huì)釋放returnd PChar類型字符串? 編輯:我問了Delphi 2005(PChar
=PAnsiChar
)GetWindowText
函數(shù),例如。函數(shù)不返回指針 CodeGo.net,除非他們是指針,調(diào)用者已經(jīng)提供的東西。相反,調(diào)用者提供了自己的一切,不管它給該函數(shù)。你幾乎從來(lái)沒有看到一個(gè)輸出緩沖區(qū)不是由另一個(gè)告訴緩沖區(qū)的大小。 進(jìn)一步可以使該技術(shù)是讓函數(shù)來(lái)告訴有多大的緩沖區(qū)需要被調(diào)用者。當(dāng)輸入指針為空指針,則該函數(shù)可以返回多少字節(jié)調(diào)用者需要提供。調(diào)用者將調(diào)用該函數(shù)兩次。 你并不需要從頭推導(dǎo)出你的API。使用已經(jīng)加工的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
第二個(gè)選項(xiàng)。使用的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調(diào)用了微軟的建議。最初發(fā)布與VB5,但仍適用于VB6的。 param = "aaaaaaaaaaaaaaaaaaaa" ' reserve 20 characters call myproc(byval param)
在調(diào)用的額外BYVAL會(huì)做一個(gè)VB字符串轉(zhuǎn)換為PChar類型和背部的mojo。 (我希望這是正確的,它已經(jīng)一段時(shí)間,因?yàn)槲沂潜黄鹊腣B。)聯(lián)系客服