Call function in c++ dll without header
Asked Answered
M

6

17

I would like to call a method from an dll, but i don't have the source neither the header file. I tried to use the dumpbin /exports to see the name of the method, but i can found the methods signature?

Is there any way to call this method?

Thanks,

Metalwork answered 16/2, 2009 at 21:4 Comment(1)
What is the exact name of the method shown by dumpbin /exports?Telson
C
4

It is possible to figure out a C function signature by analysing beginnig of its disassembly. The function arguments will be on the stack and the function will do some "pops" to read them in reverse order. You will not find the argument names, but you should be able to find out their number and the types. Things may get more difficult with return value - it may be via 'eax' register or via a special pointer passed to the function as the last pseudo-argument (on the top of the stack).

Cystotomy answered 8/3, 2009 at 7:10 Comment(0)
G
10

If the function is a C++ one, you may be able to derive the function signature from the mangled name. Dependency Walker is one tool that will do this for you. However, if the DLL was created with C linkage (Dependency Walker will tell you this), then you are out of luck.

Gypsum answered 16/2, 2009 at 21:18 Comment(0)
P
9

The C++ language does not know anything about dlls.

Is this on Windows? One way would be to:

  • open the dll up in depends.exe shipped with (Visual Studio)
  • verify the signature of the function you want to call
  • use LoadLibrary() to get load this dll (be careful about the path)
  • use GetProcAddress() to get a pointer to the function you want to call
  • use this pointer-to-function to make a call with valid arguments
  • use FreeLibrary() to release the handle

BTW: This method is also commonly referred to as runtime dynamic linking as opposed to compile-time dynamic linking where you compile your sources with the associated lib file.

There exists some similar mechanism for *nixes with dlopen, but my memory starts to fail after that. Something called objdump or nm should get you started with inspecting the function(s).

Predicament answered 16/2, 2009 at 21:18 Comment(0)
H
5

As you have found, the exports list in a DLL only stores names, not signatures. If your DLL exports C functions, you will probably have to disassemble and reverse engineer the functions to determine method signatures. However, C++ encodes the method signature in the export name. This process of combining the method name and signature is called "name mangling". This Stackoverflow question has a reference for determining the method signature from the mangled export name.

Try the free "Dependency Walker" (a.k.a. "depends") utility. The "Undecorate C++ Functions" option should determine the signature of a C++ method.

Hakenkreuz answered 16/2, 2009 at 21:25 Comment(0)
C
4

It is possible to figure out a C function signature by analysing beginnig of its disassembly. The function arguments will be on the stack and the function will do some "pops" to read them in reverse order. You will not find the argument names, but you should be able to find out their number and the types. Things may get more difficult with return value - it may be via 'eax' register or via a special pointer passed to the function as the last pseudo-argument (on the top of the stack).

Cystotomy answered 8/3, 2009 at 7:10 Comment(0)
S
1

If you indeed know or strongly suspect the function is there, you can dynamically load the DLL with loadLibrary and get a pointer to the function with getProcAddress. See MSDN

Note that this is a manual, dynamic way to load the library; you'll still have to know the correct function signature to map to the function pointer in order to use it. AFAIK there is no way to use the dll in a load-time capability and use the functions without a header file.

Shortie answered 16/2, 2009 at 21:20 Comment(0)
S
1

Calling non-external functions is a great way to have your program break whenever the 3rd party DLL is updated.

That said, the undname utility may also be helpful.

Shool answered 16/2, 2009 at 21:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.