title: How to prevent a DLL from being unloaded by the app that uses it?
body: <p>I created a DLL in C++ named MyDLL.dll.
  MyDLL.dll exported interface outside as following:
  Here is my dll code:</p>
  
  <pre><code> CMyAdapter g_DllImp;
  
   BOOL APIENTRY DllMain(
      HMODULE hModule,
      DWORD  dwReason,
      LPVOID pReserved
  )
  {
      if (DLL_PROCESS_ATTACH == dwReasonForCall)
          g_DllImp.m_hModule = hModule;
      else if (DLL_PROCESS_DETACH == dwReasonForCall)
          g_DllImp.m_hModule = NULL;
  
      return TRUE;
  }
   __declspec(dllexport) HRESULT GetVersion(
      tVersion* pVersion
  )
  {
      return g_DllImp.GetApiVersion(
                  pVersion);
  }
  </code></pre>
  
  <p>Here is the example of main app:</p>
  
  <pre><code>int _tmain(int argc, _TCHAR* argv[])
  {
    HMODULE module = LoadLibrary(L"MyDLL.dll");
  ....
    // call the interface of MyDLL.dll
    GetVersion(&amp;pVersion);
    return 0;
  }
  </code></pre>
  
  <p>unfortunately, my main app is crash due to unknown reason. but according to Dump Crash File the crash is MyDLL is unloaded:</p>
  
  <pre><code>0289cc24 52e4577e &lt;Unloaded_MyDLL.dll&gt;+0x15728
  </code></pre>
  
  <p>I must use the code to catch exception:</p>
  
  <pre><code>__try{} 
   __except(EXCEPTION_EXECUTE_HANDLER)
  </code></pre>
  
  <p>My question is:</p>
  
  <p>Should I use __try{} __except() in interface of MyDLL or in main app?
   Which one is better?</p>
  
  <p>Could you please help me?
  Thanks in advance for your support.</p>