小第到了火烧眉毛地步,绞尽脑汁不得其解,情各位帮忙!
在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();
}请高手帮助修改!!定谢!

解决方案 »

  1.   

    这是在ATL开发的.
    问题是: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);
    后面自然没法执行!愿意交流的帮个忙,顶一顶!谢!
      

  2.   

    Too terrible!
    What's the point???
      

  3.   

    前辈,"point",具体点好吗,指struct thisPoint 应用指针类型吗?
      

  4.   

    你最好将IDL与c类比,而非c++,你自定义的是结构,不是接口,也不是组件类,当然没有GUID,没有类型信息,
      

  5.   

    // 下面的代码必须定义在库块内,不然无法将结构作为参数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] 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();
    }
    建议楼主还是去多看些书。
      

  6.   

    我是想用VARIANT类型传递,手头上只有<IDL精髓 >
     参考了 http://www.yesky.com/20020516/1611539.shtml能否给些提示!
      

  7.   

    按照<IDL精髓 >中的方法加结构总出错,不知为啥!
      

  8.   

    如果说从客户端得不到struct 的GUID,那客户端应怎样写?
      

  9.   

    简单的办法: 将结构写成只有属性的COM对象
      

  10.   

    LeeZi,只有属性的COM对象,哪我的方法怎莫用,而且你这com对象中struct 具体怎莫写,才能在客户端调用,是能举个例子?????
      

  11.   

    interface IPoint;
    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);
    };
      

  12.   

    STDMETHODIMP CSimpleInterface::Trasfer(IPoint* Pos)
    {
    long nX, nY;
    HRESULT hr = Pos->get_X(&nX);
    if (FAILED(hr))
    return hr;
    hr = Pos->get_Y(&nY);
    return hr;
    }
      

  13.   

    #import "xxx.tlb" rename_namespace("XXX")  //将xxx.tlb改为你的文件名void CSimpleClientDlg::Onhere() 
    {
    ::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();
    }
      

  14.   

    谢谢,你的方法很好,不过我又遇到一个小问题,就是怎么实现
    long x;
    ???
    ::MessageBox(NULL,cx,T("Welcome"), MB_OK);//cx应是x的字符格式
    LPCSTR;
    我是过sprintf,总是输出:858993460或-858993460
    你又好办法吗?