0

I need to take over someone else's C# code but I have never used C# before and have little experience with Visual Studio. The code fails to load some DLL. It looks something like this;

            hModule = LoadLibrary("XXX.dll");

            if (hModule == IntPtr.Zero)
            {
                MessageBox.Show("Load failed.", "Error");
                return;
            }

What are the possible causes to cause DLL to fail to load? If it is a path problem, I have placed the DLL in the same location as the executable. Do I need to manually add some references for this dll (using Visual Studio 2012)? Or could there be a problem with the dll? If so, how can this be verified?

I know this question is not suitable for Stack Overflow as it is too open-ended. But some possibilities raised can point me to the right direction. I would like to ask for some leeway before Stackoverflow members close this question or downvote it too many times. Thank you.

EDIT: After trying out the answer from Hans Passant, I got the error %1 is not a valid Win32 application. Does this mean that something is wrong with the DLL? The same DLL can work with a Visual C++ code base but not with C# code. I also noticed there are some static library .lib in the code. But C# does not take in static libraries, if I am not wrong.

guagay_wk
  • 26,337
  • 54
  • 186
  • 295

2 Answers2

2

Proper error checking and reporting is often missing in pinvoke code. You'll have to take care of it yourself, you don't have the friendly .NET exceptions anymore to keep you out of trouble. Starting point is to get the pinvoke declaration correct:

[DllImport("kernel32", SetLastError=true, CharSet = CharSet.Auto)]
static extern IntPtr LoadLibrary(string lpFileName);

With the SetLastError property crucial to get the pinvoke marshaller to obtain the Windows error code. And then to report it so it is clear why the DLL could not be loaded:

IntPtr hModule = LoadLibrary(path);
if (hModule == IntPtr.Zero) throw new System.ComponentModel.Win32Exception();

With "File not found" by far the most common mishap. Not just for that DLL, also for any dependent DLLs. Which you can only easily see by enabling loader snaps or by using the SysInternals' Process Monitor utility.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks. I got the following error `%1 is not a valid Win32 application`. Does this mean there is something wrong with the dll? The same dll can work with a Visual C++ code. But on C#, it does not work. – guagay_wk Jul 30 '14 at 06:44
  • Maybe. More likely is that there's something wrong with your C# project, running in the wrong bitness. A 64-bit process cannot load a 32-bit DLL or the other way around. Use Project + Properties, Build tab and force the Platform target on your EXE project. – Hans Passant Jul 30 '14 at 06:47
1

This is from doc:

If the string specifies a full path, the function searches only that path for the module. If the string specifies a relative path or a module name without a path, the function uses a standard search strategy to find the module; for more information, see the Remarks. If the function cannot find the module, the function fails. When specifying a path, be sure to use backslashes (), not forward slashes (/). For more information about paths, see Naming a File or Directory.

Also looks here: Return value

If the function succeeds, the return value is a handle to the module. If the function fails, the return value is NULL. To get extended error information, call GetLastError.

More info here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms684175(v=vs.85).aspx

lowselfesteemsucks
  • 825
  • 1
  • 13
  • 21