Search code examples
c++winapidllexport

Win32 DLL Export Function Parameter Best Practice


I'm writing a dll that will export functions and classes. But for now I have a question about exporting functions.

If my function signature looks like:

__declspec(dllexport) int fn1(FILE *theFile);

Should I include so it defines FILE? What is the best practice here? And if I want to export a function that takes or returns a custom type, should the header file for those types be included in the exported header file as well?


Solution

  • Don't forward-decl standard library types and functions. That is what the library headers are provided for. Your file should be laid out as:

    #ifndef MYLIBRARY_H
    #define MYLIBRARY_H
    
    #ifdef  MYLIBRARY_EXPORTS
    #define MYLIBRARY_EXPORT    __declspec(dllexport)
    #else
    #define MYLIBRARY_EXPORT    __declspec(dllimport)
    #endif
    
    // required headers
    #include <stdio.h>
    
    // exported/imported functions
    MYLIBRARY_EXPORT int fn1(FILE *theFile);
    
    #endif
    

    Your DLL project is built with MYLIBRARY_EXPORTS defined as part or the defined preprocessor macros (configurable any number of ways), and consumers of your DLL do not define it.

    This is the common layout MS uses for their wizard canned DLL project generators. Consider doing the same in your projects as well. Above all, include the required standard headers for your library to properly define what it needs coming in, and what it provides going out.