Need help for translating a c++ function-call to c#

C

Christian Stelte

Hi!

I need the serial number of a pda. The manufacturer send me a c sourcecode,
but I could not translate it at all to c#. Could anyone help?

Original source:
-------------------------------------------
#define IOCTL_CODE_HUW 0xc33
// HUW_IO-Control: further specification over sub- commands
#define HUW_IOCTL_KEY 0x6157
// key for detection of unacceptable IOCTL- calls
#define SUBCMD_LOAD_INDIVIDUAL_DATA 0x25
// read individual device infos
#define SUBCMD_GET_FLASH_NR 0x4e
// read flash- ID
#define HUW_IOCTL(Buffer, pReturnSize) KernelIoControl(IOCTL_CODE_HUW,
Buffer, sizeof(Buffer), Buffer, sizeof(Buffer), pReturnSize)

extern "C" BOOL KernelIoControl(DWORD dwIoControlCode, LPVOID lpInBuf, DWORD
nInBufSize, LPVOID lpOutBuf, DWORD nOutBufSize, LPDWORD lpBytesReturned);

INDIVIDUAL_GLOBALS individual_data;

typedef struct _INDIVIDUAL_GLOBALS
{
ULONG CheckSum;
UCHAR SerialNr[12]; // Serialnumber of the pda
} INDIVIDUAL_GLOBALS, *PINDIVIDUAL_GLOBALS;

//--- snip ---

int i;
TCHAR szSerialNr[32];
DWORD InBuf[6]; // for IOCTL-Funktion

//read the serial number:
InBuf[0] = HUW_IOCTL_KEY;
InBuf[1] = SUBCMD_LOAD_INDIVIDUAL_DATA;
InBuf[2] = (DWORD) &individual_data; // copy data int this structure
InBuf[3] = sizeof (individual_data);

if (HUW_IOCTL(InBuf, NULL) != TRUE) // Is this IOCTL not supported!
{
MessageBox(hWnd, L"Function not supported!", L"", MB_OK);
break;
}

for (i=0; i < 12; i++)
szSerialNr = (TCHAR)individual_data.SerialNr; // ascii -> wcstring

szSerialNr[12] = TEXT('\0'); // string termination
-------------------------------------------

And now my:
-------------------------------------------
private static UInt32 IOCTL_CODE_HUW = 0xc33;
private static UInt32 HUW_IOCTL_KEY = 0x6157;
private static UInt32 SUBCMD_LOAD_INDIVIDUAL_DATA = 0x25;
private static UInt32 SUBCMD_GET_FLASH_NR = 0x4e;

//#define HUW_IOCTL(Buffer, pReturnSize) KernelIoControl(IOCTL_CODE_HUW,
Buffer, sizeof(Buffer), Buffer, sizeof(Buffer), pReturnSize)
// I did now know if this is possible in c#. I use the function direct in
the if-statement below...

[DllImport("coredll.dll", SetLastError = true)]
private static extern bool KernelIoControl(
UInt32 dwIoControlCode,
IntPtr lpInBuf,
UInt32 nInBufSize,
IntPtr lpOutBuf,
UInt32 nOutBufSize,
ref UInt32 lpBytesReturned);

struct INDIVIDUAL_GLOBALS
{
UInt32 CheckSum;
byte[] SerialNr;
}

public static string GetDeviceID()
{
INDIVIDUAL_GLOBALS individual_data;
UInt32 i;
char[] szSerialNr = new char[32]; // char[] correct? I need a Ascii to Ansi
conversion, I suppose?
UInt32[] InBuf = new UInt32[6];

InBuf[0] = HUW_IOCTL_KEY;
InBuf[1] = SUBCMD_LOAD_INDIVIDUAL_DATA;
InBuf[2] = ?? // How do I create this pointer? (DWORD) &individual_data;
InBuf[3] = ?? // sizeof (individual_data) is unsafe code. And this struct
didn't have a .lenght() function.

if (!KernelIoControl(IOCTL_CODE_HUW, ???, (UInt32)InBuf.Length, ???,
(UInt32)InBuf.Length, ref i))
// The second and fourth parameter need a IntPtr. IntBuf[] is a UINT32[].
How do I create this pointer?
{
MessageBox.Show("Function not supported!");
return "Error";
}

// This will be the next problems: Convert Ascii to Ansi and achar-array to
astring

// I have not translated until now
//for (i=0; i < 12; i++)
//szSerialNr = (TCHAR)individual_data.SerialNr; // ascii -> wcstring
//
//szSerialNr[12] = TEXT('\0'); // string termination
//
}
-------------------------------------------

Please have a look at it!

Thanks!

Chris
 
E

Ed Kaim [MSFT]

Try the code at
http://samples.gotdotnet.com/quicks...iceid/deviceid.src&file=cs\Deviceid.cs&font=3.

Christian Stelte said:
Hi!

I need the serial number of a pda. The manufacturer send me a c
sourcecode,
but I could not translate it at all to c#. Could anyone help?

