进程外COM组件与客户端传递自定义结构数据问题!请前辈高手教我!
近日需写一个从进程外COM组件提取自定义结构体的客户端程序,在网上查了一下,学习后,写了下面的测试程序,没有通过,请前辈高手教我!1、COM组件
.idl代码:
//========================自定义结构体===================
typedef [ uuid(44ABAE82-A173-11d4-98DB-0080C8F5B2E4)]
struct MyStruct
{
int x;
int y;
}MyStruct;
//=======================================================
.cpp代码:
STDMETHODIMP CSvrTest::GetStruct(VARIANT *strct)
{
const GUID GUID_MyStruct = {0x44ABAE82,0xA173,0x11d4,{0x98,0xDB,0x00,0x80,0xC8,0xF5,0xB2,0xE4}};
ITypeLib* pTypeLib = NULL;
ITypeInfo* pTypeInfo = NULL;
IRecordInfo* pRecordInfo = NULL;
LoadRegTypeLib(LIBID_UDTLib, 1, 0, LANG_NEUTRAL, &pTypeLib);
pTypeLib->GetTypeInfoOfGuid(GUID_MyStruct, &pTypeInfo);
GetRecordInfoFromTypeInfo(pTypeInfo, &pRecordInfo);
MyStruct *pMyStruct = new MyStruct;
pMyStruct->x = pMyStruct->y = 100;
VARIANT vr;
VariantInit(&vr);
vr.vt = VT_RECORD; //表明自定义类型
vr.pvRecord = (PVOID)pMyStruct; //指向实际数据
vr.pRecInfo = pRecordInfo; //包含了数据的一些信息
// test6.Test2(vr); //调用接口方法
strct = &vr;
pTypeLib->Release();
pTypeInfo->Release();
pRecordInfo->Release();
return S_OK;
}
2、客户端调用代码:
VARIANT vr;
VariantInit(&vr);
pSvrTest->GetStruct(&vr);
if (vr.vt == VT_RECORD)
{
IRecordInfo *pRecordInfo = vr.pRecInfo;
PVOID pMyStruct = vr.pvRecord;
VARIANT varX, varY;
int x=0, y=0;
VariantInit(&varX);
VariantInit(&varY);
pRecordInfo->GetField(pMyStruct, L"x", &varX);//获得x字段的值
if (varX.vt == VT_INT)
{
x = varX.intVal;
}
pRecordInfo->GetField(pMyStruct, L"y", &varY);//获得y字段的值
if (varX.vt == VT_INT)
{
y = varY.intVal;
}
}
调试中发现,COM能正常启动,并生成和返回VARIANT *strct数据,但调用程序在pSvrTest->GetStruct(&vr)时,提取的vr始终为空。
奇怪,高手救我!