C++ 包装一个C++;C中的接口

C++ 包装一个C++;C中的接口,c++,c,pointers,visual-studio-2008,interface,C++,C,Pointers,Visual Studio 2008,Interface,我有一个C++应用程序,它以dll的形式实现,并与C++应用程序一起成功运行。 现在我正在尝试将我的C++界面包装在C中,以便能够将我的应用程序与C客户端一起使用 我已经编写了我的C++接口(“Writer.hpp”)的一个方法,我想在C中调用它。在C中,我编写了一个C_Writer.h和C_Writer.C用于包装 C\u Writer.h包括指向C++type(structs)定义的指针和我想在C中调用的方法 在CúWriter.C中,我包括了Writer.hpp(C++接口)和CúWrit

我有一个
C++
应用程序,它以
dll
的形式实现,并与
C++
应用程序一起成功运行。 现在我正在尝试将我的
C++
界面包装在
C
中,以便能够将我的应用程序与
C
客户端一起使用

我已经编写了我的
C++
接口(
“Writer.hpp”
)的一个方法,我想在
C
中调用它。在
C
中,我编写了一个
C_Writer.h
C_Writer.C
用于包装

C\u Writer.h
包括指向
C++
type(structs)定义的指针和我想在
C
中调用的方法

CúWriter.C
中,我包括了
Writer.hpp
(C++接口)和
CúWriter.h
,并且我将
C
指向
C++
类型的指针与
C++
类型连接起来,还声明了该方法

现在我要做的是在一个简单的
C
控制台应用程序(
CInterfaceTest
)中调用这个
C
方法来测试它是否有效

但是在第
“dataTimeStamp->timeStamp.date.Day=10;”“
行给出了错误
“1>\CInterfaceTest.cpp(22):错误C2027:使用未定义的类型“timeStamp\u Alias”

你知道我错过了什么吗

以下是代码的一部分:

C++接口

Writer.hpp
virtual Status write(DataBlock sampleData, TimeStamp dataTimestamp) = 0;
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__
C接口

Writer.hpp
virtual Status write(DataBlock sampleData, TimeStamp dataTimestamp) = 0;
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__
实施文件:

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;
}
用于测试c接口的简单控制台应用程序

//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;
}

注意:我使用的是Win7 x64机器上的Visual Studio 2008 ide。

您没有在
C\u Writer.h
文件中定义结构。如果未声明结构成员,则无法访问它们。此外,你还把C++头包含在C源文件中。您应该使用dll而不是C项目来编译包装器

实际上,如果您想为C包C++类,则无法直接访问它的成员。例如,在这种情况下,可以使用声明为外部函数的setter/getter来访问它们:

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

将它们实现到
C_Writer.cpp
中,并使用dll进行编译。这是我所看到的最简单的方法。< /P>这行>代码>静态空格main(String)[/GARS] < /Cord>。你怎么能在C文件中包含一个C++头?我在使用C++库的C项目中遇到了完全相同的问题。我发现这是最简单明了的方法。