Solved

Import and use a function from a DLL

Posted on 1998-11-05
4
267 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared 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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

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

10 Experts available now in Live!

Get 1:1 Help Now