Solved

Import and use a function from a DLL

Posted on 1998-11-05
4
274 Views
Last Modified: 2013-12-03
Hi everybody!
What is the good syntax when I use the
GetProcAddress function?
In the x.dll file is the
extern "C" AFX_EXT_API BOOL WINAPI RaduTest(LPSTR, int) function. I can open this with the next code, but I can't find the good syntax to get a henle to the function:
typedef VOID (*MYPROC)(LPTSTR);
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;  
// Get a handle to the DLL module.  
hinstLib = LoadLibrary("c:\\user\\TibiDLL\\Release\\x.dll");  
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
    ProcAdd = (MYPROC) GetProcAddress(hinstLib, "RaduTest");\\?????????? I DON'T KNOW HOW !!!!@#$?!!  
// If the function address is valid, call the function.
        if (fRunTimeLinkSuccess = (ProcAdd != NULL))
            (ProcAdd) ("message via DLL function\n", 100);  
// Free the DLL module.        
        fFreeResult = FreeLibrary(hinstLib);
    }      
0
Comment
Question by:visualc
  • 2
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1415782
There are two potential problems.  First, the library might not be loaded so you get a NULL hinstlib.  Second, the procedure might not be found so you get a NULL ProcAdd.

I assume that it is the 2nd problem, but you should test (if you haven't already) to make sure that is the case.  

If it is the 2nd problem, you should check the exports from the DLL to see if you can find the function exported.  (use dumpbin in VC's bin directory)  If not, then you have made a mistake when you compiled the DLL and did not export the function.  Most likley, however, you will see the function exported but with a "@XXX" at the end of the name.  That is becuase you declared to use the standard call calling convention.  You should switch to a different convention or you should list the procedure in the .def file.  In either case the "@XXX" wll be removed.

let me know if you have quesitons.
0
 

Author Comment

by:visualc
ID: 1415783
Hi nietod!

  You have right, dumpnin.exe show me the RaduTest export function with the ordinal value = 4, hint value = 3 and with the next form:_RaduTest@8 (00001082).
  The problem is, that I didn't write this dll, I know only the function declaration. There is only a Radu32.dll and the radu32.lib files, the .def file doesn't exist.
  This is a problem; there is not a way to obtain a handler to this function?  
0
 

Author Comment

by:visualc
ID: 1415784
Using the next lines:
    FARPROC ProcAdd;
    ProcAdd = GetProcAdress(hinstLib, LPTSTR(4));//4 is the ordinal value of the function, I succeded to give a handle to the function, ProcAdd != NULL !!!

But I don't know the next step, the syntax to call the function.
I have a handler (ProcAdd) and the declaration of the functin. HELP!
0
 
LVL 22

Expert Comment

by:nietod
ID: 1415785
You could also have specified the decorated name, like

ProcAdd = GetProcAdress(hinstLib, "_RaduTest@8");

That would work as well.

>> But I don't know the next step, the syntax to call the function.

What do you mean by this?  Do you mean you don't know how to use a function pointer, lie ProcAdd, to call a function in C++?  Or do you mean you don't know how the RaduTest function is to be called, that is, what parameters must be passed to it?

If the former, you would do something like

ProcAdd("this is a test");

and it would call RaduTest passing a pointer to the string "this is a test"..

If the later, there is no way to know what a fnction expects in parameters.  You could try to figure it out by experementing with different parameters and making reasonable guesses based on what you know the function is supposed to accomplish.  the "@8" is helpful, it means that the function takes 8 bytes in parameters.  This would either by a single 8 byte structure value (like POINT) or it would be two 4 byte values (ints, char (chars are 1 byte but are passed as 4), or pointers to something)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Event ID 10010 3 69
mixing C++ & C# in Vis Studio 2013 7 201
T-SQL Debugging - Temp Object Content 2 54
Poor Man's .NET Framework Certification 2 60
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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

839 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