引用C ++/CLI代码时WPF应用程序中引发的异常 [英] Exception raised in WPF app, when referencing C++/CLI code

查看:139
本文介绍了引用C ++/CLI代码时WPF应用程序中引发的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用用C ++和A编写的大型代码库. MFC以及其他API,并且正在尝试创建使用此C ++代码的WPF应用程序(C#).我不想重写.NET中的所有内容.我正在创建一个包装一些C ++代码的C ++/CLI接口.由此产生的DLL被添加为对我的C#项目的引用.运行良好,但运行此代码时收到以下异常:

在PresentationFramework.dll中发生类型为System.Windows.Markup.XamlParseException的第一次机会异常.附加信息:无法创建在程序集中定义的Window1实例...,版本= 1.0.0.0,文化=中性,PublicKeyToken =空" .调用的目标引发了异常.标记文件"Window1.xaml"的第1行位置9错误.
我什至如何调试呢?

以下是C#代码:

//C#

Hi,

I am working with a large codebase written in C++ & MFC as well as other API''s, and am trying create a WPF application (C#) which uses this C++ code. I don''t want to rewrite everything in .NET. I am creating a C++/CLI interface that wraps some of the C++ code. The DLL produced from this is added as a reference to my C# project. It builds fine but I receive the following exception when running this code:

"A first chance exception of type System.Windows.Markup.XamlParseException occurred in PresentationFramework.dll. Additional information: Cannot create instance of Window1 defined in assembly ..., Version=1.0.0.0, Culture=neutral, PublicKeyToken=null''. Exception has been thrown by the target of an invocation. Error in markup file ''Window1.xaml'' Line 1 Position 9."

How can I even debug this?

Below is the C# code:

// C#

using System;
using IMyCLIInterface;
namespace CsharpNamespace
{
    public partial class Window1 : Window
    {
       public Window1()
        {
            InitializeComponent();
            // If I comment out the next two lines, then the exception does not occur
            IMyCLIInterface.theClass c = new IMyCLIInterface.theClass();
            c.TestInterface();
           // If instead of the above two lines I simply put the following line, then the exception does not occur 
           // IMyCLIInterface.theClass c = null
        }
    }
}



下面是C ++代码:

//H文件



And below is the C++ code:

// H file

#pragma once
namespace IMyCLIInterface
{
    public ref class theClass
    {
    public:
        theClass(void)
        {;}
        void TestInterface();
    };
}




//CPP文件




// CPP file

#include "StdAfx.h"
#include "IMyCLIInterface.h"

using namespace IMyCLIInterface;

[System::STAThreadAttribute]
void theClass::TestInterface()
{
    //nothing in here for now, but intending to call native C++ code in here
}

推荐答案

我过去有类似的问题,这几乎总是与程序集和相关非托管dll的加载有关.早期调用CWinApp :: InitInstance时,MFC dll可能会导致问题.在.net中,并非所有dll都是预先加载的,第一次访问类时,基础dll将被加载.



稍后尝试创建c ++/.net类,例如在加载Window1或仅进行测试时,从按钮单击事件中创建
I''ve had simular issues to this in the past, it is nearly always to do with the loading of assemblies and dependant unmanaged dlls. MFC dlls can cause problems when CWinApp::InitInstance is called to early. In .net not all dlls are loaded upfront, the first time you access a class then the underlying dlls will be loaded.



Try creating your c++/.net class later e.g. on Window1 load or just to test, create from a button click event


最可能的原因是WPF应用程序找不到C ++/CLI程序集.将DLL复制到WPF exe所在的文件夹中,您应该会看到此错误消失.
The most likely reason is that the WPF application cannot find the C++/CLI assembly. Copy the DLL to the same folder as the WPF exe and you should see this error go away.


感谢您的答复.我已经更新了C#项目,以便它生成类库DLL而不是exe.它将放置在包含将使用此DLL的可执行文件的文件夹中.我的本机接口类生成的DLL也放置在同一文件夹中.当我加载C#Dll时,它加载正常.但是,当我运行使用本机C ++ DLL的命令时,会收到一个异常:

System.IO.FileLoadException:无法加载文件或程序集IMyCLIInterface,版本= 0.0.0.0,区域性=中性,PublicKeyToken =空;或其依赖项之一. HRESULT的异常:0xE0434F4D
文件名:IMyCLIInterface,版本= 0.0.0.0,文化=中性,PublicKeyToken =空; ---> System.Runtime.InteropServices.COMException(0xE0434F4D):来自HRESULT的异常:MyDLL.Window1..ctor()处的0xE0434F4D

我已经尝试过DependencyWalker来确定问题,并且所需的DLL似乎在同一文件夹中.没有更简单的方法来查找到底是哪个引起问题的依赖项吗?

如果我单击继续"并再次运行该命令,则会收到另一个错误:
尝试在本机代码初始化期间使用此程序集中的MSIL代码.这表明您的应用程序中存在错误.这很可能是从本机构造函数或DllMain调用MSIL编译的(/clr)函数的结果.

我的印象是使用混合程序集并非易事,并且迁移到.NET将是更快(尽管不理想)的方法.
Thanks for your responses. I have updated my C# project so that it produces a class library DLL instead of an exe. This is placed in a folder containing the executable which will use this DLL. The DLL produced by my native interface class is also placed in the same folder. When I load the C# Dll, it loads fine. However when I run a command which uses my native C++ DLL, I recieve an exception:

System.IO.FileLoadException: Could not load file or assembly IMyCLIInterface, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null; or one of its dependencies. Exception from HRESULT: 0xE0434F4D
File name: IMyCLIInterface, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null; ---> System.Runtime.InteropServices.COMException (0xE0434F4D): Exception from HRESULT: 0xE0434F4D at MyDLL.Window1..ctor()

I have tried DependencyWalker to determine the problem and the required DLL''s appear to be in the same folder. Is there no easier way to find which dependency exactly is causing the problem?

If I hit continue and run that same command again, I then receive another error:
Attempt to use MSIL code from this assembly during native code initialization. This indicates a bug in your application. It is most likely the result of calling an MSIL-compiled (/clr) function from a native constructor or from DllMain.

I get the impression that using mixed assemblies is not trivial, and that migrating to .NET would be the faster (though not ideal) way to go.


这篇关于引用C ++/CLI代码时WPF应用程序中引发的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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