>I want to know why I have to send and receive message like this, and
>why I can't just set the vt, sent instruction and recieve message through
>test vt and change the last part of that variable.

That's a very good question. What they were attempting to do here (the
people who designed VARIANT) is allow two applications, most likely written
in different languages (or designed for different languages) to communicate
using a common mechanism. If they were both VC++, or both VB, then they
could simply use their own language and each would understand the others.
Obviously VB can't understand VC++ directly, and the same is true the other
way.

So, they came up with a way that a value could be passed from one to the
other in a language-neutral way. Thus, some conversions are necessary to get
from the native language to the neutral language.

Now, you must realise that the MSComm control (an OCX file), like most OCX
controls, was specifically designed for VB. There are actually two ways to
use the Output and Input members; one is with a string, and the other with a
byte array. A string is far easier, but for many communications tasks you
need to pass an array of bytes.

The problem is that a VARIANT doesn't have many nice ways of converting data
to the correct format; hence, in this case we have to work with SAFEARRAYS
which are quite messy when you think how easy MyVariant.pArray =
my_byte_array would be. In fact, even in VB it's a bit fiddly to send and
receive byte arrays!

You should consider creating methods for yourself to create VARIANTS from
arrays of bytes, and the other way around, so that your code is more simple
at each stage. For example, you could have code that did something like
this:

UINT SendMessage(BYTE *pbyTXBuffer, UINT uTXLength,
                        BYTE *pbyRXBuffer, UINT uRXBufferLength)
{
        UINT            uNumBytesReturned ;
        VARIANT sData ;

        uNumBytesReturned = 0 ; // Assume failure.

        if (CreateVariantFromByteArray(pbyTXBuffer, uTXLength, &sData) )
        {
                CommsControl.Output = sData ;

                // Do something to wait for an answer.

                uNumBytesReturned = CommsControl.InputLen ;
                if (uNumBytesReturned > 0)
                {
                        sData = CommsControl.Input ;
                        CopyByteArrayFromVariant(pbyRXBuffer, uRXBufferLength,
                                                        uNumBytesReturned, 
&sData);
                }
        }

        return uNumBytesReturned ;
}

(example code only!)

--
Jason Teagle
[EMAIL PROTECTED]



_______________________________________________
msvc mailing list
[email protected]
See http://beginthread.com/mailman/listinfo/msvc_beginthread.com for 
subscription changes, and list archive.

Reply via email to