Thread: First Windows DLL

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    First Windows DLL

    I'm using Open Watcom IDE. I have been following all the examples/tutorials I have found for creating a Windows DLL. I have very little experience of C programming, could anyone please give me a pointer to what I am doing wrong here?

    I have a Hello.c file in my project as:-

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <windows.h>
    
    extern "C"
    {
        __declspec(dllexport) double GetNumberFromDLL()
        {
            return 50;
        }
    }
    When I compile I get lots of errors, the first few being:-

    "Invalid Declarator" error on the extern
    Expecting ';' but found 'C'
    Expecting data or function declaration, but found 'string'

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    extern"C" is a C++ thing. You're trying to put it in a C file. The usual pattern is this:

    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    // whatever code...
    
    #ifdef __cplusplus
    } // extern "C"
    #endif
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    2
    Quote Originally Posted by brewbuck View Post
    extern"C" is a C++ thing. You're trying to put it in a C file. The usual pattern is this:

    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    // whatever code...
    
    #ifdef __cplusplus
    } // extern "C"
    #endif
    Thanks for your very prompt reply.

    I thought the extern was needed to stop mangling. It complies fine without the extern but then in my calling application I get error "Unable to find an entry point named 'GetNumberFromDLL'".

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by g_hickley View Post
    Thanks for your very prompt reply.

    I thought the extern was needed to stop mangling. It complies fine without the extern but then in my calling application I get error "Unable to find an entry point named 'GetNumberFromDLL'".
    It is needed. I guess I wasn't clear enough in my post. The problem is that your file is *.c, not *.cpp, so it is being compile as C instead of C++. Rename it to .cpp
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    1. DLLs should always have a default entry point (e.g. C has Main(), Windows GUI have WinMain). DLLs have DllMain(). The following compiles in C.
    Code:
    BOOL APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID reserved)
    {
        return TRUE;
    }
    2. Try:
    Code:
    DLLIMPORT void HelloWorld(void)
    {
        MessageBox (0, "Hello World from DLL!\n", "Hi", MB_ICONINFORMATION);
    }
    w/ the header files as:
    Code:
    #include <windows.h>
    
    #ifndef _DLL_H_
    #define _DLL_H_
    
    #if BUILDING_DLL
    # define DLLIMPORT __declspec (dllexport)
    #else /* Not BUILDING_DLL */
    # define DLLIMPORT __declspec (dllimport)
    #endif /* Not BUILDING_DLL */
    
    BOOL APIENTRY DllMain (HINSTANCE, DWORD, LPVOID);
    DLLIMPORT void HelloWorld(void);
    
    #endif /* _DLL_H_ */
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. Retrieving Data from DLL
    By JJFMJR in forum C++ Programming
    Replies: 11
    Last Post: 08-18-2008, 07:44 AM
  3. Error C2664 - Trying to call an external Dll
    By jamez05 in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2006, 06:07 AM
  4. Codec Bitrates?
    By gvector1 in forum C# Programming
    Replies: 2
    Last Post: 06-16-2003, 08:39 AM