title: Why does this explicitly-linked DLL delegate succeed in one program but not another?
body: <p>I have 2 programs which share a project in which an API DLL is used to perform actions on files in another directory. The problem that I have is that this code works in one project, but in the other it fails:</p>
  
  <pre><code>moduleHandle = LoadLibrary(“api.dll”)
  </code></pre>
  
  <p>Earlier on, the program that fails failed to connect to the API DLL here so after comparing the Process Monitor logs of both I copied some DLLs into the same directory as it. It then succeeded, and the values of moduleHandle are now the same in both programs. Next:</p>
  
  <pre><code>methodPointer = GetProcAddress(moduleHandle, "NameOfSomeFunction")
  delegateForSomeFunction = Marshal.GetDelegateForFunctionPointer(methodPointer, GetType(SomeFunctionType))
  </code></pre>
  
  <p>When we get to the delegateForSomeFunction, I can see that the value of _methodPtr in them is different from one program to another. Then when we get to invoking the delegate:</p>
  
  <pre><code>Dim argsArray() As Object = New Object() {directoryPath, errorFilePath}        
  Dim retVal As Short = delegateForSomeFunction.DynamicInvoke(argsArray)
  </code></pre>
  
  <p>In one program this just fails silently. I'm at a bit of a loss as to how to proceed. What should I do to investigate this further and how can I get it to work?</p>
  
  <p>I’m running this on debug in Visual Studio 2013, and the project is in .NET 4.</p>