title: LoadLibrary() returns 0 in C#
body: <p>I am creating an Autodesk Revit add-in in C#. Output of this project is a DLL itself. Within this project, I am loading another unmanaged DLL by calling:</p>
  
  <p><code>IntPtr pDll = LoadLibrary("E:\\Rep3DLib\\builds\\Release\\Index.dll");
  int a = Marshal.GetLastWin32Error();</code></p>
  
  <p>Within debugger, I see that <code>pDll</code> has value 0 and <code>a</code> has value 127 (Error: The specified procedure could not be found.). This is surprising because I am not even looking any particular procedure within the DLL. I just want to load the DLL.</p>
  
  <p>Now I delete the unmanaged DLL from <code>E:\\Rep3DLib\\builds\\Release\\Index.dll</code> and in the above code, <code>a</code> has value 126 (Error: The specified module could not be found.). This does make sense. It also means that when the DLL is present at the correct location, the code is able to find it but somehow it's looking for a procedure which it's not able to find.</p>
  
  <p>To debug this problem, I created another project within the same solution. Here's the code for this project:</p>
  
  <pre><code>namespace testIndex {
      class Program {
  
          [DllImport("kernel32.dll", SetLastError = true)]
          public static extern IntPtr LoadLibrary(string dllToLoad);
  
          static void Main(string[] args) {
              IntPtr pDll = LoadLibrary("E:\\Rep3DLib\\builds\\Release\\Index.dll");
              int a = Marshal.GetLastWin32Error();
          }
      }
  }
  </code></pre>
  
  <p>When I run this, <code>pDll</code> has a non-zero value and I am able to call the functions defined within the <code>Index.dll</code>.</p>
  
  <p>Note that both the projects have target platform as x64. I also verified that if I build the Revit add-in and run it on a different machine, it does find the DLL. So somehow it's not finding the DLL only on my machine although the path <code>E:\\Rep3DLib\\builds\\Release\\Index.dll</code> is definitely correct.</p>
  
  <p>Any idea what could be wrong?</p>