Qbasicnews.com

Full Version: Get a char * from a DLL?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Please be patient, its been several years since I've done anything C\C++ and I am trying to 're-learn'.

How do I get a "string" from a C DLL?

This is the C code I am using to start with (VC++ 6)
Code:
#include "stdafx.h"
#include <stdio.h>
#include <time.h>

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    return TRUE;
}

int ReportVersion() {
    return 1;
    }

char *GetDateTime(){
    time_t rawtime;
    time ( &rawtime );
    return ctime (&rawtime);
}

I have a def file with my functions.

This is how I am calling the functions in FB.

Code:
'$inclib: "testdll01.dll"
declare function rVersion cdecl alias "ReportVersion" () as integer
declare function Now cdecl alias "GetDateTime" () as string ptr
? rVersion
? now

sleep

If I don't use the String Ptr (and use only String) in the Now function it crashes. But with the pointer, I get the address (obvious), when what I want is the content.

Am I way off base with this?
The return type must be "byte ptr", "string" is a struct in FB.

To copy the returned string to an FB string simply do: s = *mybyteptrfunc() -- "s" can be a fixed-len or var-len string.
Thanks! That works perfectly.

Code:
'$inclib: "testdll01.dll"
declare function rVersion cdecl alias "ReportVersion" () as integer
declare function tGetDateTime cdecl alias "GetDateTime" () as byte ptr
function Now () as string
   Now = *tGetDateTime()
end function

dim s as string
? rVersion
? now

sleep