title: DLL Runtime Loading in C#
body: <p>I have a DLL <code>Foo.dll</code> that I want to load into the process space. This DLL is native and unmanaged. I also have a static class <code>NativeMethods</code> which contains function delegates for <code>LoadLibrary</code>, <code>GetProcAddress</code> and <code>FreeLibrary</code> that are called from <code>Kernel32.dll</code>. These work fine as I can get a pointer when trying to load <code>Kernel32.dll</code>.</p>
  
  <p>My problem lies in the fact that within my bin folder I have setup a directory called <code>modules</code> which will contain all my unmanaged Dll's. When I try to load <code>Foo</code> from this directory, the <code>LoadLibrary</code> function returns a zero pointer, however when I try and load it from the bin directory I get a pointer that points to a memory address.</p>
  
  <p>My DLL's are Loaded as follows:</p>
  
  <pre><code>IntPtr dll = NativeMethods.LoadLibrary(Directory.GetCurrentDirectory() + "\\modules\\Foo.dll"); 
  //dll = 0
  
  IntPtr dll = NativeMethods.LoadLibrary(Directory.GetCurrentDirectory() + "\\Foo.dll");
  //dll = address
  </code></pre>
  
  <p>Any help with this matter would be much appreciated!</p>