描述:
小第到了火烧眉毛地步,绞尽脑汁不得其解,情各位帮忙!
在idl中声明结构体:
// produce the type library (SimpleTest.tlb) and marshalling code.
import "oaidl.idl";
import "ocidl.idl";
[
uuid(C25AEE22-5363-4f63-83A3-73A104D72AA6)
]
typedef struct thisPoint
{
long x;
long y;
}THISPOINT;
[
object,
uuid(D21279C3-4238-4F8C-93D1-4A31AF5C82B0),
dual,
helpstring("ISimpleInterface Interface"),
pointer_default(unique)
]
interface ISimpleInterface : IDispatch
{
[id(1), helpstring("method Trasfer")] HRESULT Trasfer([in] VARIANT var);//接口ISimpleInterface 只有一个方法:
};
[
uuid(741B30EA-C1DD-4369-A8EE-A9EC569BAB98),
helpstring("SimpleInterface Class")
]
方法实现:
// SimpleInterface.cpp : Implementation of CSimpleInterface
#include "stdafx.h"
#include "SimpleTest.h"
#include "SimpleInterface.h"
/////////////////////////////////////////////////////////////////////////////
// CSimpleInterface
STDMETHODIMP CSimpleInterface::Trasfer(VARIANT var)
{
//::MessageBox(NULL,_T("Hello World!"),_T("Welcome"), MB_OK);
if(var.vt == VT_RECORD)
{
IRecordInfo *pRecordInfo = var.pRecInfo;
PVOID pMy_xy = var.pvRecord;
VARIANT varx,vary;
int x,y;
VariantInit(&varx);
VariantInit(&vary);
pRecordInfo->GetField(pMy_xy,L"x",&varx);
if(varx.vt == VT_INT)
x = varx.intVal;
pRecordInfo->GetField(pMy_xy,L"y",&vary);
if(varx.vt == VT_INT)
y = vary.intVal;
//ifend
char *xx = (char*)x;
::MessageBox(NULL,xx,_T("Welcome"),MB_OK }
return S_OK;
}
下面是客户端:用win32应用程序实现,加按钮Onhere
// SimpleClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SimpleClient.h"
#include "SimpleClientDlg.h"
#include "d:\atl\4_5\easy\simpletest\SimpleTest_i.c"
#include "d:\atl\4_5\easy\simpletest\SimpleTest.h"
......
void CSimpleClientDlg::Onhere()
{
const GUID GUID_thisPoint = {0xc25aee22, 0x5363, 0x4f63, {0x83, 0xa3, 0x73, 0xa1, 0x4, 0xd7, 0x2a, 0xa6}};
HRESULT hr;
ISimpleInterface * pIntf = NULL;
hr = CoInitialize(0);//****加此句最终成功****
if(SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_SimpleInterface, NULL, CLSCTX_SERVER,
IID_ISimpleInterface, (void **)& pIntf);
if(SUCCEEDED(hr))
{
ITypeLib* pTypeLib=NULL;
ITypeInfo* pTypeInfo=NULL;
IRecordInfo* pRecordInfo=NULL;
HRESULT hr2;
hr2 = LoadRegTypeLib(LIBID_SIMPLETESTLib,1,0,0,&pTypeLib);
pTypeLib->GetTypeInfoOfGuid(_uuidof(struct thisPoint),&pTypeInfo);
GetRecordInfoFromTypeInfo(pTypeInfo, &pRecordInfo);
thisPoint * m_point = new thisPoint;
m_point->x = 50;
m_point->y = 100;
VARIANT vr;
VariantInit(&vr);
vr.vt=VT_RECORD;
vr.pvRecord=(PVOID)m_point;
pRecordInfo->AddRef();//one IRecordInfo de Point
vr.pRecInfo=pRecordInfo; //返回它的地址
// m_point
pIntf->Trasfer(vr);
pIntf->Release();
pRecordInfo->Release();
VariantClear(&vr);
/*
hr1 = CoInitialize(0);
if (SUCCEEDED(hr1))
{
}
*/
}
}
CDialog::OnOK();
}
请高手帮助修改!!定谢!