
Passing parameters from embedded VB to embedded VC++
I am trying to do something which ought to be very simple indeed, but
is causing a huge headache. Hopefully someone might have been here
before and may be able to help.
I'm trying to pass a BYTE array from eVB to a eVC++ DLL. The bytes
are read in from a PCMCIA card, then passed over. However when I
examine what gets passed, numbers between 0->129 are fine and numbers
Quote:
> 156 are fine. Numbers in between this get mangled, seemingly with
no relation to the number they become on the C side, but consistently
to the same number. The code below is representative of the many
attempts I've had (might not be perfect here). I print dat() against
what is returned in the string to see the difference. As an example,
when 156 is in the byte array, it is incorrectly reported as 83 on the
C++ side (via the returned string). I've tried using various types
(ints, strings) all with no avail.
Any comments or wisdom gratefully accepted, I'm all out of ideas!
Thanks
Dave
VB code
' VB function declaration
Declare Function DecodeMessage Lib "Decode" (ByRef numBytes As
Variant, ByRef byteArray As Variant, ByVal resultStr As String) As
Integer
DIM dat() as Byte
DIM byteArray() as Byte
' Allocate space for arrays...
' Read data from card
For i = 0 To numBytes - 1
dat(i) = MSComm1.Input
byteArray(i) = dat(i) ' More complicated than this in reality,
but this is basically what happens
Next i
result = DecodeMessage(numBytes, byteArray(0), OutString)
VC++ code
extern "C" __declspec(dllexport) int DecodeMessage(VARIANT*
numBytesVariant, VARIANT* myData, LPTSTR resultStr);
int DecodeMessage(VARIANT* numBytesVariant, VARIANT* myData, LPTSTR
resultStr)
{
CString temp; //
int numBytes = numBytesVariant->iVal;
AFX_MANAGE_STATE(AfxGetStaticModuleState());
BYTE* byteData = new BYTE[numBytes];
for (int i=0; i<numBytes; i++)
{
CString temp2; //
byteData[i] = BYTE(*(myData->pbVal));
temp2.Format(_T("%d, "), byteData[i]); //
temp = temp + temp2; //
myData++;
}
for (i=0; i<temp.GetLength(); i++) //
resultStr[i] = (char)temp.GetAt(i);//
delete byteData;
return (myData->vt);
Quote:
}