0
votes

I'm creating a DLL lib that should be used during runtime (i.e. loaded in Windows 7 with LoadLibrary, closed with FreeLibrary and function handles given by GetProcAddress). This is being done in C++ using Borland C++ Builder. All functions in the DLL should receive some value by reference as parameter (normally std::string).

By now the method I'm using to do this is the following (example) (summarized):

typedef void (*HIS_validity)(string &);

//LoadLibrary

HIS_validity fValidity = (HIS_validity) GetProcAddress(frMain->HIS_DLL.hisLibrary,"checkForValidity");

if (fValidity == NULL) return;

string testeValidade;
fValidity(testeValidade);
const AnsiString testeValidade2(testeValidade.c_str());

if (testeValidade2 != "...")
//etc...

In the DLL:

extern "C" void LIBRARY_API checkForValidity(string &str);

void checkForValidity(string &str)
{
    str = "...";
}

Now this code is running fine. The problem is that in some functions I want to pass a whole array of strings by reference. Previously I discovered how to pass a string array by reference here and I though it would be just a matter of ajusting things accordingly:

typedef void (*HIS_patientData)(string (&)[32]);

HIS_patientData fPatientData = (HIS_patientData) GetProcAddress(frMain->HIS_DLL.hisLibrary,"patientDataFields");

string strDado2[32];
fPatientData(strDado2);
frMain->pluginData.patientData.numProntuario = AnsiString(strDado2[cont1++].c_str());
frMain->pluginData.patientData.pacNome = AnsiString(strDado2[cont1++].c_str());

In the DLL:

extern "C" void LIBRARY_API patientDataFields(string (&str)[32]);

void patientDataFields(string (&str)[32])
{
    str[0] = "One";
    str[1] = "Two";
    str[2] = "Three";
    //....
}

But here the problem appears. When I compile and run my application, the same problem always come up: if my function in the DLL has only two data attributed to 'str[]', the code goes one after 'fPatientData(strDado2);' but when I read the content of strDado2[0], it has the value of str[1] and strDado2[1] has NULL inside! By the other hand, if I add three or more attributions to 'str[]' in my DLL function, the software always crash when it comes to 'fPatientData(strDado2);' with a pop-up telling "access violation ... in module libstdc++-6.dll".

And I have no ideia what the problem is :T

Thanks for any help,

Momergil

1
Just a note: When you specify a calling convention in the prototype (which is a pretty good thing for a library) it is "advisable" to specify it in the function definition, too. - Max Truxa
Well, assuming LIBRARY_API is a calling convention ofc... - Max Truxa
And 2 more notes: It is netiquette to always code in English, especially when posting sample code, since it is much easier to read for most people. And it is pretty much a no-go to use using namespace std. - Max Truxa
@yourmt: about the english, well all the relevant parts are in English o.O About "using namespace std", I only use it when too much calls to std:: are set in the code; is quite boring to put std:: too much times... About LIBRARY_API: #if defined(_WIN32) #define LIBRARY_API __declspec(dllexport) #else #define LIBRARY_API #endif - Momergil

1 Answers

0
votes

Ok, it seems I found the answer to all such problems... Namely, I'm trying to return a C++ class (std::string) in a "extern "C"" function. It was just a matter of making it return a standart const char* that everything started to run just fine.

Thanks for the contributors,

Momergil