how to send data to c++ function from dotnet?

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

The Thinker

Guest
I have a c++ function below and vb.net code Iam using below but the vb.net code being used crashs with a bsod but normally it does not so I think I might have improperly formatted data to send to the c++ from vb.net. Heres the c++ code:

STDMETHODIMP CHIDDevice::QueueInputReport( SAFEARRAY* psaInputReport, UINT timeoutDuration )
/*++
Routine Description:
Queues additional input reports

Arguments:
IdleTimeout - used to set the value of the log level

Return value:
S_OK
--*/
{
VARIANT HUGEP* pArrayData = NULL;
ULONG cbData = 0;
LONG lLBound = 0;
LONG lUBound = 0;
HRESULT hr = S_OK;
HID_INPUT_REPORT inputReport;
BOOLEAN result = TRUE;

// Initialize Structure
inputReport.timeout = timeoutDuration;
inputReport.cbInputReport = 0;
inputReport.pbInputReport = NULL;

// Get SAFEARRAY size
IfFailHrGo(SafeArrayGetLBound(psaInputReport, 1, &lLBound));
IfFailHrGo(SafeArrayGetUBound(psaInputReport, 1, &lUBound));
IfFalseHrGo(lUBound > lLBound, E_UNEXPECTED);
cbData = lUBound - lLBound + 1;

inputReport.pbInputReport = (BYTE*)CoTaskMemAlloc( cbData * sizeof(BYTE) );

// If the memory Allocation fails, then fail and exit
if( inputReport.pbInputReport == NULL )
{
hr = E_UNEXPECTED;
goto Exit;
}

IfFailHrGo( SafeArrayAccessData( psaInputReport,
(void HUGEP**)&pArrayData ) );

// Step through the SAFEARRAY and populating only BYTE input report
// and bail out if the type is not correct
for( LONG i = lLBound; i <= lUBound; ++i )
{
if (pArrayData.vt == VT_UI1)
{
inputReport.pbInputReport[i-lLBound] = pArrayData.bVal;
}
else
{
hr = E_FAIL;

goto Exit;
}
}

inputReport.cbInputReport = cbData;

// Add the report to the input queue (does a shallow copy so no need to free the array data)
result = m_InputReportQueue.insert( inputReport );
if (result == FALSE)
{
hr = E_FAIL;
}

Exit:
SafeArrayUnaccessData(psaInputReport);
if( FAILED(hr) )
{
CoTaskMemFree(inputReport.pbInputReport);
}
return hr;
}

Here is the vb.net accessing queueinputreport:

<MarshalAs(UnmanagedType.SafeArray, SafeArraySubType:=VarEnum.VT_UI1)> Dim InputReport1(4) As Object
...... other code in-between here.
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(x(usernum))
InputReport1(2) = CByte(y(usernum))
InputReport1(3) = CByte(0)
InputReport1(4) = CByte(0)
End If
GenericHIDDev(usernum).QueueInputReport(InputReport1, 10)
GenericHIDDev(usernum).StartProcessing()

Is their anything anyone can see Iam doing wrong? Edit: please ignore x,y and replace them with static values.
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