1

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!!

  • Where did you get your DllImport line? It's been quite a while since I used DllImport so things may have changed, but I'm not sure passing an unpinned byte[] is the best idea for a pointer. – Joachim Isaksson Sep 20 '12 at 15:58
  • To be honest, I don't remember, I was searching examples in google and this was the only one that does not crash. I tried with IntPtr, but I don't have any knowledge about data marshalling between C# and unManaged code, so I'm driving blind!. I will check for other options to pass data here. Thank you so much!! – Fernando Sosa Sep 20 '12 at 20:36

0 Answers0