"Calling a PowerBASIC dll from C" as an example of passing
dynamic strings to a pb dll. The only difference with what
Michael wants to do is that he wants to return a string so I
didn't show that part.
It's very easy and you don't need to mess around with BSTR
for returning the string. Here's an example (notice) that
all I did was change the prototype and return types from the
previous example:
Code:
// C CODE #include <windows.h> #include <stdio.h> #include <oleauto.h> /* just change the prototype to char* to receive strings back from PB functions */ typedef char* __stdcall LPFNCALLDLL( int DBNumber, BSTR DBName, int &prompt ); int __cdecl main( int argc, char *argv[], char *envp[] ) { HMODULE hLib; LPFNCALLDLL* lpCallDll; BSTR bString; char *chDSN; int prompt; char *chReturn; hLib = LoadLibrary( "mydll.dll" ); if ( hLib == NULL ) { printf( "Could not load mydll.dll" ); return FALSE; } lpCallDll = (LPFNCALLDLL*)GetProcAddress( hLib, "MyFunction" ); if ( lpCallDll == NULL ) { FreeLibrary( hLib ); return FALSE; } chDSN = " DRIVER=SQL Server;server=DevTestServer;database=DB1;uid=dbuser;pwd=pwrd "; /* Use OLE strings */ bString = SysAllocStringByteLen( chDSN, strlen(chDSN) ); if ( bString==NULL ) { printf( "Could not allocate string" ); return FALSE; } prompt = 123; chReturn = lpCallDll( 1, bString, prompt ); if ( strlen(chReturn) != 0 ) printf( "Function returned\n'%s'\n", chReturn ); /* OLE String must be freed afer use */ SysFreeString( bString ); FreeLibrary( hLib ); return TRUE; }
Code:
'BASIC code #DIM ALL #COMPILE DLL #INCLUDE "win32api.inc" FUNCTION MyFunction ALIAS "MyFunction" ( BYVAL lDbNumber AS LONG, BYVAL sDBName AS STRING, BYREF lPrompt AS LONG ) EXPORT AS STRING FUNCTION = TRIM$(sDBName) END FUNCTION FUNCTION LibMain(BYVAL hInstance AS LONG, _ BYVAL fwdReason AS LONG, _ BYVAL lpvReserved AS LONG) EXPORT AS LONG SELECT CASE fwdReason CASE %DLL_PROCESS_ATTACH LibMain = 1 'success! EXIT FUNCTION CASE %DLL_PROCESS_DETACH LibMain = 1 'success! EXIT FUNCTION CASE %DLL_THREAD_ATTACH LibMain = 1 'success! EXIT FUNCTION CASE %DLL_THREAD_DETACH LibMain = 1 'success! EXIT FUNCTION END SELECT END FUNCTION
------------------
Leave a comment: