• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

Import and use a function from a DLL

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
visualc
Asked:
visualc
  • 2
  • 2
1 Solution
 
nietodCommented:
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
 
visualcAuthor Commented:
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
 
visualcAuthor Commented:
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
 
nietodCommented:
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now