The old time classic dll questions!
-
Hi, as you 've probably guessed I tring to create a dll and use it in another program. So I've search some articles and I 've found a few interesting things. However when I try to execute a simple program it doesn't seem to work. Let's be a bit more specific.
#include "stdafx.h" #include "greet.h" #include BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } // This is an example of an exported variable GREET_API int ngreet=0; // This is an example of an exported function. GREET_API int fngreet(void) { return 42; } // This is the constructor of a class that has been exported. // see greet.h for the class definition Cgreet::Cgreet() { return; } void Cgreet::run(int arg) { printf("Hello World!\n"); }
The above code is produced by VS 7.1 when asked to create a new win32 console project and dll is selected. I 've also added another method to the class created. I also give you the header file VS created.#ifdef GREET_EXPORTS #define GREET_API __declspec(dllexport) #else #define GREET_API __declspec(dllimport) #endif // This class is exported from the greet.dll class GREET_API Cgreet { public: Cgreet(void); // TODO: add your methods here. void run(int arg); }; extern GREET_API int ngreet; GREET_API int fngreet(void);
Well now I create a simple win32 console project with the following code#include #include int main() { HMODULE hdll = LoadLibrary("greet.dll"); if (hdll != NULL) { typedef int (*PFUNC)(void); PFUNC greet = (PFUNC)GetProcAddress(hdll, "fngreet"); if (greet != NULL) { // Now we can call the function in the DLL printf("greet.dll returned: %d!\n", greet()); } else printf("Loading function 'fngreet' failed!\n"); } else printf("Loading library 'greet.dll' failed!\n"); }
The output of execution is: 'Loading function 'fngreet' failed!\n' I 've also seen in another post someone asked functions like "_fngreet" but that also didnt work. What am I doing wrong? Looking the code with the debugger I see in my main program that hdll pointer gets a weird address. hdll 0x10000000 {unused=9460301 } HINSTANCE__ * That's what the debugger says about hdll and -
Hi, as you 've probably guessed I tring to create a dll and use it in another program. So I've search some articles and I 've found a few interesting things. However when I try to execute a simple program it doesn't seem to work. Let's be a bit more specific.
#include "stdafx.h" #include "greet.h" #include BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } // This is an example of an exported variable GREET_API int ngreet=0; // This is an example of an exported function. GREET_API int fngreet(void) { return 42; } // This is the constructor of a class that has been exported. // see greet.h for the class definition Cgreet::Cgreet() { return; } void Cgreet::run(int arg) { printf("Hello World!\n"); }
The above code is produced by VS 7.1 when asked to create a new win32 console project and dll is selected. I 've also added another method to the class created. I also give you the header file VS created.#ifdef GREET_EXPORTS #define GREET_API __declspec(dllexport) #else #define GREET_API __declspec(dllimport) #endif // This class is exported from the greet.dll class GREET_API Cgreet { public: Cgreet(void); // TODO: add your methods here. void run(int arg); }; extern GREET_API int ngreet; GREET_API int fngreet(void);
Well now I create a simple win32 console project with the following code#include #include int main() { HMODULE hdll = LoadLibrary("greet.dll"); if (hdll != NULL) { typedef int (*PFUNC)(void); PFUNC greet = (PFUNC)GetProcAddress(hdll, "fngreet"); if (greet != NULL) { // Now we can call the function in the DLL printf("greet.dll returned: %d!\n", greet()); } else printf("Loading function 'fngreet' failed!\n"); } else printf("Loading library 'greet.dll' failed!\n"); }
The output of execution is: 'Loading function 'fngreet' failed!\n' I 've also seen in another post someone asked functions like "_fngreet" but that also didnt work. What am I doing wrong? Looking the code with the debugger I see in my main program that hdll pointer gets a weird address. hdll 0x10000000 {unused=9460301 } HINSTANCE__ * That's what the debugger says about hdll andYour GetProcAddress on "fngreet" doesn't work because it's real name isn't fngreet, C++ uses decorated functions so you will get something different which will change if you add/remove arguments, change types. You can use Dependency Walker to see what the real name is and use that. It's recommended to use C linkage instead which will make your GetProcAddress work. Declare the function using extern "C".
#ifdef GREET_EXPORTS #define GREET_API extern "C" __declspec(dllexport) #else #define GREET_API extern "C" __declspec(dllimport) #endif
You cannot use GetProcAddress to return a class, however you can create a function which returns a class and use that function.