如何(使用C ++ \\ CLI和IJW)从本地C ++调用C#库 [英] How to call a C# library from Native C++ (using C++\CLI and IJW)

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

问题描述

背景:作为一个更大的任务,我需要做一个C#库访问非托管C ++和C code的一部分。在试图回答这个问题我自己,我一直在学习C ++ / CLI过去数天/周。

Background: As part of a larger assignment I need to make a C# library accessible to unmanaged C++ and C code. In an attempt to answer this question myself I have been learning C++/CLI the past few days/ weeks.

有似乎是许多不同的方式使用C#的dll从非托管的C ++和C的一些简要的答案似乎是实现:使用侵犯服务,使用.COM。和regasm,使用的PInvoke(这似乎去从C#到C ++中只),并在C / CLR使用IJW(它似乎被侵犯的服务)。我想这将是最好建立一个库,也许是使用IJW调用原生C ++和C code的代表我的C#DLL中的CLR包装。

There seems to be a number of different ways to achieve using a C# dll from unmanaged C++ and C. Some of the answers in brief appear to be: using Interlope services, Using .com. and regasm, Using PInvoke (which appears to go from C# to C++ only), and using IJW in the C++/CLR (which appears to be Interlope services). I am thinking it would be best to set up a library that is perhaps a CLR wrapper that uses IJW to call my C# dll on the behalf of native C++ and C code.

具体细节:我需要通过字符串的值以及INT到C#DLL从C + + code,并返回void

Specifics: I need to pass values of string as well as int to a C# dll from c++ code, and return void.

相关性:许多公司有很多借口混搭C ++,C和C#。性能:非托管code通常更快,接口:托管接口通常更易于维护,部署,并经常对眼睛更容易,经理告诉我们了。传统code迫使我们了。正是在那里(就像我们爬过山)。虽然如何调用从C#中的C ++库的例子是丰富的。如何从C ++ code调用C#库的例子是很难通过谷歌搜索,特别是如果你想看到更新4.0+ code找到。

Relevance: Many companies have many excuses to mix and match C++, C and C#. Performance: unmanaged code is usually faster, interfaces: Managed interfaces are generally easier to maintain, deploy, and are often easier on the eyes, Managers tell us too. Legacy code forces us too. It was there (Like the mountain that we climbed). While examples of how to call a C++ library from C# are abundant. Examples of how to call C# libraries from C++ code are difficult to find via Googling especially if you want to see updated 4.0+ code.

软件: C#,C ++ / CLR,C ++,C,Visual Studio 2010中,和.NET 4.0

Software: C#, C++/CLR, C++, C, Visual Studio 2010, and .NET 4.0

问细节:确定多部分的问题:


  1. 是否有优势,使用COM对象?或PInvoke的?或一些其他的方法? (我觉得学习曲线在这里将是多么险峻,尽管我的确发现在谷歌的土地主题的更多信息。IJW似乎答应我想要做的事。我应该放弃寻找一个IJW解决方案,关注这个呢?)(优势/劣势?)

  1. Is there an advantage to using com objects? Or the PInvoke? Or some other method? (I feel like the learning curve here will be just as steep, even though I do find more information on the topic in Google Land. IJW seems to promise what I want it to do. Should I give up on looking for an IJW solution and focus on this instead?) (Advantage/ disadvantage?)

我在想象,有一个解决方案,我写了一个包装,它利用IJW在C ++ / CLR正确吗?我在哪里可以找到关于此主题的更多信息,不说我也没有足够的谷歌/或看MSDN瞒着我,你看到它在那里。 (我想我preFER此选项,以撰写简单明了的code中的努力。)

Am I correct in imagining that there is a solution where I write a wrapper that that utilizes IJW in the C++/CLR? Where can I find more information on this topic, and don’t say I didn’t Google enough/ or look at MSDN without telling me where you saw it there. (I think I prefer this option, in the effort to write clear and simple code.)

问题的范围缩小:我觉得我真正的问题,需要的是回答下面的小问题:我如何设置一个非托管C ++文件可以在Visual Studio中使用C ++ / CLR库。我想,如果我可以简单地实例化非托管C ++ code一个托管C ++类的话,我也许能够制定出其余的(接口和包装等)。我希望我的主要愚蠢是试图建立参考/#包括等Visual Studio中,显然以为我可以有其他的误解。也许答案这件事可能是只是一个教程或帮助我指导的链接。

