Solved

GetProcAddress()

Posted on 1998-03-26
4
540 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
  • 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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