使用PInvoke访问冲突

使用PInvoke访问冲突

问题描述:

大家好。



我正在使用TSAPI(alcatel)而不是C#(C / C ++工作正常),但我遇到访问冲突问题,看我的代码..



Windows PInvoke

Hello guys.

i'm working with TSAPI (alcatel) over C# (C/C++ works fine), but i'm having problem with access violation, see my code..

Windows PInvoke

[Flags()]
public enum AllocationType : uint
{
    COMMIT = 0x1000,
    RESERVE = 0x2000,
    RESET = 0x80000,
    LARGE_PAGES = 0x20000000,
    PHYSICAL = 0x400000,
    TOP_DOWN = 0x100000,
    WRITE_WATCH = 0x200000
}
[Flags()]
public enum MemoryProtection : uint
{
    EXECUTE = 0x10,
    EXECUTE_READ = 0x20,
    EXECUTE_READWRITE = 0x40,
    EXECUTE_WRITECOPY = 0x80,
    NOACCESS = 0x01,
    READONLY = 0x02,
    READWRITE = 0x04,
    WRITECOPY = 0x08,
    GUARD_Modifierflag = 0x100,
    NOCACHE_Modifierflag = 0x200,
    WRITECOMBINE_Modifierflag = 0x400
}
[DllImport("kernel32.dll", SetLastError = true)]
static extern unsafe UIntPtr VirtualAlloc(uint lpAddress, uint dwSize,
   AllocationType flAllocationType, MemoryProtection flProtect);





TSAPI PInvoke



TSAPI PInvoke

[DllImport("csta32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern unsafe int acsGetEventPoll(uint acsHandle, void* eventBuf, void *eventBufSize, void* privData, void* numEvents);





MyCode C#不安全代码



MyCode C# unsafe code

byte* eventBuf = (byte*)VirtualAlloc(0, 1024, AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE);

ushort* eventBufSize = (ushort*)VirtualAlloc(0, 2, AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE); ;
*eventBufSize = 1024;

ushort* numEvents = (ushort*)VirtualAlloc(0, 2, AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE); ;
*numEvents = 0;

void *privData = (byte*)VirtualAlloc(0, 1024, AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE);

// acsHandle is OK
acsGetEventPoll(acsHandle, eventBuf, eventBufSize, privData, numEvents); // Attempted to read or write protected memory. This is often an indication that other memory is corrupt.







一些想法? ??




Some ideia????

尝试使用Marshal函数而不是VirtualAlloc。另外,对于eventBuf和privData,使用IntPtr而不是void *。尝试使用eventBufSize和numEvents的ref ushort。



Try the Marshal functions instead of VirtualAlloc. Also, use IntPtr instead of void * for eventBuf and privData. Try a "ref ushort" for eventBufSize and numEvents.

[DllImport("csta32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern unsafe int acsGetEventPoll(uint acsHandle, IntPtr eventBuf, ref ushort eventBufSize, IntPtr privData, ref ushort numEvents);

ushort numEvents = 0;
ushort eventBufSize = 100; 
ushort privDataSize = 100;
IntPtr eventBuf = Marshal.AllocHGlobal(eventBufSize);
IntPtr privData = Marshal.AllocHGlobal(privDataSize);

int result = acsGetEventPoll(acsHandle, eventBuf, ref eventBufSize, privData, ref numEvents);

Marshal.FreeHGlobal (eventBuf);
Marshal.FreeHGlobal (privData);





我不知道eventBufSize和privDataSize应该有多大。



I have no idea how large eventBufSize and privDataSize should be.