A narrowing of question scope: I feel that my true issue and need is answering the smaller question that follows: How do I set up a C++/CLR library that an unmanaged C++ file can use within visual studio. I think that if I could simply instantiate a managed C++ class in unmanaged C++ code, then I might be able work out the rest (interfacing and wrapping etc.). I expect that my main folly is in trying to set up references/#includes etc. within Visual Studio, thought clearly I could have other misconceptions. Perhaps the answer to this whole thing could be just a link to a tutorial or instructions that help me with this.

研究:我用Google搜索,并取得了一些成功猛吃了一遍又一遍。我发现有很多链接,告诉你如何使用从C#code非托管库。我会承认,出现了一些链接,展示了如何使用COM对象来做到这一点。没有多少结果在VS 2010的目标。

Research: I have Googled and Binged over and over with some success. I have found many links that show you how to use an unmanaged library from C# code. And I will admit that there have been some links that show how to do it using com objects. Not many results were targeted at VS 2010.

参考文献:
我已阅读一遍又一遍的许多职位。我曾尝试通过最相关的工作。有些人似乎功亏一篑的答案,但我似乎无法让他们的工作。我怀疑,我缺少的是诱人小如滥用关键字REF,或缺少的#include或using语句,或命名空间的滥用或不正确实际使用IJW功能,或缺少一个设置VS需要正确处理的编制等,所以你想知道,为什么不包括code?嗯,我觉得我在那里我明白了,并期望code我要工作的地方我不是。我想在我的理解的地方,当我到达那里,也许我就会需要帮助修复它。我将随机选择包括两个环节,但我不允许他们展示所有在我目前的水平Hitpoint的

References: I have read over and over many posts. I have tried to work through the most relevant ones. Some seem tantalizingly close to the answer, but I just can’t seem to get them to work. I suspect that the thing that I am missing is tantalizingly small, such as misusing the keyword ref, or missing a #include or using statement, or a misuse of namespace, or not actually using the IJW feature properly, or missing a setting that VS needs to handle the compilation correctly, etc. So you wonder, why not include the code? Well I feel like I am not at a place where I understand and expect the code I have to work. I want to be in a place where I understand it, when I get there maybe then I'll need help fixing it. I'll randomly include two of the links but I am not permitted to show them all at my current Hitpoint level.

<一个href=\"http://www.$c$cproject.com/Articles/35437/Moving-Data-between-Managed-$c$c-and-Unmanaged-Cod\">http://www.$c$cproject.com/Articles/35437/Moving-Data-between-Managed-$c$c-and-Unmanaged-Cod

这从调用code管理,并在两个方向上的非托管code从C ++去到Visual Basic和背部通过C ++ CLR,当然我感兴趣的是C#:<一href=\"http://www.$c$cproject.com/Articles/9903/Calling-Managed-$c$c-from-Unmanaged-$c$c-and-vice\">http://www.$c$cproject.com/Articles/9903/Calling-Managed-$c$c-from-Unmanaged-$c$c-and-vice

This calls code from managed and unmanaged code in both directions going from C++ to Visual Basic and back via C++CLR, and of course I am interested in C#.: http://www.codeproject.com/Articles/9903/Calling-Managed-Code-from-Unmanaged-Code-and-vice

推荐答案

我发现的东西,至少开始回答我的问题。以下两个链接有一个展示非托管C ++使用C#类从微软的WMV文件。

I found something that at least begins to answer my own question. The following two links have wmv files from Microsoft that demonstrate using a C# class in unmanaged C++.

这第一个使用的COM对象和regasm: http://msdn.microsoft。 COM / EN-US / vstudio / bb892741

This first one uses a COM object and regasm: http://msdn.microsoft.com/en-us/vstudio/bb892741.

这第二个使用C ++ / CLI的功能来包装C#类: HTTP: //msdn.microsoft.com/en-us/vstudio/bb892742 。我已经能够从实例化管理code A C#类和检索字符串作为视频。这是非常有益的,但​​它只是回答我的问题,2 /季军因为我想实例化一个类的字符串周边成C#类。作为概念证明我改变了的例子psented以下方法code $ P $,并实现了这一目标。当然,我还添加了一个改变了{公共字符串PickDate(字符串名称)}方法,做一些与名称字符串来证明自己,它的工作。

