Creating and using dll: __declspec (dllimport) and GetProcAddress

Imagine that we have a solution with two projects: MakeDll (a dll application) that creates a dll and UseDll (an exe application) that uses a dll. Now I know that there are two ways, one is pleasant, the other is not. The nice way is that UseDll communicates statically with MakeDll.lib and just dllimports functions and classes and uses them. An unpleasant way is to use LoadLibrary and GetProcAddress, which I can not even imagine how to do this with overloaded functions or class members, in other words, nothing but the external "C" functions.

My questions are the following (all regarding the first option)

  • What exactly does MakeDll.lib contain?
  • When is the MakeDll.dll file loaded into my application and when is it downloaded? Can i control this?
  • If I change MakeDll.dll, can I use the new version (if it is a superset of the old one in terms of interface) without using UseDll.exe? A special case is when a polymorphic class is exported and a new virtual function is added.

Thanks in advance.

PS I am using MS Visual Studio 2008

+3
source share
3 answers
  • MakeDll.lib contains a list of pins for exported functions and their RVAs in MakeDll.dll

  • MakeDll.dllIt is loaded into the application based on what type of load is defined for the corresponding DLL. (e.g. DELAYLOAD ). Raymond Chen has an interesting article about this.

  • MakeDll.dll, RVA, UseDll.exe, . vtable , vtable, UseDll.exe. dll UseDll.exe.

+1
  • DLL, , ( ). , UseDLL.exe - , ( ): " xxx MakeDll.dll". , () DLL, , ( ) GetProcAddress , , .

  • . /delayload , DLL.

  • , . , vtable , . , , , .

+3

- LoadLibrary GetProcAddress, , , , , "C".

, , , . , - :

// Common.h: interface common to both sides.
// Note: 'extern "C"' disables name mangling on methods.
extern "C" class ISomething
{
    // Public virtual methods...

        // Object MUST delete itself to ensure memory allocator
        // coherence. If linking to different libraries on either
        // sides and don't do this, you'll get a hard crash or worse.
        // Note: 'const' allows you to make constants and delete
        // without a nasty 'const_cast'.
    virtual void destroy () const = 0;
};

// MakeDLL.c: interface implementation.
class Something : public ISomething
{
    // Overrides + oher stuff...

    virtual void destroy () const { delete this; }
};

extern "C" ISomething * create () { return new Something(); }

++ (.. g++ MSVC 4 ).

Something , . ! , : ISomething. , , ( DirectX) ( COM). , , ... !

+1

Source: https://habr.com/ru/post/1768769/


All Articles