小第到了火烧眉毛地步,绞尽脑汁不得其解,情各位帮忙!
在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();
}请高手帮助修改!!定谢!
在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();
}请高手帮助修改!!定谢!
解决方案 »
- 更新控件内容
- VC/MFC界面版块和进程/线程DLL版块在这次升级后FireFox就打不开了吗?
- 如何用mfc socket编写一个udp的通信程序?
- Visual C++中轻松实现透明滑动控件
- 考研失败,工作也一直没找到,真失败
- 怎样直接从*.xls文件中直接读取数据,不要用ODBC和OLE等(急!!!解决就给分)分不够还可以再加!!!!
- 用VC自动生成的Win32 Application 不能添加C++语句是何故
- 有谁能提供通过HTTP下载文件的程序?
- 文件读取
- 做服务端程序员好,还是给公司开发工具好?
- VC与office版本问题
- 使用MFC后,能否像SDK一样处理所有消息?要用什么方法?
问题是:pTypeLib->GetTypeInfoOfGuid(_uuidof(struct thisPoint),&pTypeInfo);
_uuidof(struct thisPoint)报错,去不到GUID of struct,
换成const GUID GUID_thisPoint = {0xc25aee22, 0x5363, 0x4f63, {0x83, 0xa3, 0x73, 0xa1, 0x4, 0xd7, 0x2a, 0xa6}};也得不到pTypeInfo,(执行后pTypeInfo=0);
后面自然没法执行!愿意交流的帮个忙,顶一顶!谢!
What's the point???
{
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] THISPOINT var);//接口ISimpleInterface 只有一个方法: }; STDMETHODIMP CSimpleInterface::Trasfer(THISPOINT var)
{
long x = var.x;
long y = var.y;
// ...
return S_OK;
}
#import "d:\atl\4_5\easy\simpletest\SimpleTest.dll" no_namespace
void CSimpleClientDlg::Onhere()
{
::CoInitialize(NULL);
ISimpleInterfacePtr pIntf;
try
{
hr = pIntf.CreateInstance(__uuidof(SimpleInterface));
if (SUCCEEDED(hr))
{
THISPOINT pt;
pt.x = 50;
pt.y = 100;
pIntf->Trasfer(pt);
}
}
catch(_com_error e)
{
AfxMessageBox(e.Description(), MB_OK|MB_ICONWARNING);
} pIntf.Release(); ::CoUninitialize();
}
建议楼主还是去多看些书。
参考了 http://www.yesky.com/20020516/1611539.shtml能否给些提示!
interface ISimpleInterface; [
object,
uuid(F20290BD-02E1-4421-90A2-1A1A9026A569),
dual,
helpstring("IPoint Interface"),
pointer_default(unique)
]
interface IPoint : IDispatch
{
[propget, id(1), helpstring("property X")] HRESULT X([out, retval] long *pVal);
[propput, id(1), helpstring("property X")] HRESULT X([in] long newVal);
[propget, id(2), helpstring("property Y")] HRESULT Y([out, retval] long *pVal);
[propput, id(2), helpstring("property Y")] HRESULT Y([in] long newVal);
}; [
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] IPoint* Pos);
};
{
long nX, nY;
HRESULT hr = Pos->get_X(&nX);
if (FAILED(hr))
return hr;
hr = Pos->get_Y(&nY);
return hr;
}
{
::CoInitialize(NULL);
{
XXX::IPointPtr pPoint;
HRESULT hr = pPoint.CreateInstance(__uuidof(XXX::Point));
pPoint->X = 300L;
pPoint->Y = 400L; XXX::ISimpleInterface pSimple;
hr = pSimple.CreateInstance(__uuidof(XXX::SimpleInterface)); pSimple->Trasfer(pPoint);
}
::CoUninitialize();
CDialog::OnOK();
}
long x;
???
::MessageBox(NULL,cx,T("Welcome"), MB_OK);//cx应是x的字符格式
LPCSTR;
我是过sprintf,总是输出:858993460或-858993460
你又好办法吗?