// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__F0A6AEEE_DE32_4B0D_8940_C3B72B6BF5CD__INCLUDED_)
#define AFX_STDAFX_H__F0A6AEEE_DE32_4B0D_8940_C3B72B6BF5CD__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
#import "..\Simple3\Simple3.tlb" no_namespace
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__F0A6AEEE_DE32_4B0D_8940_C3B72B6BF5CD__INCLUDED_)
// Created by Microsoft (R) C/C++ Compiler Version 12.00.9782.0 (0c2f8e7f).
//
// e:\learn\program\vc\com 组件设计与应用\8\use1\debug\Simple3.tlh
//
// C++ source equivalent of Win32 type library ..\Simple3\Simple3.tlb
// compiler-generated file created 12/16/12 at 17:16:41 - DO NOT EDIT!
#pragma once
#pragma pack(push, 8)
#include <comdef.h>
//
// Forward references and typedefs
//
struct /* coclass */ Mathe;
struct __declspec(uuid("b40691fc-e1ca-4d4d-9ae7-885ac6c6e383"))
/* interface */ IMathe;
struct __declspec(uuid("072ea6cb-7d08-4e7e-b2b7-b2fb0b875595"))
/* interface */ IStr;
struct __declspec(uuid("072ea6cc-7d08-4e7e-b2b7-b2fb0b875595"))
/* interface */ IMathe2;
//
// Smart pointer typedef declarations
//
_COM_SMARTPTR_TYPEDEF(IMathe, __uuidof(IMathe));
_COM_SMARTPTR_TYPEDEF(IStr, __uuidof(IStr));
_COM_SMARTPTR_TYPEDEF(IMathe2, __uuidof(IMathe2));
//
// Type library items
//
struct __declspec(uuid("c6f241e2-43f6-4449-a024-b7340553221e"))
Mathe;
// [ default ] interface IMathe
// interface IStr
// interface IMathe2
struct __declspec(uuid("b40691fc-e1ca-4d4d-9ae7-885ac6c6e383"))
IMathe : IUnknown
{
//
// Wrapper methods for error-handling
//
long Add (
long n1,
long n2 );
//
// Raw methods provided by interface
//
virtual HRESULT __stdcall raw_Add (
long n1,
long n2,
long * pnVal ) = 0;
};
struct __declspec(uuid("072ea6cb-7d08-4e7e-b2b7-b2fb0b875595"))
IStr : IUnknown
{
//
// Wrapper methods for error-handling
//
_bstr_t Cat (
_bstr_t s1,
_bstr_t s2 );
//
// Raw methods provided by interface
//
virtual HRESULT __stdcall raw_Cat (
BSTR s1,
BSTR s2,
BSTR * psVal ) = 0;
};
struct __declspec(uuid("072ea6cc-7d08-4e7e-b2b7-b2fb0b875595"))
IMathe2 : IUnknown
{
//
// Wrapper methods for error-handling
//
long Add (
long n1,
long n2 );
long Mul (
long n1,
long n2 );
//
// Raw methods provided by interface
//
virtual HRESULT __stdcall raw_Add (
long n1,
long n2,
long * pnVal ) = 0;
virtual HRESULT __stdcall raw_Mul (
long n1,
long n2,
long * pnVal ) = 0;
};
//
// Wrapper method implementations
//
#include "e:\learn\program\vc\com 组件设计与应用\8\use1\debug\Simple3.tli"
#pragma pack(pop)
// Created by Microsoft (R) C/C++ Compiler Version 12.00.9782.0 (0c2f8e7f).
//
// e:\learn\program\vc\com 组件设计与应用\8\use1\debug\Simple3.tli
//
// Wrapper implementations for Win32 type library ..\Simple3\Simple3.tlb
// compiler-generated file created 12/16/12 at 17:16:41 - DO NOT EDIT!
#pragma once
//
// interface IMathe wrapper method implementations
//
inline long IMathe::Add ( long n1, long n2 ) {
long _result;
HRESULT _hr = raw_Add(n1, n2, &_result);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _result;
}
//
// interface IStr wrapper method implementations
//
inline _bstr_t IStr::Cat ( _bstr_t s1, _bstr_t s2 ) {
BSTR _result;
HRESULT _hr = raw_Cat(s1, s2, &_result);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _bstr_t(_result, false);
}
//
// interface IMathe2 wrapper method implementations
//
inline long IMathe2::Add ( long n1, long n2 ) {
long _result;
HRESULT _hr = raw_Add(n1, n2, &_result);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _result;
}
inline long IMathe2::Mul ( long n1, long n2 ) {
long _result;
HRESULT _hr = raw_Mul(n1, n2, &_result);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _result;
}
void CUse1Dlg::OnOK()
{
try
{
long nVal;
CString sMsg;
_bstr_t bstr;
// 通过构造函数得到 IMathe 接口指针,如果出现错误
// 比如没有注册,则会抛出异常,执行到 catch 模块
IMathePtr spMathe( _T("Simple3.Mathe") );
IStrPtr spStr;
IMathe2Ptr spMathe2;
// IMathe::Add()
nVal = spMathe->Add( 1, 2 );
sMsg.Format( _T("1 + 2 = %d"), nVal );
MessageBox( sMsg, _T("调用 IMathe::Add() in Simple3.Mathe") );
spStr = spMathe; // 自动 QueryInterface(),如果出现错误会执行到 catch
// IStr::Cat()
bstr = spStr->Cat( "Hello", " world" );
MessageBox( bstr, _T("调用 IStr::Cat()") );
spMathe2 = spMathe; // QueryInterface() 得到 IMathe2 接口指针
// IMathe2::Add(), 其实本质上和 IMathe::Add() 是同一函数体
nVal = spMathe2->Add( 3, 4 );
sMsg.Format( _T("3 + 4 = %d"), nVal );
MessageBox( sMsg, _T("调用 IMathe2::Add()") );
// IMathe2::Mul()
nVal = spMathe2->Mul( 3, 4 );
sMsg.Format( _T("3 * 4 = %d"), nVal );
MessageBox( sMsg, _T("调用 IMathe2::Mul()") );
}
catch(_com_error &e)
{
AfxMessageBox( e.ErrorMessage() );
}
}