我想从C#
致电C++CLR
班级。我也为它创建了包装器。假设在C#
类库中:
namespace MyImage
{
public class DicomHandler
{
public void TestImage(DicomImage dicomImage,int height,int width)
{
}
}
public class DicomImage
{
}
}
然后在Wrapper
中,我创建了DicomHandler
的对象,我需要调用TestImage(DicomImage dicomImage,int height,int width)
。
包装类库包含,
在IWrapper.h中
#pragma once
#include<windows.h>
#include <string>
using namespace MyImage;
#ifdef MANAGEDWRAPPER_EXPORTS
#define DLLAPI __declspec(dllexport)
#else
#define DLLAPI __declspec(dllimport)
#pragma comment(lib,"F:\\8May\\firebreath-FireBreath-firebreath-1.7.0-0- gdf8659e\\firebreath\\Wrapper\\Debug\\Wrapper.lib")
#endif
class IWrapper
{
public:
static IWrapper *CreateInstance();
static void Destroy(IWrapper *instance);
virtual DLLAPI void Sethandle(HWND handle)=0;
virtual DLLAPI void TestDicomImage(DicomImage^ _dicomImage,int width,int height)=0;
};
In Wrapper.h,
#pragma once
#include <windows.h>
#include <vcclr.h>
#include "IWrapper.h"
#include "stdio.h"
#include "string.h"
using namespace System;
using namespace System::Reflection;
using namespace System::IO;
using namespace MyImage;
using namespace std;
using namespace System::Runtime::InteropServices;
class Wrapper:public IWrapper
{
private:
gcroot<DicomHandler^> _dicomHandler;
//gcroot<DicomImageHandler^> _dicomImageHandler;
public:
Wrapper(){}
virtual DLLAPI void SetHandle(HWND handle);
virtual DLLAPI void TestDicomImage(DicomImage^ _dicomImage,int winwidth,int winheight);
};
在Wrapper.cpp中,
#include "stdafx.h"
#include "Wrapper.h"
#include "IWrapper.h"
#include <windows.h>
#include<iostream>
#include <winuser.h>
#include <tchar.h>
#include<vcclr.h>
#include <msclr\marshal_cppstd.h>
#include <string>
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;
using namespace System;
using namespace System::IO;
using namespace std;
using namespace DicomImage;
void Wrapper::SetHandle(HWND handle)
{
_dicomHandler=gcnew DicomHandler;
//_dicomImageHandler=gcnew DicomImageHandler;
_dicomHandler->SetHandle((System::IntPtr)handle);
}
IWrapper *IWrapper::CreateInstance()
{
IWrapper *instance =(IWrapper*)new Wrapper();
return (instance);
}
void IWrapper::Destroy(IWrapper *instance)
{
delete instance;
}
void Wrapper::TestDicomImage(DicomImage^ _dicomImage,int width,int height)
{
_dicomHandler->TestImage(_dicomImage,width,height);
}
然后它引发了三个错误,如
1)错误C3395:&#39; Wrapper :: TestDicomImage&#39; :__ declspec(dllexport)不能应用于具有__clrcall调用约定的函数
2)错误C3395:&#39; IWrapper :: TestDicomImage&#39; :__ declspec(dllexport)不能应用于具有__clrcall调用约定的函数
3)错误C2259:&#39; Wrapper&#39; :无法实例化抽象类
如何解决这些错误?请为我提供解决方案。
答案 0 :(得分:0)
解决方案1
这个答案描述了如何使用CLI从C ++调用C#类(此示例显示如何传递某些托管对象的数组):
public ref class CSharpClassName
{
//code
}
C++ Interop: How do I call a C# class from native C++, with the twist the class is non-static?
这个网站也非常有用:
http://www.functionx.com/cppcli/examples/array1.htm
或者这个(带示例):
解决方案2
主机CLR
http://1code.codeplex.com/wikipage?title=Invoke%20.NET%20Assembly%20from%20Native%20C%2b%2b#HostCLR
解决方案3
将.NET程序集转换为COM服务器,并通过.NET-COM互操作从C ++调用它 http://1code.codeplex.com/wikipage?title=Invoke%20.NET%20Assembly%20from%20Native%20C%2b%2b#COMInterop
答案 1 :(得分:0)
Here是一个从c ++调用托管代码的简单教程。我以前用过这个并且对我很好,希望它能满足你的要求。
答案 2 :(得分:0)
只需将c#dll添加到c ++ / cli项目中作为参考,然后例如:
DicomHandler^ foo = gcnew DicomHandler();
foo->TestImage(..);
我假设你已经有了
using namespace MyImage;