LoadLibrary function from kernel32.dll returns zero in Asp web application

I will use kernel32 dll in asp.net web application. This is the code: // Signature of the function pointer DllGetClassObject private delegate int DllGetClassObject (ref Guid ClassId, ref Guid InterfaceId, [Out, MarshalAs (UnmanagedType.Interface)] out object ppunk);

//Some win32 methods to load\unload dlls and get a function pointer private class Win32NativeMethods { [DllImport("kernel32.dll", CharSet=CharSet.Ansi)] public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName); [DllImport("kernel32.dll")] public static extern bool FreeLibrary(IntPtr hModule); [DllImport("kernel32.dll")] public static extern IntPtr LoadLibrary(string lpFileName); } public string GetTheDllHandle (dllName) { IntPtr dllHandle = Win32NativeMethods.LoadLibrary(dllName); // the dllHandle=IntPtr.Zero return dllHandle.ToString(); } 

The problem is that when I call my GetTheDllHandle function, dllHandle returns zero

Has anyone done something similar there? Or does anyone have any suggestions?

+7
source share
1 answer

A return of 0 when loading the DLL is due to several reasons, such as a DLL, not in the specified path, or the DLL is not supported with the platform or dependent dlls that do not load before you create your own DLL. therefore, you can track these errors by setting true for the SetLastError property

 DllImport("kernel32.dll", EntryPoint = "LoadLibrary", SetLastError = true)] public static extern IntPtr LoadLibrary(string lpFileName); public string GetTheDllHandle (dllName) { IntPtr dllHandle = Win32NativeMethods.LoadLibrary(dllName); // the dllHandle=IntPtr.Zero if (dllHandle == IntPtr.Zero) return Marshal.GetLastWin32Error().ToString(); // Error Code while loading DLL else return dllHandle.ToString(); // Loading done ! } 
+8
source share

All Articles