Thread: problem initializing function pointer

  1. #1
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38

    problem initializing function pointer

    I am trying to create a pointer that would point to Out32 function in inpout32.dll.
    and the following code does not work, can someone give me more insight about
    why it doesnt work?
    Code:
    #include <iostream>
    #include <windows.h>
         
    int main()
    {
       HINSTANCE Dllload = LoadLibrary("inpout32.dll");
     
       if (Dllload)
       {
               
         void (*outport)(short adress, short value) = GetProcAddres(Dllload, "Out32");
         std::cout<<"success";
         std::cin.get();
       }
       else
       { 
           std::cout<<"inpout32.dll failed to load";
           std::cin.get();
       }
    }
    it loads inpout32.dll ok, but it has problem with that function pointer initialization
    compiler has this to say about my code:
    In function `int main()':
    invalid conversion from `int (*)()' to `void (*)(short int, short int)'

    but the code with typedef works,
    Code:
    #include <iostream>
    #include <windows.h>
    
    typedef void (*outportpoint)(short adress,short value);     
    int main()
    {
       HINSTANCE Dllload = LoadLibrary("inpout32.dll");
     
       if (Dllload)
       {
                outportpoint outport;
                outport = (outportpoint) GetProcAddress(Dllload, "Out32");
                std::cout<<"success";
                std::cin.get();
       }
       else
       { 
           std::cout<<"inpout32.dll failed to load";
           std::cin.get();
       }
    }
    isn't typedef used here to make creation of multiple pointers easier insted of declaring each new pointer?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well the 2nd one explicitly casts the result of GetProcAddress, so there's no warning.

    And yes, typedefs for function pointers save a hell of a lot of typing, and an excess of parentheses in rather odd places.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38
    ok, but if you try
    void (*outport)(short adress, short value) = (void)GetProcAddres(Dllload, "Out32");
    that doesn't work niether. Is there any other way other than typedef?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have to cast it to the type on the left, which is (void (*)(short, short)), not some other random type.

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Code:
       if (Dllload)
       {
               
         void (*outport)(short adress, short value) = (void (*)(short, short))GetProcAddres(Dllload, "Out32");
         std::cout<<"success";
         std::cin.get();
       }

  6. #6
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38
    Yep that did the trick!
    But this leads me to another bunch of questions.

    Doesn't GetProcAddress function give you just the adress of Out32 ?

    Why do we need to typecast if the pointer is of type void pointer (short,short) and if I understand correctly Out32 is void Out32(short,short) ?

    What is so special about GetProcAddress function?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The problem is that GetProcAddress is not special at all: functions are only allowed to return one type of thing, and it appears that MS has chosen the generic C "function returning int with no given arguments" (which a non-prototyped function is assumed to be in C). C++ is more strict about types than that, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  3. Pointer in Function problem
    By lilhawk2892 in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 02:41 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM