Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
[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
C#
[DllImport("csta32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern unsafe int acsGetEventPoll(uint acsHandle, void* eventBuf, void *eventBufSize, void* privData, void* numEvents);


MyCode C# unsafe code
C#
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????
Posted
Comments
nv3 28-Nov-14 8:04am    
Have you checked the return values of your calls to VirtualAlloc? If any of these calls return 0, that would explain your access vialoation in acsGetEventPoll.
theafien 28-Nov-14 12:25pm    
yes, VirtualAlloc correctly allocates memory
KarstenK 28-Nov-14 10:16am    
Are the (in) buffer big enough?

why isnt privData set with a proper value or null?
theafien 28-Nov-14 12:24pm    
all buffer is sufficient
Bernhard Hiller 28-Nov-14 10:50am    
Did you check other CallingConvertions than CallingConvention.StdCall?
And that *numEvents = 0; looks somehow odd. Is it actually an "out int numEvents"?

1 solution

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.

C#
[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);


I have no idea how large eventBufSize and privDataSize should be.
 
Share this answer
 
Comments
theafien 28-Nov-14 14:23pm    
bling, the error is in buffer size, the minimum buffer size is 2333 bytes, and i allocating only 1024 bytes.

Thanks!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900