2014-03-13 33 views
7

我將在asp.net web應用程序中使用kernel32 dll。這是代碼: // DllGetClassObject函數指針簽名 private delegate int DllGetClassObject(ref Guid ClassId,ref Guid InterfaceId,[Out,MarshalAs(UnmanagedType.Interface)] out object ppunk);來自kernel32.dll的函數LoadLibrary在Asp web應用程序中返回零。

//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(); 
} 

,當我打電話給我的功能GetTheDllHandle,該dllHandle返回零

有沒有人在那裏做一些類似的問題?還是有人有任何建議?

+0

可能是該DLL不存在,你認爲它沒有,可能是配股,可能是別的東西完全。調用GetLastError來找出。 –

回答

8

加載DLLs時返回0是由於幾個原因,比如,DLL不在指定路徑上,或DLL不支持平臺或依賴DLL在構建原生DLL之前未加載。這樣你就可以通過一套真正追蹤這些錯誤的SetLastError屬性

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 ! 
} 
相關問題