LPDISPATCH是个什么东东?含有这种参数的函数该如何调用?

解决方案 »

  1.   

    Of course, it's a pointer , refer to its definition
      

  2.   

    是一个自动化接口,在COM中有一个IDISPATCH接口,LPDISPATCH是一个指向这个接口的指针,至于IDISPATCH接口你就要看相应的书了
      

  3.   

    接口指针,一般要由该指针指向的接口查询想要的接口
    xxx(LPDISPATCH lpDispatch,...)
    {
        HRESULT hr;
        hr=lpDispatch->QueryInterface(IID_You_wanted_InterfaceID,......);
       ....
    }
      

  4.   

    LPDISPATCH 是指向IDispatch接口的指针,IDispatch是(from MSDN):IDispatch
    IDispatch is a COM interface that is designed in such a way that it can call virtually any other COM interface. Developers working in Microsoft® Visual Basic® often cannot call COM interfaces directly, as they would from C or C++. However, when their tool supports IDispatch, as Visual Basic does, and when the object they want to call supports IDispatch, they can call its COM interfaces indirectly. The main method offered by IDispatch is called Invoke. This method adds a level of indirection to the control flow of the Component Object Model. In the standard COM model, an object obtains a pointer to an interface and then calls a member method of the interface. With IDispatch, instead of directly calling the member method, the program calls IDispatch::Invoke, and IDispatch::Invoke calls the member method for you. Invoke is a general method-calling machine. Its parameters include a value that identifies the method that is to be called and the parameters that are to be sent to it. In order to be able to handle the wide variety of parameters that other COM methods use, Invoke uses a self-describing data structure called a VARIANTARG. The VARIANTARG structure contains two parts: a type field, which represents the data type, and a data field, which represents the actual value of the data. The data type, known also as variant type, contains a constant such as VT_I2 or VT_DATE, which defines valid values for the data types. For more information on variant types, see the Type property of the Field object. Associated with IDispatch is the notion of a type library. The type library publishes information about an interface so that it is available to Visual Basic programs. The type library, or typelib, contains the same kind of information that C or C++ programmers would obtain from a header file: the name of the method and the sequence and types of its parameters. An executable file or DLL that exposes IDispatch and its type library is known as an Automation server. The CDO Library and the CDO Rendering Library are both Automation servers. In version 1.1 and later they are also in-process servers, residing in .DLL files and linking dynamically with the calling modules. 请看下例(来自MSDN)
    INTFACE.CPP
    /************************************************************************* ** **  This is a part of the Microsoft Source Code Samples. ** **  Copyright 1992 - 1998 Microsoft Corporation. All rights reserved. ** **  This source code is only intended as a supplement to Microsoft Development **  Tools and/or WinHelp documentation.  See these sources for detailed **  information regarding the Microsoft samples programs. ** **  OLE Automation TypeLibrary Browse Helper Sample ** **  intface.cpp ** **  CInterface implementation ** **  Written by Microsoft Product Support Services, Windows Developer Support ** *************************************************************************/  #include <windows.h>
    #include <windowsx.h>
    #ifdef WIN16    
    #include <ole2.h> 
    #include <compobj.h>   
    #include <dispatch.h>    
    #include <variant.h>  
    #include <olenls.h>   
    #endif  
    #include "browseh.h"    
    /*  
    * CInterface::Create  
    *  
    * Purpose:  
    *  Creates an instance of the Interface automation object and initializes it.  
    *  
    * Parameters:         
    *  ptinfo         TypeInfo of interface.  
    *  ppInterface    Returns Interface automation object.  
    *  
    * Return Value:  
    *  HRESULT  
    *  
    */ 
    HRESULT  CInterface::Create(LPTYPEINFO ptinfo, CInterface FAR* FAR* ppInterface)  {        
    HRESULT hr;     
    CInterface FAR* pInterface = NULL;           
    *ppInterface = NULL;          
    // Create object.     
    pInterface = new CInterface();     
    if (pInterface == NULL)     
    {         
    hr = E_OUTOFMEMORY;          
    goto error;     
    }         
    // Load type information for the object from type library.      
    hr = pInterface->LoadTypeInfo(IID_IInterface);     
    if (FAILED(hr))         
      goto error;     
    // Intialize base class, CTypeInfo     
    hr = pInterface->_InitTypeInfo(ptinfo);     
    if (FAILED(hr))         
     goto error;         
    ptinfo->AddRef();     
    pInterface->m_ptinfo = ptinfo;  
    #ifdef _DEBUG       
    lstrcpyn(pInterface->m_szClassName, TEXT("Interface"), 100); 
    #endif               
    *ppInterface = pInterface;     
    return NOERROR;      
    error:     
    if (pInterface == NULL) 
      return E_OUTOFMEMORY;     
    if (pInterface->m_ptinfo) 
      pInterface->m_ptinfo->Release();               
    // Set to NULL to prevent destructor from attempting to free again       pInterface->m_ptinfo = NULL;          
    delete pInterface;     
    return hr; 
    }  
    /*  
    * CInterface::CInterface  
    *  
    * Purpose:  
    *  Constructor for CInterface object. Initializes members to NULL.  
    *  
    */ 
    CInterface::CInterface() 
    {      
     m_pdispFunctions = NULL; 
     m_pdispBaseInterface = NULL;     
     m_ptinfo = NULL; }  
    /*  
    * CInterface::~CInterface  
    *  
    * Purpose:  
    *  Destructor for CInterface object.   
    *  
    */ 
    CInterface::~CInterface() 
    {
     if (m_pdispFunctions) m_pdispFunctions->Release(); 
     if (m_pdispBaseInterface) m_pdispBaseInterface->Release();     
     if (m_ptinfo) m_ptinfo->Release(); 
    }   
     
    STDMETHODIMP_(REFCLSID) 
    CInterface::GetInterfaceID() 
    {     
    return IID_IInterface; 
    }  STDMETHODIMP_(ICollection FAR*) 
    CInterface::get_Functions()      
    {       
    HRESULT hr;     
    CFunction FAR* pFunction;     
    CCollection FAR* pCollection = NULL;     
    LPDISPATCH pdisp;     
    LPTYPEATTR ptypeattr = NULL;     
    unsigned short n;          
    if (m_pdispFunctions == NULL)     
    {            
    // Create collection of functions in interface.         
    hr = m_ptinfo->GetTypeAttr(&ptypeattr);          
    if (FAILED(hr))             
    {
    RaiseException(IDS_Unexpected); 
    return NULL;
    }            
    hr = CCollection::Create(ptypeattr->cFuncs, 0, &pCollection);         
    if (FAILED(hr))             
    {
    RaiseException(IDS_Unexpected); 
    goto error;
    }               
    for (n=0; n<ptypeattr->cFuncs; n++)         
    {                             
    hr = CFunction::Create(m_ptinfo, n, &pFunction);              
    if (FAILED(hr))                 
    {
    RaiseException(IDS_Unexpected); 
    goto error;
    }             
    pFunction->QueryInterface(IID_IDispatch, (void FAR* FAR*)&pdisp);             pCollection->Add(pdisp);               
    pdisp->Release();         
    }         
    pCollection->QueryInterface(IID_IDispatch, (void FAR* FAR*)&pdisp);         m_pdispFunctions = pdisp;          
    m_ptinfo->ReleaseTypeAttr(ptypeattr);     
    }     
    m_pdispFunctions->AddRef();     
    return (ICollection FAR*)m_pdispFunctions;           
    error:         
    if (ptypeattr) m_ptinfo->ReleaseTypeAttr(ptypeattr);        
    if (pCollection) delete pCollection;       
    return NULL; 
    }  STDMETHODIMP_(IInterface FAR*) CInterface::get_BaseInterface() 

    HRESULT hr; 
    CInterface FAR* pInterface = NULL; 
    LPTYPEINFO ptinfoInterface = NULL; 
    HREFTYPE hreftype;      
    if(m_pdispBaseInterface == NULL)     
    {     
    hr = m_ptinfo->GetRefTypeOfImplType(0, &hreftype); 
    if (FAILED(hr))             
    {
    RaiseException(IDS_Unexpected); 
    return NULL;

    hr = m_ptinfo->GetRefTypeInfo(hreftype, &ptinfoInterface); 
    if (FAILED(hr))             
    {
    RaiseException(IDS_Unexpected); 
    return NULL;

    hr = CInterface::Create(ptinfoInterface, &pInterface);          
    if (FAILED(hr))             
    {
    RaiseException(IDS_Unexpected); 
    goto error;

    pInterface->QueryInterface(IID_IDispatch, (void FAR* FAR*)BaseInterface);         
    ptinfoInterface->Release(); 

    m_pdispBaseInterface->AddRef(); 
    return (IInterface FAR*)m_pdispBaseInterface;  
    error: 
    if (ptinfoInterface) ptinfoInterface->Release(); 
    if (pInterface) delete pInterface;     
    return NULL;