Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. The old time classic dll questions!

The old time classic dll questions!

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studiodebuggingtutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Themis
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • T Themis

      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

      M Offline
      M Offline
      mark novak
      wrote on last edited by
      #2

      Your 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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups