Hi,
My question on VARIANT is that when I was coding a program which needed to communciate with a device through COM, I use MSComm to complete this task.
The code below is I just copy from a book about how to use MSComm to communicate.

BYTE* Comm1(int iIndex,CMSComm mscomm,CListBox list)
{
int iLen;
int i;
BYTE *BReceive;
//!! BYTE BInstr[iIndex][] is the instruct;

//get the length of the instruction
iLen=sizeof(BInstr[iIndex]);

//@CInspect is used for inspect
CString CInspect;

//use a CByteArray to send BYTE to Variant
CByteArray Array;
//clean array
Array.RemoveAll();
//set the size of it
Array.SetSize(iLen);


//@output them on list2
CInspect.Format("%x,%x,%x,%x",BInstr[iIndex][0],BInstr[iIndex][1],BInstr[iIndex][2],BInstr[iIndex][3]);
list.AddString(CInspect);

//send BYTE to Array
for (i=0;i<iLen;i++)
{
Array.SetAt(i,BInstr[iIndex][i]);
}

//Send Message
m_mscomm.SetOutput(COleVariant(Array));
//Get information
Vdata=m_mscomm.GetInput();
//change Variant to byte
BReceive=(BYTE*)(unsigned char*)Vdata.parray->pvData;
//@output them on list2
CInspect.Format("%x,%x,%x,%x",BReceive[0],BReceive[1],BReceive[2],BReceive[3]);
list.AddString(CInspect);

return BReceive;

}

BYTE* Comm2(int iIndex,CMSComm mscomm,CListBox list)
{

CString CInspect;
SAFEARRAY *pSa;
VARIANT Vdata;
int iLen;
//get the length of the instruction
iLen=sizeof(BInstr[iIndex]);

CInspect.Format("%x,%x,%x,%x",BInstr[iIndex][0],BInstr[iIndex][1],BInstr[iIndex][2],BInstr[iIndex][3]);
list.AddString(CInspect);

//set safearray 4 byte array,endline is 0
pSa=SafeArrayCreateVector(VT_UI1,0,iLen);
for(i=0;i<iCount;i++)
{
SafeArrayPutElement(pSa,&i,BInstr+i);
}
Vdata.vt=VT_ARRAY|VT_UI1;
Vdata.parray=pSa;

//Send Message
m_mscomm.SetOutput(Vdata);

//Get information
Vdata=m_mscomm.GetInput();

//change Variant to byte
BReceive=(BYTE*)(unsigned char*)Vdata.parray->pvData;
//@output them on list2
CInspect.Format("%x,%x,%x,%x",BReceive[0],BReceive[1],BReceive[2],BReceive[3]);
list.AddString(CInspect);

}
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.
Thank you very much for anyhelp.
PS,My native language is Chinese, and English is my second language. Although I can get a high score in English test in China, by I found it's not easy for me to get the full meaning of the material I read, and I feel a little that English is a barrier to me when I want to get some advanced message on the Net. And thanks for anyone knows my feeling.

Yang






想 要 一 个 快 n 倍 的 免 费 邮 箱 吗 ?
126 专 业 电 子 邮 局 ―― 全 球 领 先 的 中 文 邮 箱 带 你 进 入 极 速 之 旅
_______________________________________________
msvc mailing list
msvc@beginthread.com
See http://beginthread.com/mailman/listinfo/msvc_beginthread.com for 
subscription changes, and list archive.

Reply via email to