Re: How to pass variant (array of bytes) to winsock.ocx
Kurt,
Thanks very much for your correct suggestion of using vbbyte = 17. I
incorrectly used varType.iVal=(VT_ARRAY|VT_I1) where the correct value is
varType.iVal=(VT_ARRAY | VT_UI1) which is the same as varType.iVal=(8192 |
17). I knew it was something simple that I was overlooking. In case anyone
was interested, here is the test routine I used (no error checking shown) to
display the received text:
void CWme_rwDlg::OnDataArrivalRxwfsock( long bytesTotal )
{
COleVariant var, varType, varMaxLen(bytesTotal);
VariantInit(&varType);
VariantInit(&var);
varType.iVal = (VT_ARRAY | VT_UI1);
varType.vt = VT_I2;
m_rxwfsock.GetData( &var, varType, varMaxLen); // read the data
usigned long = nbytes = var.parray->rgsabound[0].cElements; // bytes rcvd
BYTE *pbuf = new BYTE[nbytes+1]; // create a buffer for
bytes
void *prbuf;
SafeArrayAccessData(var.parray, &prbuf); // get pointer to rcv
buffer
memcpy(pbuf, prbuf, nbytes);
SafeArrayUnaccessData(var.parray);
pbuf[nbytes] = NULL; // add term null
CString str;
str.Format("Rcvd data: %s", pbuf);
AfxMessageBox(str); // display the message
delete pbuf;
}
It's funny because after all this, I probably will use the CAsyncSocket
rather then the winsock.ocx. I hate all this variant stuff and the
suggestions to use CAsyncSocket seems like the better choice.
Thanks again Kurt and Joseph for the responses and help.
Monty
"Kurt" wrote:
Try setting the type variant like below. MSDN says vbarray=8192 and vbbyte
=17 see below.
varType.iVal = 8192|17;
varType.vt = VT_I2;
Settings
The settings for type are:
Description Constant
Byte vbByte
Integer vbInteger
Long vbLong
Single vbSingle
Double vbDouble
Currency vbCurrency
Date vbDate
Boolean vbBoolean
SCODE vbError
String vbString
Byte Array vbArray + vbByte
Constant Value Description
vbEmpty 0 Uninitialized (default)
vbNull 1 Contains no valid data
vbInteger 2 Integer subtype
vbLong 3 Long subtype
vbSingle 4 Single subtype
vbSingle 5 Double subtype
vbCurrency 6 Currency subtype
vbDate 7 Date subtype
vbString 8 String subtype
vbObject 9 Object
vbError 10 Error subtype
vbBoolean 11 Boolean subtype
vbVariant 12 Variant (used only for arrays of variants)
vbDataObject 13 Data access object
vbDecimal 14 Decimal subtype
vbByte 17 Byte subtype
vbArray 8192 Array
Requirements
Version 2
"mxa" <mxa@discussions.microsoft.com> wrote in message
news:E4B08E25-61AB-464C-9115-409B89F1BAC3@microsoft.com...
Well, like I said in my first post, the GetData seems to work fine when I
define the variant as a BSTR (see BSTR example below), however when I
define
the variants (var and varType) to retrieve the data as a array of bytes,
the
call to the GetData NEVER RETURNS- therefore i can't read the data to
answer
your question. Here is the code again that I use for an array of bytes
that
does not work:
void CWme_rwDlg::OnDataArrivalRxwfsock( long bytesTotal)
{
COleVariant var, varType, varLth(bytesTotal)
varType.iVal = VT_ARRAY | VT_I1);
varType.vt = VT_I2;
// GetData(VARIANT *data,
// const VARIANT &type,
// const VARIANT &maxLen)
GetData( &var, varType, varLth); // Winsock control getdata
AfxMessageBox("Get Data Succeeded");
}
I never get to the AfxMessageBox statement after the call to GetData. If
you see below, I show a code snipet that uses BSTR's to retrieve the data
and
that works, however I needed a byte array.
Thanks for your suggestion on using CAsynchSocket and that's probably the
way I've been leaning towards. I just started on the winsock.ocx path
(I've
used winsock in VB in the past) and couldn't understand why it would not
work
with byte arrays but work fine with BSTR's. I figured it was easier to
fix
a simple mistake in my code, then re-do my code using CAsyncSocket - but
it
looks like that's the way I'm going.
"Joseph M. Newcomer" wrote:
Do not use CSocket. Use CAsyncSocket. Then define what you mean by
"can't receive the
data".
joe
On Sat, 10 Jun 2006 08:26:01 -0700, mxa <mxa@discussions.microsoft.com>
wrote:
Kurt, thanks for the suggestion. It's exactly what I've been doing
since I
didn't get a response from anyone, and after many failed attempts using
the
OCX. It's just funny that I can send data fine, but I can't receive
the
data.
Thanks for your response and suggestion.
"Kurt" wrote:
Why not use one of the MFC classes like CSocket instead of the
winsock.ocx?
"mxa" <mxa@discussions.microsoft.com> wrote in message
news:AEDFF613-F746-4962-9533-46B3C2D42DEA@microsoft.com...
I'm attempting to use Winsock.ocx in a Visual Studio 6.0, C++
application
and
I have a question on how to pass variants to a winsock GetData
function
defined as follows: GetData(VARIANT *data, const VARIANT &type,
const
VARIANT
&maxLen).
I have no problems with other functions, but this particular
function
never
returns if I make the call with the VARIANT *data defined as a byte
array.
My question, is how would I go about getting the data as an Array of
Bytes?
What has worked is when I call the GetData as follows defining a
BSTR
type:
void CWme_rwDlg::OnDataArrivalRxwfsock( long bytesTotal)
{
COleVariant var, varType, varLth(bytesTotal)
varType.iVal = VT_BSTR;
varType.vt = VT_I2;
GetData( &var, varType, varLth); // Winsock control
getdata
function
Cstring str = (BSTR) var.bstrVal;
AfxMessageBox(str);
}
The above code works when sending a BSTR. However, I want to send
an
array
of bytes and have attempted to the following:
void CWme_rwDlg::OnDataArrivalRxwfsock( long bytesTotal)
{
COleVariant var, varType, varLth(bytesTotal)
varType.iVal = VT_ARRAY | VT_I1);
varType.vt = VT_I2;
GetData( &var, varType, varLth); // Winsock control
getdata
function
AfxMessageBox("Get Data Succeeded");
}
The number of bytes transmitted matches the number of bytes
received,
however when I call the GetData function, it never returns. I'm
assuming
I'm defining something incorrectly and was wondering if someone can
provide
me with some advice?
Expand AllCollapse All
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm