Вопрос:

How to Link to a function from a DLL loaded by a DLL?

c++ visual-studio-2010 mfc vc6

152 просмотра

1 ответ

1828 Репутация автора

I'm using VS2010 and I have a layered application that looks like this:

app.exe
DLL1.dll
DLL2.dll

where app.exe loads DLL1 and it then loads DLL2. I want my app to call a function in DLL2 like so DLL2::setvariable( value ).

I've set this up properly in my vc6 workspace without having to specify the lib file for DLL2.dll. How can I set this up in my VS2010 environment? I keep getting an unresolved symbol linker error for all the calls to DLL2.dll.

I've tried to include #pragma comment(lib ,"DLL2.lib") in both app.exe and DLL1.dll and it links but does this load DLL2.dll and all its variables twice?

Автор: Shabbir Hussain Источник Размещён: 08.11.2017 10:28

Ответы (1)


2 плюса

934 Репутация автора

Решение

A DLL is only loaded once in an application's address-space. This is by Windows design. An import-library for a dll loads the dll (LoadLibrary()) and assigns local function names to the dll functions. You could do the same yourself by calling LoadLibrary() and GetProcAddress().

According to the Documentation:

The system maintains a per-process reference count on all loaded modules. Calling LoadLibrary increments the reference count. Calling the FreeLibrary or FreeLibraryAndExitThread function decrements the reference count. The system unloads a module when its reference count reaches zero or when the process terminates (regardless of the reference count).

So no, a DLL is only loaded once, only its "UseCount" is set to 2.

I've set this up properly in my vc6 workspace without having to specify the lib file for DLL2.dll

I guess you weren't calling DLL2::setvariable( value ) from app.exe then, otherwise how did you do it?

Is this the right way to do it?

This is one way to do it (though you could simply include the lib into the project - as most developers do - instead of using the #pragma, but the rseult is the same). The other alternative is call LoadLibrary()/GetProcAddress() yourself (useful if you want to conditionally load the dll). There's no other way i'm aware of.

Автор: Constantine Georgiou Размещён: 08.11.2017 11:12
Вопросы из категории :
32x32