How to use a C++ class dll loaded explicitly in vi

2019-08-05 05:09发布

问题:

For exactly the reason mentioned here: http://msdn.microsoft.com/en-us/library/vstudio/253b8k2c.aspx

"The application does not know the name of a DLL that it will have to load until run time"

I need to load a dll that doesn't bind its name to the application. That is, I don't need the application to require "myDll.dll" to work (because in our configuration system, myDll.dll is not named like that). However, using GetProcAddress for every function doesn't seems like a good idea, specially since it needs the decorated names, and that's error prone.

I was wondering if there's a way to continue using __declspec(dllimport) or something similar without the dll name binding.

My last resort is to create a C interface and a class that uses GetProcAddress, but I think there should be a better way.

Edit:

I should note that I can edit the .cpp and .h of the library, create a .lib, etc.

I can even (but this is very specific to this application) create an object of the class contained in the dll (we have some hooks for this). However, I can't use the header of my dll class, because then it requires that I load "myDll.dll"

回答1:

Use the dumpbin program to list the exported symbols:

dumpbin /exports OLDNAME.dll

You will get that kind of output:

Microsoft (R) COFF/PE Dumper Version 10.00.40219.01
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file OLDNAME.dll

File Type: DLL

  Section contains the following exports for OLDNAME.dll

    00000000 characteristics
    529B7ABB time date stamp Sun Dec 01 19:06:51 2013
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names

    ordinal hint RVA      name

          1    0 00011109 ?MtDlladd@@YAHHH@Z = @ILT+260(?MtDlladd@@YAHHH@Z)

  Summary

        1000 .data
        1000 .idata
        2000 .rdata
        1000 .reloc
        1000 .rsrc
        4000 .text
       10000 .textbss

Now create a new text file with the following contents:

LIBRARY NEWNAME
EXPORTS
?MtDlladd@@YAHHH@Z

Replace NEWNAME with the final name of your problematic DLL. Under EXPORTS, copy all the function names given by implib, as is. Name that text file NEWNAME.DEF

Make a new lib file with that DEF file, using lib:

lib /def:NEWNAME.DEF /OUT:NEWNAME.lib

Now you can link with that lib, and your application will require NEWNAME.DLL