如何从C#调用本机C ++? [英] How do I call native C++ from C#?

查看:136
本文介绍了如何从C#调用本机C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在C ++中实现的类,负责程序的算术计算,以及使用WPF的接口。我用C#处理输入,但是如何使用我的C ++类?



我看到一些关于使托管C ++包装类与其交互的评论,但是我不知道从哪里开始。我也不知道我将如何与所有其他代码一起编译。我不能真正找到一个教程,在谷歌显示在托管C ++的东西不真的似乎有帮助。



有什么帮助我吗?

编辑尝试m3rLinEz解决方案,但它给我一个BadImageFormatException,我认为这是因为DLL isn' t产生。我做了一切告诉,dunno发生了什么。任何想法?

解决方案

你看看C ++ / CLI吗?



让我举个简单的例子。下面是来自Visual C ++ - > CLR - >类库项目的源文件。它基本上获得Windows用户名并返回它。



请注意,为了得到这个编译,你必须进入项目设置,并将附加依赖关系标记为从父继承,因为我们使用Windows libs(kernel32.lib,user32.lib,..)

  // CSCPP.h 

#pragma once

#includewindows.h

使用命名空间System;

命名空间CSCPP {

public ref class Class1
{
// TODO:在此处添加此类的方法。
public:
String ^ GetText(){
WCHAR acUserName [100];
DWORD nUserName = sizeof(acUserName);
if(GetUserName(acUserName,& nUserName)){
String ^ name = gcnew String(acUserName);
return String :: Format(Hello {0}!,name);
} else {
return gcnew String(Error!);
}
}
};
}



现在创建了一个新的C#项目,并添加了对我们第一个C ++ / CLI类的引用利比里亚项目。然后调用实例方法。

 命名空间CSTester 
{
class Program
{
static void Main(string [] args)
{
CSCPP.Class1 instance = new CSCPP.Class1();
Console.WriteLine(instance.GetText());
}
}
}

我的机器:


Hello m3rlinez!


C ++ / CLI基本上是C ++标准的托管扩展。它允许您在C ++ / CLI项目中使用CLR类和数据类型,并将其暴露给托管语言。您可以使用此为您的旧C ++库创建一个托管包装器。有一些奇怪的语法,如 String ^ 定义CLR字符串的引用类型。我在这里找到快速C ++ / CLI - 在不到10分钟内学习C ++ / CLI


I have a class implemented in C++ that's responsible for the arithmetic computation of the program, and an interface using WPF. I process the input with C# but then how can I use my C++ class?

I've seen some comments about making a managed C++ wrapper class to interact with it, but I don't know where to start. Nor do I know how I'd go to compile it along with all the other code. I can't really find a tutorial on this, and the stuff google shows on managed C++ doesn't really seem helpful.

Anything out there to help me out? This doesn't seem unreasonable to me.

EDIT Tried m3rLinEz solution but it's giving me a BadImageFormatException, I think it's because the DLL isn't generated. I did everything as told, dunno what happened. Any ideas?

解决方案

Have you take a look at C++/CLI?

Let me give a very short example. Here is the source file from a Visual C++ -> CLR -> Class Library project. It basically get Windows username and return it.

Please note that, in order to get this compiled, you have to go into project settings and mark "Additional Dependencies" as "Inherit from parent" because we are using those Windows libs (kernel32.lib, user32.lib, ..)

// CSCPP.h

#pragma once

#include "windows.h"

using namespace System;

namespace CSCPP {

    public ref class Class1
    {
        // TODO: Add your methods for this class here.
    public:
        String^ GetText(){
            WCHAR acUserName[100];
            DWORD nUserName = sizeof(acUserName);
            if (GetUserName(acUserName, &nUserName)) {
                String^ name = gcnew String(acUserName);
                return String::Format("Hello {0} !", name);
            }else{
                return gcnew String("Error!");
            }
        }
    };
}

Now created a new C# project and add reference to our first C++/CLI Class Libary project. And then call the instance method.

namespace CSTester
{
    class Program
    {
        static void Main(string[] args)
        {
            CSCPP.Class1 instance = new CSCPP.Class1();
            Console.WriteLine(instance.GetText());
        }
    }
}

This gave the following result on my machine:

Hello m3rlinez !

C++/CLI is basically a managed extension over C++ standard. It allows you to utilize CLR classes and data types in your C++/CLI project and also expose this to managed language. You can created a managed wrapper for your old C++ library using this. There are some weird syntaxes such as String^ to define reference type to CLR String. I find "Quick C++/CLI - Learn C++/CLI in less than 10 minutes" to be useful here.

这篇关于如何从C#调用本机C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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