您的位置:首页 > 其它

How to call a DLL Dialog resource

2010-11-22 10:24 507 查看
If we want to use a Dialog box resource in a DLL.As begining,I should tell you some details about AFX_MANAGE_STATE macro.

AFX_MANAGE_STATE( AFX_MODULE_STATE* pModuleState ) pModuleState is pointer to an AFX_MODULE_STATE module.We can make use of this macro so that we can protect an exported function in a DLL.When this macro is invoked,pModuleState is the effective module state for the reminder of the immediate containing scope.Upon leaving the scope,the previous effective module state will be automatically restored.The AFX_MODULE_STATE contains global data for module.That is,the portion of module state is pushed or popped.By default,MFC uses the resource handle of the main application to load resource template like dialog something like that.If we have a exported function in a DLL,such as one that launches

a dialob box in the DLL,this template is actually stored in the DLL module. We need to switch

the module state for the correct handle to be used.We can do this by adding the following code to the beginning of the function:



CTestApp theApp; //The Object of main application.

void ShowDialog() //An exported function in DLL
{
AFX_MANAGE_STATE(AfxGetStaticModuleState()); // Switch the state of module

//This swaps the current module state with the state returned from AfxGetStaticModuleState until the end of the current scope



CTestDlg dlg;
dlg.DoModal(); //call the dialog box resource.
}

The next,we should import DLL library file to the source files of our work project like this:

(...cpp)

#pragma comment(lib, "test.lib")

Addheader file TestDialog.h to our work project,declare exported function in this source files like this:

void showDlg();

and then we can do this by calling this exported function like following code:

void CDemoDlg::OnTest()
{
ShowDialog();
}



; Test.def : Declares the module parameters for the DLL.

LIBRARY "Test"
DESCRIPTION 'Test Windows Dynamic Link Library'

EXPORTS
ShowDialog

The above mentioned is a simple instance of application.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: