从WPF应用程序中的不同模块调用c ++类/其他模块 [英] Invoking c++ classes/other modules from different module in WPF Application

查看:96
本文介绍了从WPF应用程序中的不同模块调用c ++类/其他模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

在我的WPF应用程序中,我需要从其他模块引用/调用c ++类到当前项目中.

假设我正在<<< MySolution.sln>>其中有许多项目,并且这里有一个处理wpf GUI(MyWPFproj.cproj)的程序,尽管在不同的模块/项目中,也需要引用一个C ++类,即(MyCPPProj.cproj)如何在wpf中解决它. />
因为我来自MFC,所以如果我们需要引用其他dll,则在我们的exe中,我们在InitInstance()中使用Load Library和GetProcAddress构造.

但是在wpf中如何使用相同的方法,我正在使用MyWPFBasic.xaml.cs
与以下,

使用系统;
使用System.Collections.Generic;
使用System.Windows;
使用System.Windows.Input;
...

命名空间Myproj.UI.Windows
{
公共局部类MyProj:Windows
等,

我正在使用VS2008 IDE通过WinXP o/s,SP 1开发WPF应用程序.

在此先感谢,

问候,
Samanth_90

Dear All,

In my WPF App, I need to refer/invoke the c++ class from a different module.,into the current project.

Assuming, I am working in a <<MySolution.sln>> which has numerous projects, and one handling wpf GUI(MyWPFproj.cproj) here, I need to refer a C++ class albeit in different module/project., viz., (MyCPPProj.cproj) how could it be addressed in wpf.

since I am from MFC, If we need to refer a differnt dll, in our exe we use the Load Library and GetProcAddress constructs in InitInstance().,

but how is the methodology of the same in wpf, I am using MyWPFBasic.xaml.cs
with following,

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
...

namespace Myproj.UI.Windows
{
public partial class MyProj:Windows
etc.,

I am using VS2008 IDE for Developing WPF Application using WinXP o/s,SP 1.

Thanks in Advance,

With Regards,
Samanth_90

推荐答案

不支持C ++类和.NET之间的直接调用,因为C ++类的布局是特定于该语言的,并且不能普遍描述为平台调用. br/>
基本上,您仅导入静态方法.您可以将C ++类的功能包装到一组静态方法中,将其导入.NET中,然后将其包装在.NET类中.

然后您有两个选择:

Direct invocation between C++ classes and .NET is not supported, because layout of C++ classes is specific to the language and cannot be universally described as platform invoke.

Basically, you import only the static methods. You can wrap the functionality of the C++ class into a set of static methods, import them in .NET and wrap them in .NET class.

Then you have two options:

  • Make your C++ project an unmanaged DLL. Use P/Invoke to consume your DLL in a .NET project. Please see:

    http://en.wikipedia.org/wiki/PInvoke[^];
    http://msdn.microsoft.com/en-us/library/Aa712982[^].

    This CodeProject article could also be useful:
    http://www.codeproject.com/csharp/EssentialPInvoke.asp[^].
  • Develop a mixed mode C++/CLI + C++ (managed+unmanaged) DLL out of your C++ application. Wrap you C++ code in managed ("ref") C++/CLI classes and make them and required method public. Resulting DLL can be used as a regular .NET assembly: referenced in your .NET project.

    Please see:
    http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
    http://www.ecma-international.org/publications/standards/Ecma-372.htm[^].


这篇关于从WPF应用程序中的不同模块调用c ++类/其他模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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