Non ho familiarità con Dephi, ma qui ci sono le due opzioni principali quando si usano stringhe con una DLL non COM e VB6.
Opzione 1. Utilizzare stringhe "ANSI".
'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 = sResult
End Function
Opzione due. Usa 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 = sResult
End Function
Vorrei anche suggerire guardando il Microsoft advice sulla scrittura DLL C di essere chiamato da VB. Originariamente rilasciato con VB5 ma ancora rilevante per VB6.
Ho fatto alcuni esperimenti prima di chiedere su SO. Ho usato una variabile di stringa globale (Ansi) e ho appena restituito PAnsiChar (MyGlobalVar) e ha funzionato. Perché ha funzionato quando in realtà devo restituire un BSTR (= due byte per carattere)? –
AFAIK quando si tratta di chiamate API, VB6 esegue in modo implicito tutte le stringhe su stringhe ANSI, anche se VB6 funziona internamente con BSTR. –
DR è corretto: se si utilizza ByVal nella dichiarazione DLL, VB6 converte implicitamente tutte le stringhe da e verso le stringhe ANSI durante la chiamata alle DLL. – MarkJ