Jump to content
  • Advertisement
Sign in to follow this  

How to export C-struct and typedefs from C++ dll ?

This topic is 1749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

 

I have a C souces that i want to wrap and export from dll library. For example:

 

C_lang_struct_and_enum.h:

typedef struct
{
  int x;
  double y;
} AStruct;

typedef enum
{
  a,
  b,
  c
} AnEnum;

I also have a few  C - functions using them:

C_lang_functions.h:

void PrintAStruct(AStruct s,AnEnum value)
{
 /*print stuff*/
}

I want to export those functions with wrappers:

CppDLL.cpp:

namespace SomeNamespace
{
   void wrapPrintAStruct(wrapAStruct s,wrapAnEnum value) //wrapper
  {
      PrintAStruct(s,value);
  }
}

CppDLL.h:

#ifdef CPP_DLL_EXPORTS
#define CPP_DLL_API __declspec(dllexport)
#else
#define CPP_DLL_API __declspec(dllimport)
#endif

namespace SomeNamespace
{
   typedef AStruct CPP_DLL_API wrapAStruct;
   typedef AnEnum CPP_DLL_API wrapAnEnum;
   CPP_DLL_API void wrapPrintAStruct(AStruct s,AnEnum value);
}

The DLL library is built succesfully, but I cannot use those symbols in a program as they seem to be undefined. How to workaround this? Any solution would be fine really.

Edited by Misery

Share this post


Link to post
Share on other sites
Advertisement

Types are compile time, it doesn't make sense to load them dynamically at runtime from a DLL.

 

#ifdef CPP_DLL_EXPORTS
#define CPP_DLL_API __declspec(dllexport)
#else
#define CPP_DLL_API __declspec(dllimport)
#endif
 
#include "C_lang_struct_and_enum.h"

namespace SomeNamespace
{
   CPP_DLL_API void wrapPrintAStruct(AStruct s,AnEnum value);
}

Share this post


Link to post
Share on other sites

 

Types are compile time, it doesn't make sense to load them dynamically at runtime from a DLL.

#ifdef CPP_DLL_EXPORTS
#define CPP_DLL_API __declspec(dllexport)
#else
#define CPP_DLL_API __declspec(dllimport)
#endif
 
#include "C_lang_struct_and_enum.h"

namespace SomeNamespace
{
   CPP_DLL_API void wrapPrintAStruct(AStruct s,AnEnum value);
}

But how to make those definitions remain in SomeNamespace?

Share this post


Link to post
Share on other sites

If you need to wrap them too, then just add the typedefs again.

#ifdef CPP_DLL_EXPORTS
#define CPP_DLL_API __declspec(dllexport)
#else
#define CPP_DLL_API __declspec(dllimport)
#endif
 
#include"C_lang_struct_and_enum.h"

namespace SomeNamespace
{
   typedef AStruct wrapAStruct;
   typedef AnEnum wrapAnEnum;
   CPP_DLL_API void wrapPrintAStruct(AStruct s,AnEnum value);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!