Search code examples
c#c++dllxnafmod

C++ Unmanaged DLL in C# runtime import failure


I'm running a C# XNA game project on Microsoft Visual Studio 2013 using a 32-bit architecture. When attempting to load an unmanaged C++ DLL at runtime using the DllImport tag, I get the error below. Note that this dll (fmod_event.dll) comes from the FMOD Ex Programmer’s API located under /fmoddesignerapi/api/

An unhandled exception of type 'System.DllNotFoundException'.
Additional information: Unable to load DLL 'fmod_event': The specified module could not be found.(Exception from HRESULT: 0x8007007E)

The code is has followed and failed during importation.

[DllImport("fmod_event")]
private static extern RESULT FMOD_EventSystem_Create(ref IntPtr eventsystem);

I've added the dll to the root directory of the project and set it to 'Copy if newer'. I can ensure that this dll is present in both the DEBUG and RELEASE bin folders, at the right location.

When importing the 'fmodex.dll' available via the same download file and located under /api/ I don't run into the same issue and everything works as expected with the following code:

[DllImport("fmodex")]
private static extern RESULT FMOD_System_Create(ref IntPtr system);

Both of these dlls can be seen under the same bin folder.
I have tried to use fmod_event.dll 32 and 64 without any luck.

Can anyone provide any insight into why one DLL is loading correctly, but not the other? Thanks


Solution

    • Specify .DLL as extension
    • Ensure that file exists in PATH, or specify full path in DllImport
    • Check that dependent modules do exist for this DLL. Use Dependency Walker to find if dependent DLLs exists, and are loadable.
    • check 32-bit and 64-bit issue. A process of 32-bit cannot load a 64-bit DLL, and vice versa