如何从C#调用C ++/CLI? [英] How do I call C++/CLI from C#?

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

问题描述

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

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?

我已经看到一些有关使托管C ++包装器类与之交互的评论,但我不知道从哪里开始.我也不知道该如何与其他所有代码一起进行编译.我真的找不到关于此的教程,谷歌在托管C ++上显示的内容似乎并没有帮助.

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.

编辑尝试了m3rLinEz解决方案,但它给了我BadImageFormatException,我认为这是因为未生成DLL.我做了一切,不知道发生了什么.有什么想法吗?

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?

推荐答案

您看过C ++/CLI吗?

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

让我举一个非常简短的例子.这是Visual C ++-> CLR->类库项目的源文件.它基本上获得Windows用户名并返回它.

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.

请注意,为了进行编译,您必须进入项目设置并将其他依赖项"标记为从父项继承",因为我们正在使用这些Windows库(kernel32.lib,user32.lib 、. )

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!");
            }
        }
    };
}

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

Now created a new C# project and add reference to our first C++/CLI Class Library 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:

你好m3rlinez!

Hello m3rlinez !

C ++/CLI基本上是对C ++标准的托管扩展.它使您可以在C ++/CLI项目中利用CLR类和数据类型,也可以将其公开给托管语言.您可以使用此方法为旧的C ++库创建托管包装.有一些奇怪的语法,例如String^来定义CLR字符串的引用类型.我发现快速C ++/CLI-不到10分钟即可学习C ++/CLI" 在这里有用.

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 ++/CLI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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