如何使用C ++ / CLI在C#应用程序 [英] How to Use C++/CLI Within C# Application
问题描述
我想从我的C#应用程序调用我的C ++库(通过C ++ / CLI)。我跟着从这个问题(我的具体应用的例子)。我的应用程序的设置是:
I am trying to call my C++ library from my C# application (via C++/CLI). I followed the example from this question (for my specific application). The setup of my application is:
- PROJECT1:C ++项目(我编这一个DLL)
- Project2的:C ++项目(我的CLR包装,只需按照上面的例子中,头文件;参考PROJECT1)
- 项目3:C#项目(引用Project2的)
不幸的是,当我真正去访问CLR包装对象在我的C#应用程序时,我收到以下错误:
Unfortunately, when I actually go to access the CLR wrapper object in my C# application, I receive the following error:
的类型或命名空间名称YourClass 找不到(是否缺少 使用指令或程序集 参考?)
The type or namespace name 'YourClass' could not be found (are you missing a using directive or an assembly reference?)
我是否有项目设置不正确,或者是有别的东西,我应该寻找到? (不幸的是,我不能发布的code专有的原因,但它是code非常简单一点,轻松地遵循上面的例子。)
Do I have the project setup incorrectly, or is there something else I should be looking into? (Unfortunately, I cannot post the code for proprietary reasons, but it is a very simple bit of code and easily follows the above example.)
更新:
所以,我也正是克里斯说的去做(见回答以下),但我仍然接受来自我的C#应用程序的信息是类型或命名空间名称MyProgram'找不到(是否缺少using指令或程序集引用?),这是一个(实物模型)我的code。
So I did exactly what Chris said to do (see answer below), but I am still receiving a message from my C# application that "The type or namespace name 'MyProgram' could not be found (are you missing a using directive or an assembly reference?). Here is a (mock-up) of my code.
- PROJECT1 - 这是我的C ++应用程序。它编译/工作。我在其他地方使用它。 (我得到一个DLL出这个版本的。)
- 在Project2的 - 这是我的$ C $下我的包装 。
MyWrapper.h 的
#pragma once
#include "myorigapp.h"
using namespace System;
namespace MyProgram
{
public ref class MyWrapper
{
private:
myorigapp* NativePtr;
public:
MyWrapper()
{
NativePtr = new myorigapp();
}
~MyWrapper()
{
delete NativePtr;
NativePtr = NULL;
}
void dostuff()
{
NativePtr->dostuff();
}
}
}
- 项目3 - 这是我的C#应用程序
的Program.cs 的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyProgram;
namespace Testing
{
class Program
{
static void Main(string[] args)
{
MyWrapper p = new MyWrapper();
p.dostuff();
}
}
}
项目3引用Project2的它引用PROJECT1。一切都建立没有错误(除非我在C#code上述的使用MyProgram
行错误)。
推荐答案
好了,好了,我现在觉得愚蠢。
Okay, well, I now feel dumb.
事实证明,这个问题我是有(这是我解决了一个几个星期前 - 只是抽时间去更新这个答案)是,我已经包含头文件(见克里斯的答案是),但我就没'牛逼实际上包括CPP文件(里面是空的以外,包括头文件)。
It turns out that the problem I was having (which I solved a couple weeks ago - just got around to updating this answer) was that I had included the header file (see Chris' answer for that), but I hadn't actually included the CPP file (which is empty other than including the header file).
在我这样做,该DLL编译正确,我可以调用C ++函数从我的C#code(使用C ++ / CLI)。
Once I did this, the DLL compiled correctly and I could call the C++ functions (using C++/CLI) from my C# code.
这篇关于如何使用C ++ / CLI在C#应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!