Converting a Windows Dll to .lib for C++ Visual St

2019-03-12 21:52发布

问题:

I know there is a tool called Dll to lib but the developer is asking $1000. I only need to convert one library, once, so its not easy to justify that price.

I tried IMPLIB32.EXE, but I just get empty .lib files. How can I accomplish this? Perhaps I can write a simple conversion app?

Added1:

The Dll's are typically stdcall not cdecl and written in older C like languages NOT C# or .NET or C++. I now need to call them from C++ apps. An example would be the SQLite.dll or zlib.dll. I do not have access to the .lib files for these dll's.

Added2:

I re-wrote this code for VS2008 http://floodyberry.wordpress.com/2008/09/08/generating-dll-wrappers/ and included the example Dll etc downloadable here: http://www.transferbigfiles.com/Get.aspx?id=7d86fa0b-6ddc-4f6f-8d31-2c20824aae9a This in turn makes a project that creates a Dll. When I try to compile the Dll I get the linker error: AddShow.dll : fatal error LNK1107: invalid or corrupt file: cannot read at 0x300 Described here: http://list.isis.vanderbilt.edu/pipermail/udm-users/2006-March/000664.html Not sure how to proceed. So close yet so far

Next we move to this method

http://www.coderetard.com/2009/01/21/generate-a-lib-from-a-dll-with-visual-studio/

Running dumpbin with the argument /exports C:\path\to\AddShow.dll does absolutly nothing After some research

http://msdn.microsoft.com/en-us/library/aa446532.aspx it seems that mspdb71.dll (now mspdb80.dll) is needed from the common/ide folder dumpbin.exe now runs with error:

fatal error LNK1106: invalid file or disk full: cannot seek to 0x6179A These threads suggests the version of dumpbin.exe might be the issue

I have Microsoft (R) COFF/PE Dumper Version 9.00.30729.01

So I tried Microsoft (R) COFF Binary File Dumper Version 5.12.8078 with no success. After much reading I am no closer

http://support.microsoft.com/kb/815645 http://support.microsoft.com/kb/839286 http://markmail.org/message/p5vwzyfyv3bs6z34 http://fixunix.com/programmer/94825-fatal-error-lnk1106-invalid-file-disk-full.html

When I run ProcMon I see the first occurance of queryopen and sqlite3.dll when svchost.exe tries to open it and fails with the error PATH NOT FOUND. the path is C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\SQLITE3.DLL and is correct. If I put it at the root of the C drive then I get NAME NOT FOUND errors:

C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\link.exe.Local

C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\dumpbin.exe.Local

from link.exe and dumpbin.exe respectivly. Im using XPSP3 not Vista and this is about the limit of my knowledge of sysinternals. what are these .local files?

(csrss.exe is also not able to find a few manifest files.)

So no success yet, just more mystery

Added 3:

I tried to run dumpbin.exe from its installed location, \Program Files\Microsoft Visual Studio 8\VC\bin, but the OS said it couldn't find mspdb80.dll. I copied mspdb80.dll from \Program Files\Microsoft Visual Studio 8\Common7\IDE to try to get dumpbin.exe to run.

now I get the error: "c1902 program database manager mismatch please check your installation"

If I remove mspdb80.dll from \Program Files\Microsoft Visual Studio 8\VC\bin the error goes away! but I can't run dumpbin.exe.

Added 4:

I was finally able to get dumpbin to run by copying the following files to a folder:

dumpbin.exe link.exe lib.exe mspdb80.dll

I did get the error:

fatal error LNK1248: image size (FFFFFXXX) exceeds maximum allowable size (80000000)

once, but replacing the dll fixed that. Presumably it got corrupted?

I then moved onto the next step in the instructions: http://www.coderetard.com/2009/01/21/generate-a-lib-from-a-dll-with-visual-studio/ http://support.microsoft.com/kb/131313 and got the error: warning lnk4017 statement not supported for the target platform ignored This turns out to be because I specified the .dll instead of the .def file.

The resulting .Lib and .exp files are then added to the VS2008 project and compile and run. The debugger then reported an error: Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call...

As mentioned here "Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call" after successful C# callback from the C++ code of GameSpy lib this is because I used stdcall in my dll and declared __cdecl in my app.

extern "C" { // put Dll C function prototypes here
 int __cdecl AddTwoNum(int n, double f); // __stdcall
}

So changing this to __stdcall should fix it you would think.. but alas no.

now I get a linking error: error LNK2001: unresolved external symbol _AddTwoNum@12

This is a decorated function name for some reason. Why?

Added n:

Well this turned out to be because the .lib file was made using a dll that with STDCALL functions. STDCALL requiers the caller to clean up the stack so the number of bytes of the arguments is appended to the function name with an @ sign. IN this case I had three 4Byte iintegers for a total of 12 bytes.

Once I remade the .lib file from a dll created with the CDECL calling convention then all was good.

回答1:

I can't seem to comment on his answer (I need more reputation?), but you can try using my .dll wrapper generator to create the stub .dll iain suggested.



回答2:

I am assuming you want to call some function/methods in a dll in C/C++ without having access to the lib file to link with it.

If you know the signatures of the functions you require, you can:

  • create your own stub version of the dll
  • create a dll project, with the same name as the dll you want to use
  • add stub implementations of the functions you want to call
  • the signatures are very important use depends.exe or dumpbin.exe to check they match.
  • then write/build your program to link to the stub dll
  • to run the program replace the stub dll with the real one


回答3:

You could also go the dynamic way, using LoadLibrary() and GetProcAddress().

(See second link for an example).



回答4:

In cases like zlib or sqlite you can download the source directly and compile your own lib file. In fact, I just did this the other day for zlib, I downloaded the source from www.zlib.net (direct zlib 1.2.3 download) opened the provided visual studio project file and compiled both a debug and release version of the lib. I haven't tried sqlite but seeing that they have the sources, it shouldn't be difficult to do.

If it's a proprietary dll, you could try asking the original developer or company for a lib file, or write your own stub dll that iain suggested or go the dynamic route that RaphaelSP.

If they are exported in C (no C++ name mangling) I would go the dynamic route myself.



回答5:

I found this example rather quickly: http://www.coderetard.com/2009/01/21/generate-a-lib-from-a-dll-with-visual-studio/



回答6:

This is supposedly a free tool that makes the conversion. I'm not sure if it's "free" as in "limited trial version" or not, the information is not very clear. If you've already tested this, edit your question and I'll delete this answer.



回答7:

If you contact the developer of the Dll to lib tool they might be willing to give you a discount.

I've been in these situations before (where I needed an expensive tool just one time), and sometimes I have had success in getting a large discount.

It never hurts to try.

Good luck.



回答8:

I have had success in getting a lib from DLL using details in http://wyw.dcweb.cn/dllfaq.htm. Your mileage might vary. I have used this to get a lib from python DLL to build some python extensions that are written in 'C'.



回答9:

Open source edll has its own internal COFF loader. edll is written for quite different purposes, but since it has independent COFF loader within, maybe it's possible to incorporate your .dll into binary stream or resource inside your .exe, then in run-time use edll's COFF loader to load built-in dll.