如何围绕 C++ 代码编写 C 包装器以公开类方法 [英] how to write c wrapper around c++ code to expose class methods
本文介绍了如何围绕 C++ 代码编写 C 包装器以公开类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在非托管 C++ 中有许多 cpp 文件,我想使用 P\Invoke 从 vb .net 访问这些文件的类方法,为此我编写了 C 包装器来公开类方法.任何人都可以帮助我,如何围绕 C++ 代码编写 ac 包装器.我正在复制我的文件的一些代码,请帮助我为这些函数编写 c 包装器.
I have number of cpp files in unmanaged c++ , I want to access class methods of these files from vb .net using P\Invoke, For that i have write C wrapper for exposing class methods.Can anybody help me ,how to write a c wrapper around C++ code. I am copying some code of my files ,please help me writting c wrapper for these function.
#include "StdAfx.h"
#include "Verify.h"
Verify::Verify(void)
:_verified(false)
{
}
Verify::~Verify(void)
{
}
void Verify::SetVerified(bool value)
{
_verified = value;
}
bool Verify::GetVerified(void) const
{
_verified;
}
void Verify::SetFailurePoint(std::basic_string<TCHAR> const & value)
{
_failurePoint = value;
}
std::basic_string<TCHAR> const & Verify::GetFailurePoint(void) const
{
return _failurePoint;
}
推荐答案
在 wrapper.h 中:
In wrapper.h:
typedef void * VERIFY_HANDLE;
extern VERIFY_HANDLE Verify_Create();
extern void VERIFY_SetVerified(VERIFY_HANDLE, bool);
extern bool VERIFY_GetVerified(VERIFY_HANDLE);
/* etc, etc */
在 wrapper.c 中:
In wrapper.c:
#include "wrapper.h"
#include "Verify.h"
VERIFY_HANDLE Verify_Create() { return (VERIFY_HANDLE) new Verify(); }
void SetVerified(VERIFY_HANDLE h, bool b) { ((Verify *)h)->SetVerified(b); }
bool GetVerified(VERIFY_HANDLE h) { return ((Verify *)h)->GetVerified(); }
这篇关于如何围绕 C++ 代码编写 C 包装器以公开类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文