Like Tree4Likes
  • 1 Post By VirtualAce
  • 1 Post By CommonTater
  • 2 Post By CommonTater

GetProcAddress ...

This is a discussion on GetProcAddress ... within the Windows Programming forums, part of the Platform Specific Boards category; I load a DLL dinamically... But I want to call a function from it passing arguments... How should I use ...

  1. #1
    INSANE INSIDE ekosix's Avatar
    Join Date
    May 2010
    Location
    Rio de Janeiro, Brazil
    Posts
    44

    Question GetProcAddress ...

    I load a DLL dinamically...

    But I want to call a function from it passing arguments...

    How should I use GetProcAddress??

    __
    OK..
    let me try to explain better...

    my .cpp file should load a dll.
    Then the .cpp should call few methods in the dll.
    Well im able to call the methods with no parameters but im unable to call the methods with parameters.

    How do I proceed?
    Last edited by ekosix; 04-06-2011 at 01:41 AM. Reason: to explain better

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    142
    And that is your own DLL or one provided by the system?

    There shouldn't be any problem when you call stuff from system DLLs, just use _stdcall to mark your function pointers. It gets tricky when you create your own stuff.

    Win API designers chose to create all Windows DLLs in way so they can be used from different languages so entry points to functions are not designed as normal C functions, but like pascal functions. That is why you have those _stdcall keywords.

    When you create your own DLLs, you can have _stdcall or _cdecl entry points. I always use _cdecl because this way my function names do not get mangled, everything is natural to C conventions and I have never used other languages to call these DLLs so why bother.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Code:
    typedef void (*FuncNoParams)();
    typedef void (*FuncWithParams)(int value);
    
    ...
    ...
    HMODULE module = LoadLibrary("MyDLL.dll");
    if (module)
    {
        FuncNoParams noParamFunc;
        FuncWithParams paramFunc;
        
        noParamFunc = (FuncNoParams)GetProcAddress(module,"FooNoParams");
        if (noParamFunc)
        {
             noParamFunc();
        }
    
        paramFunc = (FuncWithParams)GetProcAddress(module,"FooWithParams");
        if (paramFunc)
        {
            paramFunc(0);
        }
    
        FreeLibrary(module);
    }
    ...
    ...
    DLL import/export header - MyModule_DLL.h
    Code:
    #pragma once
    
    #ifdef MYDLL_EXPORTS
        #define MYDLL_API __declspec(dllexport)
    #else
        #define MYDLL_API __declspec(dllimport)
    #endif
    DLL module header
    Code:
    #pragma once
    
    #include "MyModule_DLL.h"
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    MYDLL_API void FooNoParams();
    MYDLL_API void FooWithParams(int value);
    
    #ifdef __cplusplus
    }    
    #endif
    ...
    DLL implementation
    Code:
    #include "MyDLL.h"
    
    void FooNoParams()
    {
    }
    
    void FooWithParams(int value)
    {
        value;
    }
    I did not test this code but it should get you extremely close to a working DLL and a client that can load and use it.
    Last edited by VirtualAce; 04-09-2011 at 09:57 PM.

  4. #4
    INSANE INSIDE ekosix's Avatar
    Join Date
    May 2010
    Location
    Rio de Janeiro, Brazil
    Posts
    44
    idelovski, I intend to use both.
    ...
    VirtualAce, I tested your code with my own DLL. I did exactly as in your code (with necessary changes, according to my DLL), but it's not working.
    At least, now I can understand better how functions are loaded from explicit linking.

    I'll keep researching...
    If I find something that works I'll post here!

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    What part of my code is not working? Could you show me how you are using it?

  6. #6
    INSANE INSIDE ekosix's Avatar
    Join Date
    May 2010
    Location
    Rio de Janeiro, Brazil
    Posts
    44
    VirtualAce, yes of course:
    [Executable]
    Code:
    #include <iostream>
    #include <windows.h>
    
    typedef int (*TheFunc)(unsigned int);
    
    int main()
    {
       TheFunc _TheFunc;
       HMODULE Module = LoadLibrary(LPCWSTR("C:\\RedQueen.dll"));
    
       if (Module)
       {
          _TheFunc = (TheFunc) GetProcAddress(Module, "BeepMe");
    
          if (_TheFunc)
          {
    		  std::cout << "PROCESSING... " << std::endl;
    		  _TheFunc(0xFFFFFFFF);
          }
    
          FreeLibrary(Module);
       }
       else
       {
          std::cout << "DLL Failed To Load!" << std::endl;
       }
    
       std::cin.get();
    
       return 0;
    }
    You can see that in this line...
    Code:
    HMODULE Module = LoadLibrary(LPCWSTR("C:\\RedQueen.dll"));
    ... I used LPCWSTR because VC++ is rejecting without it.

    [DLL import/export header - Import_Export.h]
    Code:
    #pragma once
    
    #ifdef REDQUEEN_EXPORTS
    #define REDQUEEN_API __declspec(dllexport)
    #else
    #define REDQUEEM_API __declspec(dllimport)
    #endif
    [DLL module header]
    Code:
    #include "Import_Export.h"
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    	REDQUEEN_API void BeepMe(unsigned int);
    
    #ifdef __cplusplus
    }
    #endif
    
    #include <Windows.h>
    [DLL implementation]
    Code:
    #include "RedQueen.h"
    
    void BeepMe(unsigned int Hex)
    {
    	MessageBeep(Hex);
    }
    ...
    I used MessageBeep() from WinAPI. MessageBeep Function (Windows)
    I'm using it just for simple tests.

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Do you have a line defining REDQUEEN_EXPORTS in your DLL?
    [ie #define REDQUEEN_EXPORTS]

    So that the Import_Export.h header defines the functions as 'exported' from the DLL and the app defines them as 'Imported'.

    Ie this code fires correctly in both the DLL (exports) and the app (imports).

    Code:
    #pragma once
    
    #ifdef REDQUEEN_EXPORTS
    #define REDQUEEN_API __declspec(dllexport)
    #else
    #define REDQUEEM_API __declspec(dllimport) //is the 'M' instead of a 'N' a typo?
    #endif
    Last edited by novacain; 04-12-2011 at 03:30 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    ... I used LPCWSTR because VC++ is rejecting without it.
    This is because you have selected use unicode character set in your project settings. You can also set it to multi-byte or off.
    ekosix likes this.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    GetProcAddress() has absolutely zero to do with parameter passing. It just looks something up by name and hands you a pointer. It's up to you to cast this pointer to an appropriate function pointer type (specifying the correct calling convention, parameters, and all that jazz), and then you can call through the function pointer passing the correct arguments. That's what VirtualAce is trying to show.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    HMODULE Module = LoadLibrary(LPCWSTR("C:\\RedQueen.dll"));
    Your typecast is wrong...
    Code:
    HMODULE Module = LoadLibrary((LPCWSTR)"c:\\RedQueen.dll");
    if you're in C or C++ what you most likely need is the unicode version of the string literal...
    Code:
    HMODULE Module = LoadLibrary(L"c:\\RedQueen.dll");
    I do wonder why you need GetProcAddress() in this case...
    If you have the DLL loaded and have used #include to read in the header with the function prototype you should be able to call it like any other function...
    Code:
    BeepMe(50);
    That's the whole point of exporting the functions from the DLL... so you can use them like any other.

    For example the GetProcAddress() function is actually in kernel32.dll ... but you don't have to do anything special to use it other than including the header and linking with the library.
    ekosix likes this.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,257
    Quote Originally Posted by CommonTater View Post
    Code:
    HMODULE Module = LoadLibrary(LPCWSTR("C:\\RedQueen.dll"));
    Your typecast is wrong...
    Code:
    HMODULE Module = LoadLibrary((LPCWSTR)"c:\\RedQueen.dll");
    Functional style type casts are perfectly fine in C++. The real issue (as you pointed out), is that he is casting a multibyte string to a wide character string.
    bit∙hub [bit-huhb] n. A source and destination for information.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bithub View Post
    Functional style type casts are perfectly fine in C++. The real issue (as you pointed out), is that he is casting a multibyte string to a wide character string.
    Yep, he's likely to end up with something like (LPCWSTR)L"string text"

    That L prefix or _TEXT() wrapper, makes all the difference.

    When I first started out with Unicode --aside from a 5 Excedrin headache-- I learned the hard way that Pelles C has quite the sense of humour to it...

    I tried typecasting to wide string like our friend did and promptly received the dreaded error message: "More than 100 errors, try to improve yourself" ... and just about fell off my chair laughing.
    Salem and ekosix like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HOW TO Find right names for GetProcAddress
    By albert3721 in forum Windows Programming
    Replies: 15
    Last Post: 03-16-2010, 08:55 AM
  2. GetProcAddress of string
    By JaWiB in forum Windows Programming
    Replies: 5
    Last Post: 11-12-2005, 06:41 PM
  3. GetProcAddress problem
    By froque in forum C++ Programming
    Replies: 5
    Last Post: 07-29-2003, 03:11 AM
  4. Speed of GetProcAddress
    By Gherkin in forum Windows Programming
    Replies: 1
    Last Post: 03-07-2002, 05:04 PM
  5. Kernel32.dll, loadlibary() and GetProcAddress()
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 12-03-2001, 07:33 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21