title: SetDllDirectory LoadLibrary inside a DLL
body: <p>Can I use C++ <code>SetDllDirectory</code> and <code>LoadLibrary</code> commands inside a C++ DLL to load another DLL? I have tried using them like this: </p>
  
  <p>Executable calls the 1st DLL, 
  then 1st DLL loads the 2nd DLL,
  then 2nd DLL does the calculations...</p>
  
  <p>but when I run the executable, I get this error message: </p>
  
  <blockquote>
    <p>This application has requested the Runtime to terminate it in an unusual way. Please contact the applications support team for more information. </p>
  </blockquote>
  
  <p>2nd DLL works fine when linked directly to Executable!</p>
  
  <p>This is the code inside my executable: </p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;iostream&gt;
  
  int main(){
      HINSTANCE hDLL_Link=NULL;
      SetDllDirectory((LPCWSTR)L"C:\\Users\\MC\\Documents\\2014_07_01a_FDD_VC2008\\test_call_DLL\\EXE_calls_Link_DLL\\Release");
      hDLL_Link=LoadLibrary((LPCWSTR)L"Link_DLL.dll");
      if(hDLL_Link==NULL) std::cout&lt;&lt;"did not load"&lt;&lt;'\n';
      typedef void (*Ptr_OPS_Link)();
      Ptr_OPS_Link Ptr_OPS_Link_0;
      Ptr_OPS_Link_0=(Ptr_OPS_Link)GetProcAddress(hDLL_Link,"OPS_Link");
      Ptr_OPS_Link_0();
      FreeLibrary(hDLL_Link);
      system("pause");
  }
  </code></pre>
  
  <p>this is the code inside the 1st DLL:</p>
  
  <pre><code>#include "Link.h"
  
  extern "C" __declspec(dllexport)
  void OPS_Link(){
      Link*Link_Ptr_Object=NULL;
      if(Link_Ptr_Object==NULL){
          Link_Ptr_Object=new Link();
      }
      if(Link_Ptr_Object==NULL){
          //can not throw inside __declspec(dllexport) functions marked extern "C" that's why std::cout is implemented:
          std::cout&lt;&lt;"Error: could not link to FDD DLL"&lt;&lt;'\n';
          system("pause");
      }
      delete Link_Ptr_Object;
      Link_Ptr_Object=NULL;
  }
  
  Link::Link()
  :m_void_Ptr_ObjectByDLL(NULL){
      HINSTANCE hDLL=NULL;//handle to DLL
      SetDllDirectory((LPCWSTR)L"C:\\Software\\Octave-3.6.1\\bin\\");
      hDLL=LoadLibrary((LPCWSTR)L"C:\\Users\\MC\\Documents\\2014_07_01a_FDD_VC2008\\Executable\\Release\\FDD_DLL.dll");
      if(hDLL==NULL){
          throw "DLL loading could not be done";
      }else if(hDLL!=NULL){
          typedef void (*Ptr_OPS_FDD)(std::string, int, int);
          Ptr_OPS_FDD Ptr_OPS_FDD_0;//pointer to procedure inside DLL
          Ptr_OPS_FDD_0=NULL;
          Ptr_OPS_FDD_0=(Ptr_OPS_FDD)GetProcAddress(hDLL,"OPS_FDD");
          if(Ptr_OPS_FDD_0==NULL){
              FreeLibrary(hDLL);
              throw "DLL exported function address could not be determined";
          }else{
              //run the procedure inside DLL:
              Ptr_OPS_FDD_0("FDD_INPUT_Truss_Bridge_Data2_Ambient_Inch_11Channels_77824Samples_SamplingFreq_256Hz.txt",11,256);//LabScaleTruss
              //Ptr_OPS_FDD_0("FDD_INPUT_Ambient_EW_15Channels_3000Samples_SamplingFreq_20Hz.txt",15,20);//AmbientEW
              //Ptr_OPS_FDD_0("FDD_INPUT_Meriden_3Channels(3_5_8)_3686400Samples_SamplingFreq_2048Hz.txt",3,2048);//MeridenBridge
              FreeLibrary(hDLL);
          }
      }
  }
  </code></pre>