我想從C++CLR
調用C#
類。我也創造了相同的包裝。假設在C#
類庫:從C++調用C#CLR
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 :'Wrapper :: TestDicomImage':__declspec(dllexport)不能應用於具有__clrcall ca的函數灌裝慣例
2)錯誤C3395: 'IWrapper :: TestDicomImage':__declspec(DLLEXPORT)不能被施加到函數與__clrcall調用約定
3)錯誤C2259: '包裝':不能實例化抽象類
如何解決這些錯誤?請爲我提供解決方案。
添加到C#類庫的引用。 –
你應該投票並接受一個答案,如果它是你正在尋找的那個,堆棧溢出保持清理'未回答'的問題。 +1問題 –