简体   繁体   English

在旧的MFC / Win32 DLL中实现CLR并将DLL托管到MFC / Win32应用程序中

[英]Implementing the CLR into old MFC/Win32 DLL and hosting DLL into MFC/Win32 Application

I have an old MFC application which I currently import an old MFC/Win32 DLL into. 我有一个旧的MFC应用程序,当前已将其导入到旧的MFC / Win32 DLL中。

In this old DLL, I have been tasked with writing a bunch of Multi-Threaded code which I was planning on writing using the .NET framework (hence needing the CLR). 在这个旧的DLL中,我的任务是编写一堆计划使用.NET框架编写的多线程代码(因此需要CLR)。

I've made cracks at this before, and even if I can get the project to compile properly with the CLR, I find that as soon as I try to use the DLL's user interface (written in MFC) after loading the DLL into the MFC/Win32 application, the application will crash pointing to problems with the user interface. 之前,我已经进行了破解,即使我可以使用CLR正确编译项目,也发现将DLL加载到MFC中后,尝试使用DLL的用户界面(用MFC编写)就可以了。 / Win32应用程序,该应用程序将崩溃,指向用户界面问题。

This DLL has always worked without the CLR, so I know that it is not broken. 此DLL始终在没有CLR的情况下可以工作,因此我知道它没有损坏。

What is the best way of implementing the CLR in my project, even if it is only for one class? 即使仅针对一门课程,在我的项目中实现CLR的最佳方法是什么?

EDIT : I currently can get the code to build with the CLR only on the one class I need it in, but the application I load the DLL into still crashes upon trying to load the user interface contained in the DLL. 编辑 :我目前只能在需要它的一个类上获取使用CLR构建的代码,但是在尝试加载DLL中包含的用户界面时,我将DLL加载到的应用程序仍然崩溃。

EDIT2 : I have figured out that it is failing an assertion on afxCurrentResourceHandle in afxwin1.inl . EDIT2:我已经想通了,这是失败的断言afxCurrentResourceHandleafxwin1.inl After doing more reading, I have a feeling that this has to do with MFC being in a "shared DLL" instead of "static DLL." 阅读更多内容后,我感觉这与MFC位于“共享DLL”而不是“静态DLL”中有关。 Is there a workaround for this assertion? 有没有针对此断言的解决方法?

_AFXWIN_INLINE HINSTANCE AFXAPI AfxGetResourceHandle()
    { ASSERT(afxCurrentResourceHandle != NULL);
        return afxCurrentResourceHandle; }

EDIT3 : I have made progress, but am still failing assertions! EDIT3 :我已经取得了进步,但是仍然失败了! Apparently before you create the objects of the pages within the user interface, you must use the AFX_MANAGE_STATE macro to have afxCurrentResourceHandle be defined! 显然,在用户界面中创建页面的对象之前,必须使用AFX_MANAGE_STATE宏定义afxCurrentResourceHandle!

Here's an example of what I mean: 这是我的意思的示例:

CPropertySheet Sheet("Config"); //Assume this is defined
AFX_MANAGE_STATE(AfxGetStaticModuleStatus());

CConfigPage ConfigPage;
CTestPage TestPage;

//Now I am failing an assertion when trying to run the following code
if (Sheet.DoModal() == ID_OK)
{
    //Do stuff...
}

The assertion currently failing now is: 当前失败的断言是:

CObject* AFX_CDECL AfxStaticDownCast(CRunTimeClass* pClass, CObject* pObject)
{
    ASSERT(pObject == NULL || pObject->IsKindOf(pClass));
    return pObject
}

pObject certainly isn't null: pObject: 0x043fd4fc {CWnd hWnd=0x002c0abe} pObject当然不为空: pObject: 0x043fd4fc {CWnd hWnd=0x002c0abe}

Combining MFC and .NET/CLR is unlikely to work, unless you can recompile the old MFC application with CLR support . 除非您可以使用CLR支持重新编译旧的MFC应用程序 ,否则将MFC和.NET / CLR结合使用不太可能。 Even then, I would strongly discourage it; 即使那样,我也会坚决劝阻。 the two frameworks are not intended to be used simultaneously. 这两个框架不能同时使用。 Keep in mind that you can only use one GUI thread even if you don't use MFC. 请记住,即使您不使用MFC,也只能使用一个GUI线程。

A better solution would be to use the standard MFC threading mechanisms. 更好的解决方案是使用标准MFC线程机制。 It's really not hard; 确实不难; just make a new class with a member function like this: 只需使用如下成员函数创建一个新类:

static UINT Go(LPVOID pParam);

Then call AfxBeginThread(Go, this) from somewhere else in your class. 然后从类中的其他地方调用AfxBeginThread(Go, this) Recast pParam to pointer to your class and then start calling functions on it. 重铸pParam以指向您的类的指针,然后开始在其上调用函数。 You don't have to use anything fancy to make that strategy work in MFC, and you've got all the standard Win32/MFC/C++ resources available. 您不必花任何花哨就可以使该策略在MFC中起作用,并且您已经获得了所有标准的Win32 / MFC / C ++资源。 Tell me what you need from .NET and I bet I can find a way to do the same thing in C++ or through COM. 告诉我您从.NET需要什么,我敢打赌,我可以找到一种方法来用C ++或通过COM执行相同的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM