I'm trying to translate some Code written in C to C# (Compact Framework 2.0) (It's for a Windows CE Device with an RFID Reader).
And in C this system call works fine, but it does not work in C#:
Code In C
HANDLE m_Power =NULL; //<-- This HANDLE is set correctly before call "ControlDevice"
int ControlDevice(int device, BYTE power_state)
{
bool bRet;
DWORD tcBuffer2,dwBytesReturned;
unsigned int a = sizeof(power_state);
bRet=DeviceIoControl( m_Power,
device,
&power_state,
sizeof(power_state),
(PBYTE)&tcBuffer2,
sizeof(DWORD),
&dwBytesReturned,
NULL);
if(bRet)
return 1;
else
return -1;
}
CODE IN C#
int ControlDevice(int device, byte[] power_state)
{
try
{
bool bRet = false;
int bytesReturned = 0;
byte[] tcBuffer2 = new byte[1];
tcBuffer2[0] = 0;
bRet = Device_WinApi.DeviceIoControlCE(m_Power,
device,
power_state,
(int)Marshal.SizeOf(power_state),
tcBuffer2,
(int)Marshal.SizeOf(tcBuffer2),
out bytesReturned,
IntPtr.Zero);
if (bRet)
return 1;
else
{
int LastError = Device_WinApi.GetLastError();
return -1;
}
}
catch (Exception e)
{
return -1;
}
}
The DLL is imported in this way:
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
internal static extern bool DeviceIoControlCE(
IntPtr hDevice,
long dwIoControlCode,
byte[] lpInBuffer,
int nInBufferSize,
byte[] lpOutBuffer,
int nOutBufferSize,
out int lpBytesReturned,
IntPtr lpOverlapped);
In the current implementation in C#, bRet = false, and I should be true, as this is the value obtained in C
this return zero too:
int LastError = Device_WinApi.GetLastError()
Any help will be really appreciated!
Thank you advance for your time!!