我做了一個包裝類的C DLL,所以我可以從託管代碼調用它的功能,我可以從C#WCF服務訪問它們。 Everyting似乎很好,但是在C庫中分配大量內存時。 IIS似乎並不喜歡它。它會給我一個stackoverflow例外。
問題
當C DLL分配內存。它在IIS中打破。
char stmt[163840+1]; // allocation a lot of memory
char stmt2[163840+1]; // allocation a lot of memory
IIS是否有特殊的設置,以允許更多的內存來從C模塊進行分配?
代碼,其暴露的C DLL功能
步驟:
1. use SetDllDirectory
2. LoadLibrary
3. then call my function with DLLImport
4. FreeLibrary
的NativeClassWrapper代碼(Simplefied)
[SuppressUnmanagedCodeSecurity]
public static class NativeClassWrapper
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr LoadLibrary(string hModule);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FreeLibrary(IntPtr hModule);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetDllDirectory(string lpPathName);
[DllImport("myCDll.dll", EntryPoint = "MyFunction", ExactSpelling = false, SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
public static extern int MyFunction(
);
}
C代碼
long MyFunction() {
char stmt[163840+1]; // allocation alot of memory
char stmt2[163840+1];
long lReturn = 0L;
*stmt = '\0';
*stmt2 = '\0';
return lReturn;
}
爲什麼對這個問題 – lordkain
負顯然,在你的代碼中的缺陷。你沒有顯示任何重要的代碼。因此你必須自己調試它。如果您需要幫助,請提供[mcve]和診斷。 –
感謝大衛,我更新了示例 – lordkain