Need help importing a function from a DLL!

This is a discussion on Need help importing a function from a DLL! within the C++ Programming forums, part of the General Programming Boards category; Can anyone help me with this problem? Basically, I'm trying to call the function 'MessageBoxA' without including windows.h (and by ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    6

    Question Need help importing a function from a DLL!

    Can anyone help me with this problem? Basically, I'm trying to call the function 'MessageBoxA' without including windows.h (and by rather linking to 'libuser32.a'). Can this be done? Thanks in advance.

    The following gives me an error ([Linker error] undefined reference to 'MessageBoxA'):

    Code:
    extern "C" int MessageBoxA(int hWnd,const char* lpText,const char* lpCaption,unsigned int uType);
    
    int main()
    {
        MessageBoxA(0,"Test","Test",0);
        return 0;
    }
    Note: I am using MinGW as my compiler.
    Last edited by BoredCrzy; 02-29-2004 at 04:57 PM.

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    #pragma comment(lib, "libuser32.a")

    that might work

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    6
    Thanks skorman00:

    Unfortunately, however, there is still the same error

    Could it be an issue of how I compile it? I have tried it both under Dev-C++ (I included the library under 'Project Options'->'Linker') and under Command Prompt (gcc -Os -s Debug.cpp -luser32).

  4. #4
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    I've never had to extern a function, I was think that it would run fine if you put the #pragma statement and got rid of the extern statement.

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    6
    I tried removing

    Code:
    extern "C" int MessageBoxA(long hWnd,const char* lpText,const char* lpCaption,unsigned int uType);
    and am now receiving the following errors:

    [Warning] In function `int main()':
    `MessageBoxA' undeclared (first use this function)

    Any other suggestions?

    Thanks again!

    Debug.cpp ->

    Code:
    #pragma comment(lib, "libuser32.a")
    
    //extern "C" int MessageBoxA(long hWnd,const char* lpText,const char* lpCaption,unsigned int uType);
    
    int main()
    {
        MessageBoxA(0,"Test","Test",0);
        return 0;
    }

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    extern "C" int __stdcall MessageBoxA(void * hWnd, const char * lpText, const char * lpCaption, unsigned int uType);
    Why is it not found without the __stdcall:
    Name-decoration convention:
    An underscore (_) is prefixed to the name. The name is followed by the at sign (@) followed by the number of bytes (in decimal) in the argument list. Therefore, the function declared as int func( int a, double b ) is decorated as follows: _func@12
    http://msdn.microsoft.com/library/en...___stdcall.asp
    Maybe.

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    6
    Thank you so much anonytmouse! It finally works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  4. Problem in returning value from the dll exported function
    By dattaforit in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2006, 04:30 AM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM

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