I have a DLL which expose a function :
extern "C" __declspec( dllimport ) __stdcall int EP_ProtectedStringByKey( char* Key, char* Left, int Len);
Head of the C++ file :
typedef int (*GetString)(const char *Key, const char *Str, int Len);
With this code, everything works fine. The program write in the "example.txt" what it should write.
GetString _GetSTR;
int longueur;
string my_string;
const char *pmy_string;
HINSTANCE hInstLibrary = LoadLibrary("my.dll");
_GetSTR = (GetString)GetProcAddress(hInstLibrary, "EP_ProtectedStringByKey");
ofstream myfile("example.txt");
longueur = _GetSTR("key_421", NULL, 0);
cout << "Taille : " << longueur << endl;
myfile << "Size : " << longueur << endl;
my_string.resize(longueur); // resizing the string
pmy_string = my_string.c_str(); // Pointer to pmy_string
// ---- _GetSTR("key_421", pmy_string, longueur); ----
myfile << "Another line.\n";
myfile.close();
FreeLibrary(hInstLibrary);
Now if I add this line of code :
_GetSTR("key_421", pmy_string, longueur);
My data is well print on the screen with :
cout << "Data : " << pmy_string << endl;
But NOTHING is write anymore in the file "example.txt ("Another line" isn't write anymore). It's like after fulling the my_string variable, my program refused to write in file. But strangely it show the content of pmy_string on the screen.
I suppose I'am doing something wrong but I can't figure what...
Thanks for help,
To avoid two undefined behavior, I need to :
typedef int (*GetString)(const char *Key, const char *Str, int Len);
In
typedef int (__stdcall *GetString)(const char *Key, const char *Str, int Len);
pmy_string = my_string.c_str(); // Pointer to pmy_string
In
pmy_string = my_string.data(); // Pointer to pmy_string