Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function in c++ dll without header

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,

like image 787
mileschet Avatar asked Feb 16 '09 21:02

mileschet


People also ask

Does DLL need header?

Yes. Otherwise, your customers will have to manually declare the functions themselves before they can use it.

Can we execute a program without header file?

Yes you can wirte a program without #include , but it will increase the complexity of the programmer means user have to write down all the functions manually he want to use.It takes a lot of time and careful attention while write long programs.


3 Answers

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.

like image 123
zdan Avatar answered Oct 11 '22 10:10

zdan


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).

like image 38
dirkgently Avatar answered Oct 11 '22 12:10

dirkgently


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.

like image 45
Peter Neubauer Avatar answered Oct 11 '22 10:10

Peter Neubauer