通过反射从C#访问C ++非成员函数 [英] Accessing a C++ non-member function from C# via reflection

查看:177
本文介绍了通过反射从C#访问C ++非成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获得一些关于C ++程序的运行时信息,这是很困难的,因为C ++不提供一些复杂的反射机制。现在,我的方法是使用/ clr编译C ++代码,并从C#中反映出最终的汇编(因为我喜欢这种语言超过C ++)。

I need to gain some run-time information about a C++ program, which is kinda difficult due to C++ not offering some sophisticated reflection mechanism. Now, my approach is to compile the C++ code using /clr and to reflect over the resulting assembly from C# (simply because I like that language more than C++).

这是所有或多或少都很好,我现在停留在一个点,我需要实际运行程序通过调用其main方法。这是一个令人沮丧的考虑我有多远我已经...

While this is all turning out more or less fine, I'm now stuck at a point where I need to actually run the program by calling its main method. Which is kind of frustrating considering how far I got already...

这是有问题的C ++程序:

This is the C++ program in question:

#include "systemc.h"
#include <iostream>
using namespace std;

// Hello_world is module name
SC_MODULE (HelloWorld) {
    SC_CTOR (HelloWorld) {
        // Nothing in constructor 
    }
    void HelloWorld::say_hello() {
        //Print "Hello World" to the console.
        cout << "Hello World.\n";
    }
};

//sc_main in top level function like in C++ main
int sc_main(int argc, char* argv[]) {
  HelloWorld hello("HELLO");
  hello.say_hello();
  string input = "";
  getline(cin, input);
  return(0);
}

没什么好奇的,真的...

Nothing fancy, really...

这是用于检查生成的程序集的C#方法:

This is the C# method used to inspect the resulting assembly:

System.Reflection.Assembly ass = System.Reflection.Assembly.LoadFrom(filename);

System.Console.WriteLine(filename + " is an assembly and has been properly loaded.");

Type[] hello = ass.GetTypes().Where(type => type.Name.ToLower().Contains("hello")).ToArray();
Type[] main = ass.GetTypes().Where(type => type.Name.ToLower().Contains("main")).ToArray();

现在,hello类型数组包含HelloWorld类(或者至少我认为那个类),主var包含三种类型,所有这些类型都处理doMAINs(即与我正在寻找的sc_main方法无关)。我假定它与它不是公开的东西有关,但声明它是一个静态的HelloWorld类的公共成员函数不工作,因为该函数期望是一个非成员函数被发现。

Now, while the hello Type-array contains the HelloWorld class (or at least I assume that it is that class), the main var contains three types, all of which deal with doMAINs (ie have nothing to do with the sc_main method I'm looking for). I assume that it has something to do with it not being public, but declaring it a static public member function of the HelloWorld class doesn't work either since the function is expected to be a non-member function to be found. Or am I just overlooking something terribly stupid?

推荐答案

不,它不是。您需要了解C ++ / CLI的工作原理 - 您不能仅仅使用/ CLR重新编译C ++程序,并使用它来完成。 sc_main 方法在这里是本地的,不是管理的,你不能反映它,对于HelloWorld类型也是如此,除非你重新定义它为 ref class ,但我怀疑你是因为你在主要实例化它的价值,这将是合法的,如果它是一个本地类。

No, it doesn't. You need to learn how C++/CLI works- you can't just recompile a C++ program with /CLR and be done with it. The sc_main method here is native, not managed, and you can't reflect over it, and the same is true about the HelloWorld type, unless you redefined it to be a ref class, but I doubt you did because there you go in main instantiating it by value, which would only be legal if it was a native class.

.NET和本地代码具有非常根本不同的语义,而且一个神奇的编译器开关将不会在这方面帮助你。

.NET and native code have very fundamentally different semantics and a magic compiler switch will not help you in this regard.

这篇关于通过反射从C#访问C ++非成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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