How to export / import a C-structure from a DLL / into a console application using __declspec (dllexport / import)

This is my first experience with a DLL. After the MSDN documentation, I created the fooExports.h header file with macros defined according to the preprocessor definition:

#ifdef FOODLL_EXPORTS
    #define FOO_API __declspec( dllexport )
#else
    #define FOO_API __declspec( dllimport )

My intention was to use this header both in my DLL implementation and in the console application. So far, the import and export functions work very well. The problem arises when I try to export an already defined structure that I need as a parameter for one of the exported functions. For example, in the above header file, I declare FOO_API void foo( FooParams *args ), and argsis a structure defined as follows:

typedef struct FooParams
{
    char *a;
    char *b;
    void *whatever; //some other type
} FooParams;

foo.h, fooExports.h. , (, , / fooExports.h). ? DLL - C, , .

+5
1

, - FooParams, - , DLL-, DLL, "": Put

typedef struct FooParams FooParams;

fooExports.h. FOO_API . , :

  • FooParams ( FooParams * ptr = NULL; ).
  • - FooParams.
  • sizeof(FooParams) - , , malloc FooParams.

#define, , . , DLL "" "factory", , -

FOO_API FooParams* CreateFooParams(const char * input);

,

FOO_API void DestroyFooParams(FooParams * p);

, { free(p); }, , , DLL, ( Windows malloc free).

, - #include struct . - FooParams, , , , FooParams. ( ) #include -d, DLL.

+9

All Articles