Solved

GetProcAddress()

Posted on 1998-03-26
4
551 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:bod_1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
tflai earned 50 total points
ID: 1412320
You have to typedef the function pointer:

typedef VOID (WINAPI * MYDLLFUNCTION)(BOOL bFlag);
HINSTANCE hDLL = LoadLibrary("MYDLL.DLL");
MYDLLFUNCTION MyDllFunction = (MYDLLFUNCTION)GetProcAddress(hDLL, "My_Dll_Function");
...
Then you can call it such as:
(*MyDllFunction)(TRUE);
or simply MyDllFunction(TRUE);

Of course, don't forget to check for errors and null handles.
0
 

Author Comment

by:bod_1
ID: 1412321
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
0
 
LVL 4

Expert Comment

by:tflai
ID: 1412322
You need to typecast the function pointer for parameter type checking.  (I don't think the compiler would even allow you doing that...)
0
 

Author Comment

by:bod_1
ID: 1412323
Thanks again Tflai
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question