This second one uses the features of C++/CLI to wrap the C# class: http://msdn.microsoft.com/en-us/vstudio/bb892742. I have been able to instantiate a c# class from managed code and retrieve a string as in the video. It has been very helpful but it only answers 2/3rds of my question as I want to instantiate a class with a string perimeter into a c# class. As a proof of concept I altered the code presented in the example for the following method, and achieved this goal. Of course I also added a altered the {public string PickDate(string Name)} method to do something with the name string to prove to myself that it worked.

wchar_t * DatePickerClient::pick(std::wstring nme)
{
    IntPtr temp(ref);// system int pointer from a native int
    String ^date;// tracking handle to a string (managed)
    String ^name;// tracking handle to a string (managed)
    name = gcnew String(nme.c_str());
    wchar_t *ret;// pointer to a c++ string
    GCHandle gch;// garbage collector handle
    DatePicker::DatePicker ^obj;// reference the c# object with tracking handle(^)
    gch = static_cast<GCHandle>(temp);// converted from the int pointer 
    obj = static_cast<DatePicker::DatePicker ^>(gch.Target);
    date = obj->PickDate(name);
    ret = new wchar_t[date->Length +1];
    interior_ptr<const wchar_t> p1 = PtrToStringChars(date);// clr pointer that acts like pointer
    pin_ptr<const wchar_t> p2 = p1;// pin the pointer to a location as clr pointers move around in memory but c++ does not know about that.
    wcscpy_s(ret, date->Length +1, p2);
    return ret;
}

我的问题的一部分是:什么是好?从我在很多很多努力研究,答案已经阅读是一个COM对象被认为是更容易使用,以及使用的包装,而不是允许更大的控制权。在使用包装可以(但不总是)降低thunk的大小,作为COM对象某些情况下,自动拥有一个标准尺寸封装和包装只是一样大,他们需要。

Part of my question was: What is better? From what I have read in many many efforts to research the answer is that COM objects are considered easier to use, and using a wrapper instead allows for greater control. In some cases using a wrapper can (but not always) reduce the size of the thunk, as COM objects automatically have a standard size footprint and wrappers are only as big as they need to be.

在thunk(正如我上面使用)是指在COM对象的情况下,C#和C ++之间使用的空间时间和资源,并在C ++ / CLI和本地C ++之间使用的编码-a的情况下, C ++ / CLI包装。所以,我的答案的另一部分应包括警告,穿越的thunk边界超过绝对必要的是不好的做法,访问环路内的thunk边界不推荐,而且它有可能错误地设置了一个包装,使其加倍的thunk没有code似乎是不正确的像我这样一个新手(只有一个thunk是呼吁越过边界两次)。

The thunk (as I have used above) refers to the space time and resources used in between C# and C++ in the case of the COM object, and in between C++/CLI and native C++ in the case of coding-using a C++/CLI Wrapper. So another part of my answer should include a warning that crossing the thunk boundary more than absolutely necessary is bad practice, accessing the thunk boundary inside a loop is not recommended, and that it is possible to set up a wrapper incorrectly so that it double thunks (crosses the boundary twice where only one thunk is called for) without the code seeming to be incorrect to a novice like me.

对WMV的两个注意事项。首先:一些画面在两个重复使用,不要上当。起初,他们似乎相同,但他们确实涵盖不同的主题。其次,还有一些额外的功能,如编组,而现在未涵盖的WMV的CLI中的一部分。

Two notes about the wmv's. First: some footage is reused in both, don't be fooled. At first they seem the same but they do cover different topics. Second, there are some bonus features such as marshalling that are now a part of the CLI that are not covered in the wmv's.

编辑:

请注意有您的安装结果,你的C ++包装将不会被CLR找到。你将不得不要么确认C ++应用程序在任何安装/每次使用它,或图书馆(将需要被强命名),在安装时添加到GAC目录。这也意味着,在开发环境中这两种情况下,你可能将不得不库复制到每个应用程序的地方调用它的目录。

Note there is a consequence for your installs, your c++ wrapper will not be found by the CLR. You will have to either confirm that the c++ application installs in any/every directory that uses it, or add the library (which will then need to be strongly named) to the GAC at install time. This also means that with either case in development environments you will likely have to copy the library to each directory where applications call it.

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

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