我想从 C++CLR
调用 C#
类。我还为此创建了包装器。假设在 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)error C3395: 'Wrapper::TestDicomImage' : __declspec(dllexport) 不能应用于具有 __clrcall 调用约定的函数
2)error C3395: 'IWrapper::TestDicomImage' : __declspec(dllexport) 不能应用于具有 __clrcall 调用约定的函数
3)error C2259: 'Wrapper': 无法实例化抽象类
如何解决这些错误?请提供解决方案。
最佳答案
解决方案一
这个 SO 答案描述了如何使用 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
或者这个(有例子):
方案二
宿主 CLR
http://1code.codeplex.com/wikipage?title=Invoke%20.NET%20Assembly%20from%20Native%20C%2b%2b#HostCLR
方案三
将 .NET 程序集转换为 COM 服务器,并通过 .NET-COM 互操作从 C++ 调用它 http://1code.codeplex.com/wikipage?title=Invoke%20.NET%20Assembly%20from%20Native%20C%2b%2b#COMInterop
关于c# - 从 C++CLR 调用 C# 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16563810/