DLL Function / Load Library Problem

This is a discussion on DLL Function / Load Library Problem within the Windows Programming forums, part of the Platform Specific Boards category; I'm having a bit of a problem calling a function in a DLL I created. Now it works fine when ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    DLL Function / Load Library Problem

    I'm having a bit of a problem calling a function in a DLL I created. Now it works fine when I call a function which returns void and has no arguments, but when I try to call this function (the following is from the DLL):

    Code:
    extern "C" _declspec (dllexport) int AddNums (int a, int b)
    {
            return (a + b);
    }
    I get this runtime error:

    Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually the result of calling a function delcared with one calling convention with a function pointer declared witha a different calling convention.
    I can understand what's it's saying, but at the same time I can't because the function pointer... well, see for yourself. Here's the program trying to call the function:

    Code:
    #include "stdafx.h"
    
    typedef void (WINAPI* DLL_NumberList) ();
    typedef int  (WINAPI* DLL_AddNums) (int, int);
    
    DLL_NumberList  NumberList;
    DLL_AddNums     AddNums;
    
    
    int main(int argc, char **argv[])
    {
            HINSTANCE       hLib = LoadLibrary ("DLLProj.dll");
            char            cModName[_MAX_PATH];
            int             iAns = 0;
    
            if (hLib == NULL)
            {
                    std::cerr << "Unable to load library!" << std::endl;
                    getchar ();
                    return 1;
            }
            
            memset (cModName, '\0', _MAX_PATH);
            GetModuleFileName ((HMODULE) hLib, (LPSTR) cModName, _MAX_PATH);
            std::cout << "Library Loaded: " << cModName << std::endl;
    
            NumberList = (DLL_NumberList) GetProcAddress ((HMODULE) hLib, "NumberList");
            AddNums = (DLL_AddNums) GetProcAddress ((HMODULE) hLib, "AddNums");
    
            if (NumberList == NULL)
            {
                    std::cerr << "Unable to load function NumberList!" << std::endl;
                    FreeLibrary ((HMODULE) hLib);
                    getchar ();
                    return 1;
            }
    
            if (AddNums == NULL)
            {
                    std::cerr << "Unable to load function AddNums!" << std::endl;
                    FreeLibrary ((HMODULE) hLib);
                    getchar ();
                    return 1;
            }
    
            NumberList ();
            iAns = AddNums (10, 10);
            std::cout << "Answer: " << iAns << std::endl;
    
            FreeLibrary ((HMODULE) hLib);
    	return 0;
    }
    Now NumberList is loaded / called fine, I just get that runtime error when I try to call AddNums ("iAns = AddNums (10,10);").

    UPDATE: Ah strange: When I get the error dialog, if I press Ignore the answer still appears (correctly) in the console (Answer: 20), but the same ESP error persists.
    Last edited by cboard_member; 12-10-2005 at 06:14 AM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    Try to declare function with standard calling convention, like
    extern "C" _declspec (dllexport) WINAPI int AddNums (int a, int b)

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Hmm I got the following error:

    error C2144: syntax error : 'int' should be preceded by ';'
    When I added the WINAPI; here it is now:

    Code:
    extern "C" _declspec (dllexport) WINAPI int AddNums (int a, int b)
    {
            return (a + b);
    }
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Ah I forgot to include windows.h in the dll project, but now I get some warnings:

    c:\Documents and Settings\lee\Desktop\DLLProj\DLLProj.cpp(14) : warning C4518: 'int ' : storage-class or type specifier(s) unexpected here; ignored
    c:\Documents and Settings\lee\Desktop\DLLProj\DLLProj.cpp(14) : warning C4230: anachronism used : modifiers/qualifiers interspersed; qualifier ignored
    Should they present a problem?

    UPDATE: Yes, yes they ........ing do. Now it can't load the function. Here's the updated code:

    Code:
    // DLLProj.cpp
    extern "C" _declspec (dllexport) int WINAPI AddNums (int a, int b)
    {
            return (a + b);
    }
    
    // Program trying to load function
    #include "stdafx.h"
    
    typedef void (WINAPI *DLL_NumberList) ();
    typedef int  (WINAPI *DLL_AddNums) (int, int);
    
    DLL_NumberList  NumberList;
    DLL_AddNums     AddNums;
    
    
    int main(int argc, char **argv[])
    {
            HINSTANCE       hLib = LoadLibrary ("DLLProj.dll");
            char            cModName[_MAX_PATH];
            int             iAns = 0;
    
            if (hLib == NULL)
            {
                    std::cerr << "Unable to load library!" << std::endl;
                    getchar ();
                    return 1;
            }
            
            memset (cModName, '\0', _MAX_PATH);
            GetModuleFileName ((HMODULE) hLib, (LPSTR) cModName, _MAX_PATH);
            std::cout << "Library Loaded: " << cModName << std::endl;
    
            NumberList = (DLL_NumberList) GetProcAddress ((HMODULE) hLib, "NumberList");
            AddNums = (DLL_AddNums) GetProcAddress ((HMODULE) hLib, "AddNums");
    
            if (NumberList == NULL)
            {
                    std::cerr << "Unable to load function NumberList!" << std::endl;
                    FreeLibrary ((HMODULE) hLib);
                    getchar ();
                    return 1;
            }
    
            if (AddNums == NULL)
            {
                    std::cerr << "Unable to load function AddNums!" << std::endl;
                    FreeLibrary ((HMODULE) hLib);
                    getchar ();
                    return 1;
            }
    
            NumberList ();
            iAns = AddNums (10, 10);
            std::cout << "Answer: " << iAns << std::endl;
    
            FreeLibrary ((HMODULE) hLib);
    	return 0;
    }
    This is starting to really annoy me now. Each time I change something it either makes no difference or the function can't be loaded.
    Last edited by cboard_member; 12-10-2005 at 07:54 AM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Solved it. Removed WINAPI from the function pointer declaration and it all works fine (I also removed WINAPI from the DLL function declaration).
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Change:
    Code:
    extern "C" _declspec (dllexport) int WINAPI AddNums (int a, int b)
    {
            return (a + b);
    }
    to:
    Code:
    // DLLProj.cpp
    extern "C" _declspec (dllexport)int AddNums (int a, int b)
    {
            return (a+b);
    }
    Get rid of the WINAPI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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