how to pass a safearray of type variant to unmanaged code?

  • Thread starter Thread starter The Thinker
  • Start date Start date
T

The Thinker

Guest
I need to pass a variant that holds byte values to unmanaged code. But here is what I know the following link describes the safearrray structure:


http://msdn.microsoft.com/en-us/library/windows/desktop/ms221482(v=vs.85).aspx


I know when I run the unmanaged c++ code with the vbscript it works correctly and gives me 2194 (0x892 in hex) for the type of the safe array and works. On the .net side I get 2176 (0x880 in hex) though and thats when the array crashs my program. however, when I run from vb.net something gets passed wrong to the unmanaged side or I have my variable dimmed incorrectly or the byte content is incorrect. Hum something does not seem right here? :/

So how do I pass a variant array holding byte values correctly? I can know if it is passed correctly because I can see the array type by putting a messagebox in the unmanaged c++ code.

Heres my relevant vb.net code so far:

<MarshalAs(UnmanagedType.SafeArray, SafeArraySubType:=VarEnum.VT_UI1)> Dim InputReport1(4)

Public Function sendinputreport(xcoord As Integer, ycoord As Integer, leftclick As Boolean, rightclick As Boolean) As Boolean
device report to send to computer (x,y coordinates, left or right clicks, and scrolling).
file contents:
number of devices
x for dev1
y for dev1
left click for dev1
x for dev2
y for dev2
left click for dev2

Dim x(40), y(40) As Integer
x(usernum) = xcoord
y(usernum) = ycoord
MsgBox(x(usernum))

If x(usernum) <= 255 And y(usernum) <= 255 Then

InputReport1(0) = CByte(0)
InputReport1(1) = CByte(100)
InputReport1(2) = CByte(100)
InputReport1(3) = CByte(0)
InputReport1(4) = CByte(0)
GenericHIDDev(usernum).QueueInputReport(InputReport1, 10)
MsgBox("start processing")
GenericHIDDev(usernum).StartProcessing()
End If

sendinputreport = True
End Function

Notes: i have tried to dim as byte which does not work in this instance. I think its the dim statement is incorrect or visual studio created the RCW(runtime callable wrapper) incorrectly. Because it likes that it has the bytes inside the array but not the array type.

Heres the idl file for the c++ dll it might help in passing to the safearray:

HRESULT QueueInputReport( [in]SAFEARRAY(VARIANT) psaInputReport, [in]UINT timeoutDuration );
[
id(6),
helpstring("Method: Starts processing Queued Input reports")
]


Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.co.nr

Continue reading...
 
Back
Top