Re: function in DLL
"--== Alain ==--" wrote:
Yes, but i would like to not use MFC for that.
if i use the following code, we know that local address is
lost and therefore can not be used :
extern "C" __declspec(dllexport) LPTSTR GetString(HMODULE
hwnd_DLL, int Index);
extern "C" __declspec(dllexport) LPTSTR GetString(HMODULE
hwnd_DLL, int Index)
{
static TCHAR *strRes;
int iError;
TCHAR re[2048] = {'\0'};
strRes=&re[0];
iError = ::LoadString(hwnd_DLL,Index, re,
sizeof(re)/sizeof(re[0]));
return(strRes);
}
i also tried to pass it by reference, but nothing has
changed.
i tried also to turn the function to send back a string
(STL), but nothing works also...
Alain,
You need to get decent C textbook and start reading it.
You're struggling with language features, which are
explained in first couple of chapters of any C programming
tutorial.
Regarding GetString function, it doesn't make any sense. You
don't need to export anything to load string froum
resources. You can call LoadString from anywhere in your
program with appropriate module handle. If you want to have
GetString anyway, then you need to pas to it memory buffer
to store string there. This buffer must stay valid after
GetString returns:
TCHAR szMsg[2048] = { 0 };
GetString(hInst, IDS_MESSAGE_ID, szMsg, 2048);
// use szMsg now..
GetString function should look like this:
extern "C" __declspec(dllexport) LPTSTR GetString(
HMODULE hDLL, int ID, LPTSTR psz, int nBufSize)
{
if(psz)
{
if(LoadString(hDLL, ID, psz, nBufSize))
return psz;
}
return NULL;
}
HTH
Alex