Skip to content

Calling Unmanaged DLL From C#

byron wallace

Asked Jul 25, 2007, 7:38 AM EDT5 answers
Share
LinkedInX
Hi all, I've inherited a code-base which uses existing DLLs that contain all of our statistical algorithms. In the .NET development environment, the DLLs execute fine and return as expected. Moreover, when I compile the .NET project to an .exe, I am able to run it locally on the same machine that the development was done on. However, when I attempt to port the executable (along with the DLLs, which I store in the same directory as the .exe) to other machines, the program crashes, with no error message, when the DLL is called (I verified this by removing the DLL call and compiling, and the program ran with no problem). In our Wrapper class (CoreAlgorithms.cs), I have the following code:

[DllImport(@"dcdflib.dll", EntryPoint = "cdft", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern unsafe void cdft(int* which, double* p, double* q, double* mult, double* df, int* status, double* bound);

I then call this method as follows:

unsafe
{
CoreAlgorithms.cdft(&which, &p, &q, &mult, &df, &status, &bound);
}


Again, this works fine in .NET and my machine, but fails immediately on other machines. Does anyone have any idea as to I'm doing incorrectly here? Any help or pointers would be greatly appreciated. Thank you for your time.

5 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    I suspect you don't need to make the PInvoke cdft call unsafe.  You can probably pass it as a ref to a double value

    e.g.

    [DllImport("cpplib.dll", ExactSpelling = true)]
    private static extern void GiveMeADouble(ref double myVal);

    Pass it as follows

    double myDoubleValue = 15.0d;

    GiveMeADouble(ref myDoubleValue);

    Best,
    0
  • 0
    Thank you, Mike! Unfortunately, that did not resolve the issue (it is a slightly cleaner way of doing things, however, in my opinion). But the (compiled) program still crashes on the same call when I port it to other machines. If you or anyone else has any more thoughts on what might cause this I would really appreciate it! Thanks again.

    Update: I have handled the generated exception on the call and output the Exception trace. Here it is:

    First, the exception message is: "Unable to load DLL 'dcdflib.dll': This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1)"

    And the StackTrace:

    System.DllNotFoundException: Unable to load DLL 'dcdflib.dll':
    This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1)

    (At the call to the dll method)
    The DLL is in the same directory as the executable.
    0
  • Mike Gold
    0

    Hi Byron,

    Is it possible that your machine contains certain dlls that the dllimport dll is dependent on and other machines don't?  One thing you can do is use Depends.exe to see if the dll you are importing depends on other dlls.  If you run depends on the machine that the unmanaged dll is on, it will tell you which dlls it is dependent on that are missing.

    Also make sure that all COM objects used by the dll are registered with regsvr32.exe

    0
  • 0
    Hi Mike, Yes, that was my suspicion originally. So I ran a dependency walker ( http://www.dependencywalker.com/ ) over the .dll that was crashing. I copied all of the dependencies to another machine and then ran the dependency walker on the dll there. It turns out that all of the dependencies were resolving, but for whatever reason the statistical algorithms dll's manifest file was corrupt. I tried porting over the copy from the development box but this didn't work. I ended up getting my hands on the dll (c) source and rebuilding a dll from scratch using the VS Studio wizard. This generated a new manifest and everything worked fine (I distribute the dll and manifest file in the same directory as my executable). I hope this might help anyone who has similar problems. Thanks again for your help, Mike.
    0
  • Mike Gold
    0
    Glad you got it working, Byron.

    It's lucky you had the source code for the statistical dlls.

    Thanks for posting how you solved the problem.

    Best,
    0