AFX_MANAGE_STATE(AfxGetStaticModuleState()) 到底做什么

发布于 2024-12-29 15:02:17 字数 263 浏览 2 评论 0原文

我使用过很多模式对话框,它们在不使用 AFX_MANAGE_STATE 的情况下工作得很好,但最近我正在开发一个不同的项目,其中资源 dll 与启动 dll 不同。我上网冲浪并找到了上面的行,当我在启动对话框之前插入它时,它起作用了。我想也许因为我们有不同的 dll,所以我们需要加载主 dll 的状态才能启动对话框,但我不确定。我无法在互联网上的任何地方找到很好的解释。谁能简单地解释一下 AFX_MANAGE_STATE 的作用以及为什么我突然不得不使用它。

谢谢。

I have used a lot of modal dialogs and they worked fine without the use of AFX_MANAGE_STATE, but recently I was working on a different project in which the resource dlls are different from the launching dll. I surfed the web and found out the above line and when I inserted it before launching the dialog, it worked. I guess that maybe since we have different dlls, we need to load the state of the main dll in order to launch the dialog, but I am not sure. I have not been able to find a good explanation anywhere on the internet. Could anyone please explain in simple terms what AFX_MANAGE_STATE does and why I suddenly had to use it.

Thanks.

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

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

发布评论

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

评论(2

何以心动 2025-01-05 15:02:17

每个 .exe 和 .dll 都有一个内部资源句柄,指向您的对话框和其他资源。如果调用DLL中的函数,则当前资源句柄指向.exe中的资源,这是错误的,需要更改为DLL的资源。

这就是 AFX_MANAGE_STATE 的作用。

Every .exe and .dll has an internal resource handle, pointing to your dialogs and other resources. If you call a function in your DLL, the current resource handle is pointing to the resources in the .exe, which is wrong and needs to be changed to the resources of the DLL.

This is what AFX_MANAGE_STATE does.

凤舞天涯 2025-01-05 15:02:17

AFX_MANAGE_STATE 是一个调用资源函数的宏,以便仅在该 DLL 中查找资源,而不是在调用特定函数的 EXE/DLL 中查找资源。该宏还会导致 AFX_MAINTAIN_STATE 类放入堆​​栈中。此类将在函数退出时重置资源查找,以便调用此导出函数的 EXE/DLL 可以重新进行资源搜索。

用 C++ 术语来说:

// Some exported function that launches GUI or uses other resources
int GetSomething()
{
   AFX_MANAGE_STATE();
  ...
}

类似于(不完全是):

int GetSomething()
{
       SetResourceSearchingToThisDLL();

       AFX_MAINTAIN_STATE state_RAII; 

       //Use resource

       // Compiler will put destroctor call for state_RAII object here
       // which will mean AFX_MAINTAIN_STATE::~AFX_MAINTAIN_STATE()
       // And that would call something like:
       ResetResourceSearching();
}

在同一个 DLL 调用堆栈中使用此宏不会伤害任何人,因为资源搜索有一些使用计数器,它将恢复给调用者( DLL/EXE 资源)仅当它达到 0 时才有效。

需要注意的是,并非每个 MFC DLL 都必须使用此宏。仅当 DLL 由非 MFC 客户端加载时,可能是由 C 客户端、基于 C++ 控制台的应用程序、.NET 客户端等(是的,也可能是 MFC Windows 应用程序客户端)。

如果您的 EXE 和 DLL 是在 MFC 中制作的,使用相同的 MFC/编译器/链接器版本并且有一个 CWinApp 对象,则不需要使用此宏。

AFX_MANAGE_STATE is a macro which calls resource function so that resource would be looked up only in this DLL, and not the EXE/DLL from which particular function is called. This macro also causes AFX_MAINTAIN_STATE class to be put on stack. This class would, on exit of function, reset the resource lookup, so that EXE/DLL that called this exported function gets it resource searching back.

In C++ terms:

// Some exported function that launches GUI or uses other resources
int GetSomething()
{
   AFX_MANAGE_STATE();
  ...
}

Would be something like (not exactly):

int GetSomething()
{
       SetResourceSearchingToThisDLL();

       AFX_MAINTAIN_STATE state_RAII; 

       //Use resource

       // Compiler will put destroctor call for state_RAII object here
       // which will mean AFX_MAINTAIN_STATE::~AFX_MAINTAIN_STATE()
       // And that would call something like:
       ResetResourceSearching();
}

Usage of this macro, within same DLL call stack wont hurt anyone, since Resource-Searching has some usage-counter, which will revert to caller (DLL/EXE resource) only if it reaches 0.

It is important to note that, not every MFC DLL has to use this macro. It is only if DLL is loaded by non-MFC client, may be by a C client, a C++ console based application, .NET client etc (yes, may be MFC Windows application client also).

If your EXE and DLL are made in MFC, using same MFC/Compiler/linker version and has one CWinApp object, you need not to use this macro.

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