Solved

GetProcAddress()

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now