Results 1 to 3 of 3

Thread: Using dll exports in c++

  1. #1
    Senior Member
    Join Date
    Jun 2003
    Posts
    772

    Using dll exports in c++

    Hey, I am trying to use exported dll functions like in the following working code:
    Code:
    #include <windows.h>
    #include <iostream.h>
    #include <stdio.h>
    #include <conio.h>
    
    #define MAXMODULE 50
    
    typedef void (WINAPI*cfunc)();
    
    cfunc DoSomething;
    
    void main() {
    
       HINSTANCE hLib=LoadLibrary("SHELL32.dll");
    
    
       if(hLib==NULL) {
    
    cout << "Unable to load library!" << endl;
    getch();   //lame pause
    return;
       }
    
       char mod[MAXMODULE];
    
       GetModuleFileName((HMODULE)hLib, (LPTSTR)mod, MAXMODULE);
       cout << "Library loaded: " << mod << endl;
    
    
       DoSomething=(cfunc)GetProcAddress((HMODULE)hLib, "SHExitWindowsEx");
    
       if((DoSomething==NULL)) {
    
    cout << "Unable to load function(s)." << endl;
    FreeLibrary((HMODULE)hLib);
    return;
       }
    
       DoSomething();
    
       FreeLibrary((HMODULE)hLib);
    
       getch(); //lame pause
    }
    How do I use a function which needs arguments (unlike SHExitWindowsEx)? And where would I find the arguments it needs?
    The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me

    www.elhalf.com

  2. #2
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    See this article for an example of a function that needs a (string) argument:
    http://msdn.microsoft.com/library/de...ic_linking.asp

    I believe SHExitWindowsEx takes an integer argument and is only supported on Windows ME/98:
    http://support.microsoft.com/default...b;en-us;234216

    You can use ExitWindowsDialog with no arguments on newer Windows. It will display the standard Shut Down Windows dialog. Unfortunately, there is no way to know what the user chose. This function will immediately begin the shut down, reboot or whatever she chose (including cancel).

    Note that using LoadLibrary incorrectly can pose security risks (for example, if someone has altered the PATH or replaced the shell32.dll with her own version for malicious purposes). In this case, LoadLibrary should be provided with a fully-qualified path to the "trusted" dll. Also, examine output from DllGetVersion to ensure the correct version of dll is used.

    Peace always,
    <jdenny>
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  3. #3
    Senior Member
    Join Date
    Jun 2003
    Posts
    772
    I understand now, thanks.
    Code:
    #include <windows.h> 
    #include <iostream> 
    
    #define MAXMODULE 50 
    
    typedef int (WINAPI*cfunc)(HWND, LPCTSTR, LPCTSTR, UINT); 
    
    
    void main() { 
    
       HINSTANCE hLib=LoadLibrary("USER32.dll"); 
       cfunc DoSomething; 
    
    
       if(hLib==NULL) { 
    
          std::cout << "Unable to load library!" << std::endl; 
          std::cin.get(); 
          return; 
       } 
    
       char mod[MAXMODULE]; 
    
       GetModuleFileName((HMODULE)hLib, (LPTSTR)mod, MAXMODULE); 
       std::cout << "Library loaded: " << mod << std::endl; 
    
    
       DoSomething=(cfunc)GetProcAddress((HMODULE)hLib, "MessageBoxA"); 
    
       if((DoSomething==NULL)) { 
          std::cout << "Unable to load function(s)." << std::endl; 
          FreeLibrary((HMODULE)hLib); 
          return; 
       } 
    
       DoSomething(0, "Hello World!", "Information", MB_ICONINFORMATION); 
    
       FreeLibrary((HMODULE)hLib); 
    
       std::cin.get(); 
    }
    Someone else has provided me with this code, I simply forgot to give the correct typedef:
    typedef int (WINAPI*cfunc)(HWND, LPCTSTR, LPCTSTR, UINT);
    The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me

    www.elhalf.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •