使用c ++调用使用c ++类的托管C#DLL. [英] using c++ to call a managed C# DLL, that uses a c++ class.

查看:62
本文介绍了使用c ++调用使用c ++类的托管C#DLL.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专家们,我正在努力实现以下目标:
我有一个已经编写的c ++类(我无法触摸),它看起来像这样:

Hey experts, i''m trying to achieve the following:
i have an already written c++ class (which i can''t touch), it looks like this:

// this is the class i want to pass.
class CCPlus
{
public:
	CCPlus();
	~CCPlus();
	void WriteToLog(char* toWrite);
	void setNumber(int num);
	void printNumber();
	int  getNumber();
	void setInternalString(char* str);
	void printInternalString();


private:
	int m_Number;
	CSTRHolder* m_holder;
};

//and the STRHolder:
class CSTRHolder
{
public:
	CSTRHolder(void);
	~CSTRHolder(void);
	void setString(char* str);
	char* getCopyOfString();
private:
	char* m_str;
};



然后,我想创建一个执行类似操作的C ++项目:



Then i want to create a c++ project that does something like that:

void main()
{
  CCPlus* p = new CCPlus();
  p->setNumber(10);

  // now I want to call a C# dll, and pass the "p" pointer.
  // something like:
  int result =  MyManagedClass::DoOperation(&p);
  // where MyManagedClass is below:
}



可以说我在C#中有这样的内容:



lets say i have in C# something like this:

namespace ManagedDotnet
{
    public class MyManagedClass
    {
        public static int DoOperation(CCPlusWrapper p)
        {
             Console.Writeline(p.getNumber());
             p.setInternalString("Hello");
             p.printInternalString();
             return 0;
        }
    }
}




我已经在c ++/CLI中创建了一个项目,该项目想包装非托管类:




i''ve created a project in c++/CLI that suppose to wrap the unmanaged class:

// MyBridge.h

#pragma once

using namespace System;


class CCPlus;

public ref class Bridger
{
public:
	Bridger(CCPlus* myUnmanaged);
	~Bridger();
	void WriteToLog(String^ toWrite);
	void setNumber(int num);
	void printNumber();
	int  getNumber();
	void setInternalString(String^ str);
	void printInternalString();
private:
	CCPlus* m_pUnmanaged;

};





// This is the main DLL file.

#include "stdafx.h"
#include "MyBridge.h"
#include <stdio.h>
#include "CPlus.h"
#include <vcclr.h>
#include <iostream>

using namespace std;
using namespace System;
Bridger::Bridger(CCPlus* myUnmanaged)
:m_pUnmanaged(NULL)
{	
	this->m_pUnmanaged = myUnmanaged;	
}
Bridger::~Bridger()
{

}
void Bridger::WriteToLog(String^ toWrite)
{	
   // Pin so GC won't move the managed string around
   pin_ptr<const wchar_t> ptr = PtrToStringChars(toWrite);
   m_pUnmanaged->WriteToLog((char*)static_cast<const wchar_t*>( ptr ));   
}
void Bridger::setNumber(int num)
{
	m_pUnmanaged->setNumber(num);
}
void Bridger::printNumber()
{	
	m_pUnmanaged->printNumber();
}
int  Bridger::getNumber()
{
	return m_pUnmanaged->getNumber();
}
void Bridger::setInternalString(String^ str)
{
   pin_ptr<const wchar_t> ptr = PtrToStringChars(str);
   m_pUnmanaged->setInternalString((char*)static_cast<const wchar_t*>( ptr ));   
}
void Bridger::printInternalString()
{
	m_pUnmanaged->printInternalString();
}




但是现在,我需要以某种方式在C ++中创建此包装器,传递非托管对象,然后调用另一个C#DLL,然后将该包装器传递到那里?

还是我的方向错了?
我正在尝试避免使用COM.

谢谢!




But now, i need to somehow create this wrapper in C++, pass the unmanaged object, then call another C# DLL, and pass that wrapper to there?

or my direction is wrong?
i''m trying to avoid COM by the way.

thanks!

推荐答案



我相信以下链接将对您有所帮助.我做了这样的事情.

http://support.microsoft.com/kb/828736


干杯
Hi,

I am sure the following link will help you.I have done such a thing.

http://support.microsoft.com/kb/828736


Cheers


这篇关于使用c ++调用使用c ++类的托管C#DLL.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