visualc
asked on
Import and use a function from a DLL
Hi everybody!
What is the good syntax when I use the
GetProcAddress function?
In the x.dll file is the
extern "C" AFX_EXT_API BOOL WINAPI RaduTest(LPSTR, int) function. I can open this with the next code, but I can't find the good syntax to get a henle to the function:
typedef VOID (*MYPROC)(LPTSTR);
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
// Get a handle to the DLL module.
hinstLib = LoadLibrary("c:\\user\\Tib iDLL\\Rele ase\\x.dll ");
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "RaduTest");\\?????????? I DON'T KNOW HOW !!!!@#$?!!
// If the function address is valid, call the function.
if (fRunTimeLinkSuccess = (ProcAdd != NULL))
(ProcAdd) ("message via DLL function\n", 100);
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
What is the good syntax when I use the
GetProcAddress function?
In the x.dll file is the
extern "C" AFX_EXT_API BOOL WINAPI RaduTest(LPSTR, int) function. I can open this with the next code, but I can't find the good syntax to get a henle to the function:
typedef VOID (*MYPROC)(LPTSTR);
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
// Get a handle to the DLL module.
hinstLib = LoadLibrary("c:\\user\\Tib
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "RaduTest");\\?????????? I DON'T KNOW HOW !!!!@#$?!!
// If the function address is valid, call the function.
if (fRunTimeLinkSuccess = (ProcAdd != NULL))
(ProcAdd) ("message via DLL function\n", 100);
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Using the next lines:
FARPROC ProcAdd;
ProcAdd = GetProcAdress(hinstLib, LPTSTR(4));//4 is the ordinal value of the function, I succeded to give a handle to the function, ProcAdd != NULL !!!
But I don't know the next step, the syntax to call the function.
I have a handler (ProcAdd) and the declaration of the functin. HELP!
FARPROC ProcAdd;
ProcAdd = GetProcAdress(hinstLib, LPTSTR(4));//4 is the ordinal value of the function, I succeded to give a handle to the function, ProcAdd != NULL !!!
But I don't know the next step, the syntax to call the function.
I have a handler (ProcAdd) and the declaration of the functin. HELP!
You could also have specified the decorated name, like
ProcAdd = GetProcAdress(hinstLib, "_RaduTest@8");
That would work as well.
>> But I don't know the next step, the syntax to call the function.
What do you mean by this? Do you mean you don't know how to use a function pointer, lie ProcAdd, to call a function in C++? Or do you mean you don't know how the RaduTest function is to be called, that is, what parameters must be passed to it?
If the former, you would do something like
ProcAdd("this is a test");
and it would call RaduTest passing a pointer to the string "this is a test"..
If the later, there is no way to know what a fnction expects in parameters. You could try to figure it out by experementing with different parameters and making reasonable guesses based on what you know the function is supposed to accomplish. the "@8" is helpful, it means that the function takes 8 bytes in parameters. This would either by a single 8 byte structure value (like POINT) or it would be two 4 byte values (ints, char (chars are 1 byte but are passed as 4), or pointers to something)
ProcAdd = GetProcAdress(hinstLib, "_RaduTest@8");
That would work as well.
>> But I don't know the next step, the syntax to call the function.
What do you mean by this? Do you mean you don't know how to use a function pointer, lie ProcAdd, to call a function in C++? Or do you mean you don't know how the RaduTest function is to be called, that is, what parameters must be passed to it?
If the former, you would do something like
ProcAdd("this is a test");
and it would call RaduTest passing a pointer to the string "this is a test"..
If the later, there is no way to know what a fnction expects in parameters. You could try to figure it out by experementing with different parameters and making reasonable guesses based on what you know the function is supposed to accomplish. the "@8" is helpful, it means that the function takes 8 bytes in parameters. This would either by a single 8 byte structure value (like POINT) or it would be two 4 byte values (ints, char (chars are 1 byte but are passed as 4), or pointers to something)
ASKER
You have right, dumpnin.exe show me the RaduTest export function with the ordinal value = 4, hint value = 3 and with the next form:_RaduTest@8 (00001082).
The problem is, that I didn't write this dll, I know only the function declaration. There is only a Radu32.dll and the radu32.lib files, the .def file doesn't exist.
This is a problem; there is not a way to obtain a handler to this function?