Link to home
Start Free TrialLog in
Avatar of bod_1
bod_1

asked on

GetProcAddress()

msvc++4
Hi,
Im trying to use a function in a dll.  I can load and get a handle to the dll file but GetProcAddress()  always fails for me.
The function is named in my dll's .def file's EXPORTS list.
Its prototype is:
long CountLines(char* pFileName);

Here is a clip of whats going wrong:

HINSTANCE      hDllInst = NULL;      // Handle for LoadLibrary()
HMODULE      hMod = NULL;      // Handle for GetModuleHandle()
long (*pFunc)(char*) = NULL;       // The prototype matches my dll's function's arguments
// Im only calling both of these functions to see if i can get even one of them to work with GetProcAddress()
// Everything is fine with these.  The library's DllMain() gets processed.

hDllInst = ::LoadLibrary(m_Library);
ASSERT(hDllInst != NULL);
hMod = ::GetModuleHandle(m_Library);
ASSERT(hMod != NULL);
/////////////////////////////////////////////

// Here is where im lost.  It says GetProcAddress returns the (FARPROC?) address of the function.  I assume //FARPROC is simply an address so  I make pFunc() point to the address returned by GetProcAddress() function
pFunc -> ::GetProcAddress( hDllInst, "My_Dll_Function");
ASSERT(pFunc != NULL);                   // FAIL
pFunc-> ::GetProcAddress( hMod, "My_Dll_Function");
ASSERT(pFunc != NULL);                   // FAIL

// Both of these assertions fail.

Not sure what the return FARPROC is but if it's the address of the function than wouldnt it be correct to make my function  pointer equal the return of GetProcAddress().
pFunc = ::GetProcAddress(hMod, "My_Dll_Function");           // No conversion from int(__stdcall*)(void) ... ERROR

A book i have says to do the above line.

Likewise couldnt I make my pFunc equal to a FARPROC variable which is assigned the return of GetProcAddress()?
FARPROC fp = GetProcAddress(hMod, "My_Dll_Function");
pFunc = fp;             // No conversion from int(__stdcall*)(void) ... ERROR


Thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of tflai
tflai

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bod_1
bod_1

ASKER

Thanks Tflai,
     That some tricky stuff there.  Could I also do something like this?

FARPROC  pFunc;
HINSTANCE hDLL;
hDLL = LoadLibrary( "MYDLL");
.../
pFunc = GetProcAddress(hDLL, "MY_LIBRARY_FUNC");
(*pFunc)(param1, param2);  


Thanks
You need to typecast the function pointer for parameter type checking.  (I don't think the compiler would even allow you doing that...)
Avatar of bod_1

ASKER

Thanks again Tflai