Vecihi
Vecihi

Reputation: 261

Wrapping a C++ interface in C

I have a C++ application which implemented as a dll and it is running succesfuly with C++ applications. Now I’m trying to wrap my C++ interface in C to be able to use my app with C clients as well.

I’ve written one of the methods of my C++ interface (“Writer.hpp”) that i want to call in C. In C i’ve written a C_Writer.h and C_Writer.c for wrapping.

C_Writer.h includes the pointer to C++ type (structs) definitions and the method that i want to call in C.

In C_Writer.c, i’ve included Writer.hpp (C++ interface) and C_Writer.h and i’ve connected the C pointers to C++ types with C++ types, also declared the method.

Now what i’m trying to do is to call this C method in a simple C console application (CInterfaceTest) to test if it works.

But at the line “dataTimeStamp -> timeStamp.date.Day = 10;” it gives the error “1>.\CInterfaceTest.cpp(22) : error C2027: use of undefined type ' Timestamp_Alias'”.

Do you have an idea what i’m missing?

Here is the part of the code:

C++ interface

Writer.hpp
virtual Status write(DataBlock sampleData, TimeStamp dataTimestamp) = 0;

C interface

C_Writer.h
#ifndef __C_WRITER_H__
#define __C_WRITER_H__

#ifdef __cplusplus
extern "C"
{
#endif

/*----------------------------------------------------*/

#if defined (WRITER_DLL_EXPORT)
    #define WRITER_DLL __declspec(dllexport)
#elif defined (WRITER_DLL_IMPORT)
    #define WRITER_DLL __declspec(dllimport)
#else 
    #define WRITER_DLL
#endif

//TYPEDEFS:
struct Status_Alias;
typedef struct Status_Alias* ptrToStatusType;
struct Timestamp_Alias;
typedef struct Timestamp_Alias* ptrToTimestampType;
struct DataBlock_Alias;
typedef struct DataBlock_Alias* ptrToDataBlockType;
//C-FUNCTIONS
I_WRITER_DLL extern
ptrToStatusType write_c (ptrToDataBlockType sampleData, ptrToTimestampType dataTimestamp);
#ifdef __cplusplus
}
#endif

#endif //__C_WRITER_H__

Implementation File:

C_Writer.c

#include "Writer.hpp"
#include "C_Writer.h" 

struct Status_Alias
{
    Status status;
};

struct Timestamp_Alias
{
    Time_Stamp timeStamp;
};

struct DataBlock_Alias
{
    DataBlock sampleBlock;
};
ptrToStatusType write_c (ptrToDataBlockType sampleData, ptrToTimestampType dataTimestamp)
{
  Status_Alias* alias = new Status_Alias ();
  alias->status = GetWriterInterface()->write(sampleData->sampleBlock,dataTimestamp->timeStamp);

  return alias;
}

Simple console application to test c interface.

//testing C interface
// CInterfaceTest.cpp : main project file.

#include "C_Writer.h"

static void Main(string[] args)
{
    ptrToStatusType statusCont;
    ptrToDataBlockType datablock;
    ptrToTimestampType dataTimeStamp; //10-10-2012 10:20:25

    dataTimeStamp -> timeStamp.date.Day = 10;
    dataTimeStamp -> timeStamp.date.Month = 10;
    dataTimeStamp -> timeStamp.date.Year = 2012;
    dataTimeStamp -> timeStamp.time.Hours = 10;
    dataTimeStamp -> timeStamp.time.Minutes = 20;
    dataTimeStamp -> timeStamp.time.Seconds = 25;

    statusCont = write_c(datablock, dataTimeStamp);

      return;
}

NOTE: I'm working with Visual Studio 2008 ide, in Win7 x64 machine.

Upvotes: 2

Views: 179

Answers (1)

alphashooter
alphashooter

Reputation: 314

You didn't define your structures in C_Writer.h file. You cannot access to a structure members if they are not declared. Also you included C++ header into the C source file. You should compile your wrapper with your dll, not with a C project.

Actually, if you want to wrap C++ classes for C, there is no way to access its members directly. For example, in such purposes, you can use setters/getters declared as extern functions to access them:

// C_Writer.h
I_WRITER_DLL extern void setDateDay(ptrToTimestampType ptr, int value);
I_WRITER_DLL extern int getDateDay(prtToTimestampType ptr);

Implement them into C_Writer.cpp and compile with your dll. This is the easiest way I see.

Upvotes: 2

Related Questions