본문 바로가기
MFC

AFX_MANAGE_STATE(AfxGetStaticModuleState());

by leo21c 2019. 5. 17.
SMALL

MFC에서 동적 DLL을 만들고 Dialog 등을 만들어 사용할 때 DLL 함수의 첫 라인에 이 매크로 함수를 써야 한다.
예들들어 Sample Dialog를 만들어 DoModal()로 화면에 표시를 하려고 하는데 DoModal()이 -1로 리턴하며 화면에 표시되지 않는 경우를 확인 할 수 있다.

 

원인을 찾지 못하다가 타 소스를 참조해서 확인했다.

맨처음 제작했을 때에는 아래와 같이 만들었었다.

int ShowSampleDlg() ///< DLL 함수
{
  CSampleDlg dlg();
  int nReturn = dlg.DoModal();
  if( nReturn == IDOK )
  {
  }
  else if( nReturn == IDCANCEL )
  {
  }

  return nReturn;
}

그런데 dlg.DoModal()을 하고 바로 -1 리턴을 하는 것이다. 당연히 화면에 표시되는 것도 없다.

아래와 같이 매크로 함수를 추가하고 dlg에 parent를 넣어 주었다.

int ShowSampleDlg() ///< DLL 함수
{
   AFX_MANAGE_STATE( AfxGetStaticModuleState() );

  CSampleDlg dlg( AfxGetMainWnd() );

  int nReturn = dlg.DoModal();

  if( nReturn == IDOK )
  {
  }
  else if( nReturn == IDCANCEL )
  {
  }

  return nReturn;
}

DLL의 리소소를 정상적으로 사용하기 위해서는 AFX_MANAGE_STATE( AfxGetStaticModuleState() ); 매크로 함수를 추가해야 한다고 하는군요.

<참조> https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/ft1t4bbc(v=vs.100)

 

TN058: MFC Module State Implementation

TN058: MFC Module State Implementation 01/30/2013 11 minutes to read In this article --> Note The following technical note has not been updated since it was first included in the online documentation. As a result, some procedures and topics might be out of

docs.microsoft.com

For exported functions from a DLL, such as one that launches a dialog box in your DLL, you need to add the following code to the beginning of the function:

AFX_MANAGE_STATE(AfxGetStaticModuleState( ))

 

LIST