我做了一个包装类的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