了解此功能并将其从C ++转换为C#代码 [英] Understanding and converting this function from C++ to C# code

查看:120
本文介绍了了解此功能并将其从C ++转换为C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我有一个使用C ++编写的代码,我正在尝试编写等效的C#.我遇到了以下我无法理解的功能.因此,我无法为其编写C#代码.任何人都可以简短地描述此函数的作用以及与c#等效的作用或为我提供转换的任何指针.谢谢

Hello All
I have a code that is in C++ and I am trying to write the c# equivalent. I encountered the following function which I can not make sense of. Therefore I can not write the C# code for it. Can anyone briefly describe what does this function do and what would the c# equivalent be or give me any pointers for conversion. Thanks

static HRESULT Init(System::String^ InstanceName)
{
    wchar_t szInstanceName[MAX_PATH];
    pin_ptr<const wchar_t> ch = PtrToStringChars(InstanceName);
    wcscpy_s(szInstanceName, MAX_PATH, ch);
    return Init(szInstanceName);
};

推荐答案

首先,从什么转换为什么?这里的关键是:这个问题涉及三种不同的语言,而不是两种.它们是C ++,C ++/CLI和C#.

您可以从C ++/CLI转换"(实际上是直接转换)到C#(不完全是100%)然后返回,但是C ++和C#是用于不同平台的语言-没有转换".

现在,您显示的示例更加棘手:它将C ++/CLI和本机C ++代码混合在一起.从管理者域到非管理者域,这似乎是一个麻烦.它调用了一些未显示给我们的非托管函数Init(wchar_t).看起来该代码具有不受管的组件(引擎,我看不到某些东西),并且它接受用于初始化的宽字符指针.您显示的方法使用.NET System::String^参数添加托管函数,该函数将其转换为非托管指针并调用非托管Init.

此方法在C#中没有直接意义,因为C#不能直接使用非托管指针进行操作.相反,您可以使用P/Invoke调用应从某些DLL导出的非托管Init.我不确定您是否真的想要. C ++/CLI的好处之一是可以避免P/Invoke.无论如何,在这种情况下非常简单:
First of all, conversion from what to what? The key here is: there are three different languages involved in this question, not two. They are C++, C++/CLI and C#.

You can "convert" (actually, translate directly) from C++/CLI to C# (not exactly 100%) and back, but C++ and C# are languages for different platforms — there is no "convert".

Now, the sample you show is more tricky: it''s a mix of C++/CLI and native C++ code. It looks like it is a thunk from the manager domain to unmanaged one. It calls some unmanaged function Init(wchar_t) which you did not show to us. It looks like the code has unmanaged component (engine, something I cannot see), and it accept a wide character pointer for initialization. The method you show adds the managed function with .NET System::String^ parameter which converts it to an unmanaged pointer and calls unmanaged Init.

This method makes no direct sense in C# which does not operate with unmanaged pointers directly. Instead, you could use P/Invoke to call the unmanaged Init which should be exported from some DLL. I''m not sure you really want it. One of the benefits of C++/CLI is the ability to avoid P/Invoke. Anyway, in this case it''s pretty simple:
using System.Runtime.InteropServices;
using HRESULT = System.Int32;

//...

const string DllName = //... ?

//...

[DllImport(DllName, EntryPoint="Name")]
static extern HRESULT Init([MarshalAs(UnmanagedType.LPWStr] string instanceName);


请注意EntryPoint:由于C ++中可能的名称修饰,您可能需要对其进行修改:
http://en.wikipedia.org/wiki/Name_mangling [ http: //msdn.microsoft.com/en-us/library/c1h23y6c%28v=vs.100%29.aspx [ http://en.wikipedia.org/wiki/P/Invoke [ http://msdn.microsoft.com/en-us/library/Aa712982 [ ^ ].

此CodeProject也可能有用:基本P/调用 [


Pay attention for EntryPoint: you might need to modify it, due to possible name mangling in C++:
http://en.wikipedia.org/wiki/Name_mangling[^].

Use some binary dump utility (such as "dumpbin.exe", http://msdn.microsoft.com/en-us/library/c1h23y6c%28v=vs.100%29.aspx[^]) to see under what exact name it was really exported.

If you need to learn P/Invoke, start from here:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/en-us/library/Aa712982[^].

This CodeProject can also be useful: Essential P/Invoke[^].

—SA


这篇关于了解此功能并将其从C ++转换为C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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