Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

GetProcAddress()

Posted on 1998-03-26
4
Medium Priority
?
563 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 200 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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…
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

877 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