Thread: How to define function pointer

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    50

    How to define function pointer

    Look:
    I write codes in file DLL(name dll1.dll),
    Code:
    extern "C" _declspec(dllexport) int __stdcall ret()
    {
            return 100;
    }
    and then I want to use this function:
    Code:
    typedef int (__stdcall FUN)();
            FUN *fun=NULL;
    
            HMODULE hmod=LoadLibrary("dll1.dll");
            if(NULL==hmod)
            {
                    printf("error load\n");
                    return -1;
            }
            fun=(FUN*)GetProcAddress(hmod,"ret");
            if(NULL==fun)
            {
                    printf("error GetProcAddress:%d\n",GetLastError());
                    return -1;
            }
    but error number of function GetProcAddress is 127,
    I want to know How to correct?

  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
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    GetProcAddress Function (Windows)
    Return Value

    If the function succeeds, the return value is the address of the exported function or variable.
    return 100; is meaningless anyway.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by leetow2003 View Post
    but error number of function GetProcAddress is 127,
    I want to know How to correct?
    System Error Codes (0-499) (Windows)
    Quote Originally Posted by MSDN
    ERROR_PROC_NOT_FOUND
    127 (0x7F)
    The specified procedure could not be found.
    Use the right export name. (hint: dumpbin /exports)

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You might also want to check that you are loading the right library. The names "dll1.dll" and "dlll.dll" look a lot alike in code (one reason why some style guides actively discourage using names containing the letter l and the digit 1). If you are loading a wrong dll, it will probably not export the function you want to call.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    While we're talking about error messages, OP should look at FormatMessage Function (Windows)

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by leetow2003 View Post
    Look:
    I write codes in file DLL(name dll1.dll),
    Code:
    extern "C" _declspec(dllexport) int __stdcall ret()
    {
            return 100;
    }
    and then I want to use this function:
    Code:
    typedef int (__stdcall FUN)();
            FUN *fun=NULL;
    
            HMODULE hmod=LoadLibrary("dll1.dll");
            if(NULL==hmod)
            {
                    printf("error load\n");
                    return -1;
            }
            fun=(FUN*)GetProcAddress(hmod,"ret");
            if(NULL==fun)
            {
                    printf("error GetProcAddress:%d\n",GetLastError());
                    return -1;
            }
    but error number of function GetProcAddress is 127,
    I want to know How to correct?
    First read this... Dynamic-Link Libraries (Windows) ... yes, ALL of it.

    Now create a proper DLL entry point with DLLMain().
    Export your functions using DLLEXPORT tags.
    Write an include file (.h) for the DLL's functions using DLLIMPORT tags.
    Compile the DLL as a separate project.

    Include the DLL's .h file in your main code.
    Call LoadLibrary() and check it's return value to be sure it loaded.
    Use your functions just like they were local in your .c file.

    You do not need GetProcAddress() to use functions in Windows DLLs.
    Last edited by CommonTater; 08-19-2011 at 08:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #define function
    By begin in forum C Programming
    Replies: 14
    Last Post: 06-18-2010, 03:35 PM
  2. #define w/ function
    By xwielder in forum C Programming
    Replies: 7
    Last Post: 11-10-2009, 03:18 PM
  3. Need for function define
    By enet in forum C++ Programming
    Replies: 3
    Last Post: 08-23-2008, 05:34 PM
  4. How can I define and pass a pointer to a vector object?
    By asmileguo in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2006, 11:19 AM
  5. How do i define this function?
    By misplaced in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2005, 01:38 AM