(c to vb) 1-byte aligned struct in VB?

Started by
4 comments, last by Extrarius 17 years, 8 months ago
I'm trying to port an IOCTL sample over from C to VB, but i'm hence my little knowledge of C, i got stuck. (it's the IOCTL sample from PPJoy to simulate a joystick) This is my (current) problem:

'#pragma pack(push,1)        /* All fields in structure must be byte aligned. */
'typedef struct
'{
' unsigned long  Signature;              /* Signature to identify packet to PPJoy IOCTL */
' char           NumAnalog;              /* Num of analog values we pass */
' long           Analog[NUM_ANALOG];     /* Analog values */
' char           NumDigital;             /* Num of digital values we pass */
' char           Digital[NUM_DIGITAL];   /* Digital values */
'}   JOYSTICK_STATE;
'#pragma pack(pop)
can someone trie to explain to me what i should do to create an equalent in VB? After the struct is set etc. it's used like this:

DeviceIoControl(h,IOCTL_PPORTJOY_SET_STATE,&JoyState,sizeof(JoyState),NULL,0,&RetSize,NULL)
Advertisement
VB.NET?
VB6. I created a type like this:

type JOYSTICK_STATE
NumAnalog as byte
Analog(NUM_ANALOG) as long
NumDigital as byte
Digital(NUM_DIGITAL) as byte
end type

and i checked, it's the same size as the C struct.
Any ideas?
Try this:

Public Type JOYSTICK_STATE    Signature As Long    NumAnalog As Byte    Analog(NUM_ANALOG) As Long    NumDigital As Byte    Digital(NUM_DIGITAL) As ByteEnd TypePublic Declare Function DeviceIoControl Lib "kernel32.dll" _        (ByRef hDevice As Long, _        ByVal dwIoControlCode As Long, _        ByRef lpInBuffer As JOYSTICK_STATE, _        ByVal nInBufferSize As Long, _        ByRef lpOutBuffer As Long, _        ByVal nOutBufferSize As Long, _        ByRef lpBytesReturned As Long, _        ByRef lpOverlapped As Long) As Long' and this to call itDeviceIoControl(h, IOCTL_PPORTJOY_SET_STATE, JoyState, Len(JoyState), 0, 0, RetSize, 0)
hmm, still doesn't work. i'm beginning to think something else is wrong, maybe the part where i get the handle (C version above, VB version below):
'DevName= "\\\\.\\PPJoyIOCTL1";DevName = "\\.\PPJoyIOCTL1"'h= CreateFile(DevName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);h = CreateFile(DevName, GENERIC_WRITE, FILE_SHARE_WRITE, ByVal CLng(0), OPEN_EXISTING, 0, ByVal CLng(0))
i do get a handle in return, so i think it's weird if this is why it fails.
As allways, any help is greatly apreciated!

[Edited by - -Fruz- on July 29, 2006 7:29:31 PM]
Why do you need to interface with the hardware directly? Reading a joystick is much easier using joyGetPosEx

If you have to use direct hardware interfacing, you'd do it like ronkfist said except that I think the handle should by ByVal and not ByRef - ByRef is only for things passed as pointers, and the handle is not passed as a pointer (even though it IS a pointer). VB6 comes with a tool that has the prototypes for everything, and you should check that, because I'm certainly not sure
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk

This topic is closed to new replies.

Advertisement