Error when passing structure as an Win API argument 
Author Message
 Error when passing structure as an Win API argument

Dear all,
I have problem in passing structure as Win API argument.
I defined a structure as follow:

<StructLayout(LayoutKind.Sequential)> Private Structure
INPUT_TYPE
    Dim dwType As Integer
    Dim xi() As Byte ' xi(0 to 23) as byte
End Structure

and declared a Win API function :
Private Declare Function SendInput Lib "user32.dll"
(ByVal nInputs As Integer, ByRef pInputs As INPUT_TYPE,
ByVal cbSize As Integer) As Integer

Then in a subprocedure I stated dimension the 'xi' member
of the structure:
Dim myVar(0) As INPUT_TYPE
....
Redim myVar(0).xi(23)

But I get an exception when I invoke the Win API function:
SendInput(1, myVar(0), Len(myVar(0)))

The exception says:
"Additional information: Can not marshal field xi of type
INPUT_TYPE: This type can not be marshaled as a structure
field."

How to pass this kind of structure correctly?
Thanks in advance for your help.



Sun, 05 Jun 2005 17:19:03 GMT  
 Error when passing structure as an Win API argument
You can try the following:
<StructLayout(LayoutKind.Sequential)> _
Private Structure INPUT_TYPE
  Dim dwType As Integer
  <MarshalAs(UnmanagedType.ByValArray, SizeConst:=24)> Dim xi() As Byte
End Structure

Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As
Integer, ByVal pInputs() As INPUT_TYPE, ByVal cbSize As Integer) As Integer

Dim inputs(0) As INPUT_TYPE
ReDim inputs(0).xi(23)
..
..
..
SendInput(1, inputs, Marshal.SizeOf(GetType(INPUT_TYPE)))

Or you can use the union as it was designed without the need for the byte
array:
<StructLayout(LayoutKind.Explicit, Size:=28)> _
Private Structure INPUT_TYPE
  <FieldOffset(0)> Public dwType As Integer
  <FieldOffset(4)> Public mi As MOUSEINPUT
  <FieldOffset(4)> Public ki As KEYBDINPUT
  <FieldOffset(4)> Public hi As HARDWAREINPUT
End Structure

<StructLayout(LayoutKind.Sequential)> _
Private Structure MOUSEINPUT
  Public dx As Integer
  Public dy As Integer
  Public mouseData As Integer
  Public dwFlags As Integer
  Public time As Integer
  Public dwExtraInfo As IntPtr
End Structure

<StructLayout(LayoutKind.Sequential)> _
Private Structure KEYBDINPUT
  Public wVk As Short
  Public wScan As Short
  Public dwFlags As Integer
  Public time As Integer
  Public dwExtraInfo As IntPtr
End Structure

<StructLayout(LayoutKind.Sequential)> _
Private Structure HARDWAREINPUT
  Public uMsg As Integer
  Public wParamL As Short
  Public wParamH As Short
End Structure


Quote:
> Dear all,
> I have problem in passing structure as Win API argument.
> I defined a structure as follow:

> <StructLayout(LayoutKind.Sequential)> Private Structure
> INPUT_TYPE
>     Dim dwType As Integer
>     Dim xi() As Byte ' xi(0 to 23) as byte
> End Structure

> and declared a Win API function :
> Private Declare Function SendInput Lib "user32.dll"
> (ByVal nInputs As Integer, ByRef pInputs As INPUT_TYPE,
> ByVal cbSize As Integer) As Integer

> Then in a subprocedure I stated dimension the 'xi' member
> of the structure:
> Dim myVar(0) As INPUT_TYPE
> ....
> Redim myVar(0).xi(23)

> But I get an exception when I invoke the Win API function:
> SendInput(1, myVar(0), Len(myVar(0)))

> The exception says:
> "Additional information: Can not marshal field xi of type
> INPUT_TYPE: This type can not be marshaled as a structure
> field."

> How to pass this kind of structure correctly?
> Thanks in advance for your help.



Sun, 05 Jun 2005 21:09:23 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Exception when passing structure as a Win API parameter

2. WIN 2k API vs WIN 98/WIN NT API - Help needed

3. Converting between IntPtr and structure from Win API

4. Declaration of non Win API call and structures

5. Getting at data in structure returned as a long pointer (Win API)

6. HELP setting structures to NULL in WIN API's

7. Declaration of non Win API call and structures

8. Passing Structures to API Calls

9. How to pass an array of structures in an API call

10. WScript.Arguments won't parse double-quotes within arguments

11. Passing a structure to an API

12. Passing VB Structures to API Functions.

 

 
Powered by phpBB® Forum Software