?
Solved

Import and use a function from a DLL

Posted on 1998-11-05
4
Medium Priority
?
285 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
[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 22

Accepted Solution

by:
nietod earned 200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

752 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