Original source:
-------------------------------------------
#define IOCTL_CODE_HUW 0xc33
// HUW_IO-Control: further specification over sub- commands
#define HUW_IOCTL_KEY 0x6157
// key for detection of unacceptable IOCTL- calls
#define SUBCMD_LOAD_INDIVIDUAL_DATA 0x25
// read individual device infos
#define SUBCMD_GET_FLASH_NR 0x4e
// read flash- ID
#define HUW_IOCTL(Buffer, pReturnSize) KernelIoControl(IOCTL_CODE_HUW,
Buffer, sizeof(Buffer), Buffer, sizeof(Buffer), pReturnSize)

extern "C" BOOL KernelIoControl(DWORD dwIoControlCode, LPVOID lpInBuf,
DWORD
nInBufSize, LPVOID lpOutBuf, DWORD nOutBufSize, LPDWORD lpBytesReturned);

INDIVIDUAL_GLOBALS individual_data;

typedef struct _INDIVIDUAL_GLOBALS
{
ULONG CheckSum;
UCHAR SerialNr[12]; // Serialnumber of the pda
} INDIVIDUAL_GLOBALS, *PINDIVIDUAL_GLOBALS;

//--- snip ---

int i;
TCHAR szSerialNr[32];
DWORD InBuf[6]; // for IOCTL-Funktion

//read the serial number:
InBuf[0] = HUW_IOCTL_KEY;
InBuf[1] = SUBCMD_LOAD_INDIVIDUAL_DATA;
InBuf[2] = (DWORD) &individual_data; // copy data int this structure
InBuf[3] = sizeof (individual_data);

if (HUW_IOCTL(InBuf, NULL) != TRUE) // Is this IOCTL not supported!
{
MessageBox(hWnd, L"Function not supported!", L"", MB_OK);
break;
}

for (i=0; i < 12; i++)
szSerialNr = (TCHAR)individual_data.SerialNr; // ascii -> wcstring

szSerialNr[12] = TEXT('\0'); // string termination
-------------------------------------------

And now my:
-------------------------------------------
private static UInt32 IOCTL_CODE_HUW = 0xc33;
private static UInt32 HUW_IOCTL_KEY = 0x6157;
private static UInt32 SUBCMD_LOAD_INDIVIDUAL_DATA = 0x25;
private static UInt32 SUBCMD_GET_FLASH_NR = 0x4e;

//#define HUW_IOCTL(Buffer, pReturnSize) KernelIoControl(IOCTL_CODE_HUW,
Buffer, sizeof(Buffer), Buffer, sizeof(Buffer), pReturnSize)
// I did now know if this is possible in c#. I use the function direct in
the if-statement below...

[DllImport("coredll.dll", SetLastError = true)]
private static extern bool KernelIoControl(
UInt32 dwIoControlCode,
IntPtr lpInBuf,
UInt32 nInBufSize,
IntPtr lpOutBuf,
UInt32 nOutBufSize,
ref UInt32 lpBytesReturned);

struct INDIVIDUAL_GLOBALS
{
UInt32 CheckSum;
byte[] SerialNr;
}

public static string GetDeviceID()
{
INDIVIDUAL_GLOBALS individual_data;
UInt32 i;
char[] szSerialNr = new char[32]; // char[] correct? I need a Ascii to
Ansi
conversion, I suppose?
UInt32[] InBuf = new UInt32[6];

InBuf[0] = HUW_IOCTL_KEY;
InBuf[1] = SUBCMD_LOAD_INDIVIDUAL_DATA;
InBuf[2] = ?? // How do I create this pointer? (DWORD) &individual_data;
InBuf[3] = ?? // sizeof (individual_data) is unsafe code. And this struct
didn't have a .lenght() function.

if (!KernelIoControl(IOCTL_CODE_HUW, ???, (UInt32)InBuf.Length, ???,
(UInt32)InBuf.Length, ref i))
// The second and fourth parameter need a IntPtr. IntBuf[] is a UINT32[].
How do I create this pointer?
{
MessageBox.Show("Function not supported!");
return "Error";
}

// This will be the next problems: Convert Ascii to Ansi and achar-array
to
astring

// I have not translated until now
//for (i=0; i < 12; i++)
//szSerialNr = (TCHAR)individual_data.SerialNr; // ascii -> wcstring
//
//szSerialNr[12] = TEXT('\0'); // string termination
//
}
-------------------------------------------

Please have a look at it!

Thanks!

Chris
 
C

Christian Stelte

Ed Kaim said:

Hello Ed!

Nice try...
:)
....but this pad of the manufacturer (skeye.com) does not support the
DeviceID. I do not know why and he also didn't know. :)
The 'IOCTL_HAL_GET_DEVICEID' returns the 'ERROR_NOT_SUPPORTED'. So he send
me this sourcecode...

I came from VB to C# so I've not lots of experience in pointer-conversation.
Is there no way by using this 'marshaling-stuff'? I've read a little bit in
this, but it seems very complex to me.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top