Dynamically linking libraries

This is a discussion on Dynamically linking libraries within the Windows Programming forums, part of the Platform Specific Boards category; I've searched and read. Most of everything was repetetive and I've learned this much: Code: typedef VOID (*MYPROC)(LPTSTR); // <- ...

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    Dynamically linking libraries

    I've searched and read. Most of everything was repetetive and I've learned this much:

    Code:
    typedef VOID (*MYPROC)(LPTSTR); // <- Pt. 1
    MYPROC Function;
    HINSTANCE hInstLib;
    hInstLib = LoadLibrary("whatever.dll");
    if (hInstLib != NULL)
    {
        Function = (MYPROC)GetProcAddress(hInstLib, "functionname");
        if (Function == NULL)
            return 0;
    }
    
    Function("string?");
    
    FreeLibrary(hInstLib);
    Now, where I've labeled Pt. 1, I have a few questions. I'm just guessing here, but it looks like we're defining MYPROC to be similar to a function. Such that the VOID means the function in the dll returns nothing, and the LPTSTR is a parameter. What about more parameters? (I've never used typedef before and not too sure of its capabilities)
    Code:
    typedef VOID (*MYPROC)(LPTSTR)(INT iSize);
    //or
    typedef VOID (*MYPROC)(LPTSTR, INT iSize);
    //or can you only have one parameter?
    More questions about the dll files. What if you don't know the names of the functions? I'm hoping there might be a way to get an array of pointers to all functions. Also, what about finding the number of parameters/parameter types of a function?

    It would seem that if I'm going to call a dll function, that I would know the name, parameters and what it does. But I have the urge to load random dll files, call functions and find out what they do. I know it's dangerous, but I'm in the mood to take a risk. Any information would be of great help.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    The typedef is a pointer to a function.

    typedef VOID (*MYPROC)(LPTSTR, int iSize);

    Kuphryn

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Kuphryn, thanks for clarifying that. It was my guess that with more than one parameter, you just list them like that.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    I don't know about the listing of dll functions, but you don't have to create a typedef for each function:
    Code:
    int (WINAPI *Func1)(int iParam1, int iParam2);//Prototype
    if(((FARPROC&)Func1= GetProcAddress(h1, "Func1")))
      //Success
    else
      //Fail

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    you would have to for each different function that doesn't have the same prototype (return value, types of parameters, number of parameters),right?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Code:
    int (WINAPI *Func1)(int iParam1, int iParam2);//Prototype
    int (WINAPI *Func2)(int iParam1);//Prototype
    (FARPROC&)Func1= GetProcAddress(h1, "Func1");
    (FARPROC&)Func2= GetProcAddress(h1, "Func2");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linking to libraries
    By davbeck in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 05:56 PM
  2. Help with linking to other c++ libraries via .net
    By pipercubusa in forum C++ Programming
    Replies: 0
    Last Post: 05-02-2005, 02:08 PM
  3. Linking and Libraries
    By krygen in forum C++ Programming
    Replies: 14
    Last Post: 12-26-2004, 03:01 PM
  4. Two questions (Linking Libraries and Creating Them)
    By Thantos in forum Linux Programming
    Replies: 3
    Last Post: 03-21-2004, 05:01 PM
  5. Linking .h files to libraries
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2001, 09:20 PM

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