从外部代码调用时 CMDIFrameWnd::MDIGetActive 返回 null

发布于 2025-01-13 11:08:01 字数 897 浏览 3 评论 0原文

我继承了一些遗留代码,需要将它们与现代 C# GUI 集成。代码库是一个 MFC MDI 应用程序,它创建类型库并注册 COM 组件以向外部应用程序和脚本公开应用程序 API。

在整个 MFC 应用程序(此后我将 MFC 代码称为“应用程序”)中,有一个函数用于检查活动 MDI 文档并返回它,或者返回 null。该方法是从继承 CMDIFrameWnd 的“MainFrame”类调用的。它看起来像这样:

CMDIChildWnd * pChildFrame = MDIGetActive();
if (pChildFrame)
{
    CDocument *pDoc = (CDocument *) pChildFrame->GetActiveDocument();
    if (pDoc)
    {
        return(pDoc);
    } else { 
        return NULL; 
}

我创建了一个测试 C# 控制台应用程序,可以成功导入 COM 组件并访问 API,并对其进行调用。问题是,每当我通过 API 调用需要活动文档的内容时,MDIGetActive() 都会返回 null。例如,我可以通过 API 打开文档,并且可以直观地确认它在正在运行的 MFC 应用程序中打开。但如果我调用 API 方法来保存该文件,则活动文档为空。但如果我通过应用程序 GUI 调用相同的方法,则效果很好。都是同一个函数调用,暴露的API方法只是一个包装器。

奇怪的是,当我通过 API 方法打开文件时,它最终会对活动文档执行相同的检查并成功。获取活动文档后,它调用 CDocument::UpdateAllViews() 来更新 UI。此后通过 API 进行的任何调用都将导致没有活动文档。

我在这里不知所措,我不明白为什么活动文档为空。我仍在研究 MFC 文档,但没有找到任何可以说明为什么会出现这种情况的内容。有谁知道吗?

I have inherited some legacy code that i'm required to integrate with a modern c# GUI. The codebase is a MFC MDI application, that creates a type library and registers a COM component to expose the application API for external applications and scripting.

Throughout the MFC application (henceforth I will refer to the MFC code as "the application") there is a function that checks for an active MDI document and returns it, or null. This method is called from a "MainFrame" class which inherits CMDIFrameWnd. it looks something like this:

CMDIChildWnd * pChildFrame = MDIGetActive();
if (pChildFrame)
{
    CDocument *pDoc = (CDocument *) pChildFrame->GetActiveDocument();
    if (pDoc)
    {
        return(pDoc);
    } else { 
        return NULL; 
}

I have created a test c# console application and can successfully import the COM component and access the API, and make calls to it. The problem is that whenever I call something that requires an active document via the API, MDIGetActive() returns null. For example, I can open a document via the API, and I can visually confirm it opens in the running MFC application. But if I call the API method to save this file, the active document is null. But if I call the same method via the application GUI, this works fine. It is the same function call, the exposed API method is just a wrapper.

Strangely enough when I open a file via the API method, it eventually executes the same check for an active document which succeeds. After getting the active document, it calls CDocument::UpdateAllViews() to update the UI. Any calls made after this via the API will result in no active document.

I'm at a loss here, I can't understand why the active document is null. I'm still working my way through MFC documentation but I haven't found anything that would suggest why this is the case. Does anyone know?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缘字诀 2025-01-20 11:08:01

避免任何 GetActiveWhatever() 方法的另一种方法是使用以下代码,它可以从您的 CYourApp 类中调用:

POSITION posDoc, pos = GetFirstDocTemplatePosition();
while (NULL != pos)
{
    CDocTemplate* pDocTemplate = (CDocTemplate*)GetNextDocTemplate(pos);
    posDoc = pDocTemplate->GetFirstDocPosition();
    while(NULL != posDoc)
    {
        CDocument* pDoc = pDocTemplate->GetNextDoc(posDoc);
        if(NULL != pDoc)
            pDoc->UpdateAllViews(pSender, lHint, pHint);
    }
}

当然,一旦您拥有该文档,您就可以该文档所附的任何视图。

Another way to avoid any GetActiveWhatever() method is the following code, which can be called from your CYourApp class:

POSITION posDoc, pos = GetFirstDocTemplatePosition();
while (NULL != pos)
{
    CDocTemplate* pDocTemplate = (CDocTemplate*)GetNextDocTemplate(pos);
    posDoc = pDocTemplate->GetFirstDocPosition();
    while(NULL != posDoc)
    {
        CDocument* pDoc = pDocTemplate->GetNextDoc(posDoc);
        if(NULL != pDoc)
            pDoc->UpdateAllViews(pSender, lHint, pHint);
    }
}

Of course, once you have the document, you'll have any view attached from that document.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文