⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 create type library.cpp

📁 经验交流,从网上下载的好东西望大家分享
💻 CPP
字号:
// This program creates a type library that exactly matches that produced by
// the MIDL compiler when fed the IDL shown here. The comments found to the
// right of each line of IDL code will direct to the code that creates that
// element of the type library.

// Use the OLE/COM Object Viewer to view the contents of the TLB file

/*
import "unknwn.idl";

[ object, uuid(10000001-0000-0000-0000-000000000001),		// (6)
  oleautomation ]											// (7)
interface ISum												// (5)
	: IUnknown												// (12)
{
	HRESULT Sum(int x, int y, [out, retval] int* retval);	// (13)
}

[ uuid(10000003-0000-0000-0000-000000000001),				// (1)
  helpstring("Inside COM+ Component Type Library"),			// (4)
  version(1.0) ]											// (3)
library Component											// (2)
{
	importlib("stdole32.tlb");								// (12)

	interface ISum;

	[ uuid(10000002-0000-0000-0000-000000000001) ]			// (9)
	coclass InsideCOM										// (8)
	{
		[default]											// (11)
			interface ISum;									// (10)
	}
};
*/

#define _WIN32_DCOM
#include <windows.h>
#include <iostream.h>
#include <stdio.h>

void main()
{
	CoInitialize(NULL);

	// Create the type library file
	ICreateTypeLib2* pCreateTypeLib2;
	CreateTypeLib2(SYS_WIN32, L"C:\\MYLIB.TLB", &pCreateTypeLib2);

	// (1) Set the library LIBID to {10000003-0000-0000-0000-000000000001}
	GUID LIBID_Component = {0x10000003,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};
	pCreateTypeLib2->SetGuid(LIBID_Component);

	// (2) Set the library name to Component
	pCreateTypeLib2->SetName(L"Component");

	// (3) Set the library version to 1.0
	pCreateTypeLib2->SetVersion(1, 0);

	// (4) Set the helpstring to Inside COM+ Component Type Library
	pCreateTypeLib2->SetDocString(L"Inside COM+ Component Type Library");

	pCreateTypeLib2->SetLcid(LANG_NEUTRAL);

	// (5) Create the ISum interface
	ICreateTypeInfo* pCreateTypeInfoInterface;
	pCreateTypeLib2->CreateTypeInfo(L"ISum", TKIND_INTERFACE, &pCreateTypeInfoInterface);



	ICreateTypeInfo2* pCreateTypeInfo2 = 0;
	HRESULT hr = pCreateTypeInfoInterface->QueryInterface(IID_ICreateTypeInfo2, (void**)&pCreateTypeInfo2);
	printf("pTypeCreateTypeInfo2 hr = %0x\n", hr);
	pCreateTypeInfo2->Release();



	// (6) Set the ISum IID to {10000001-0000-0000-0000-000000000001}
	IID IID_ISum = {0x10000001,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};
	pCreateTypeInfoInterface->SetGuid(IID_ISum);

	// (7) Set the oleautomation flag
	pCreateTypeInfoInterface->SetTypeFlags(TYPEFLAG_FOLEAUTOMATION);

	// (8) Create the coclass InsideCOM
	ICreateTypeInfo* pCreateTypeInfoCoClass;
	pCreateTypeLib2->CreateTypeInfo(L"InsideCOM", TKIND_COCLASS, &pCreateTypeInfoCoClass);

	// (9) Set the InsideCOM CLSID to {10000002-0000-0000-0000-000000000001}
	CLSID CLSID_InsideCOM = {0x10000002,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};
	pCreateTypeInfoCoClass->SetGuid(CLSID_InsideCOM);

	// Specify that this coclass can be instantiated
	pCreateTypeInfoCoClass->SetTypeFlags(TYPEFLAG_FCANCREATE);

	// Get a pointer to the ITypeLib interface
    ITypeLib* pTypeLib;
	pCreateTypeLib2->QueryInterface(IID_ITypeLib, (void**)&pTypeLib);




	ITypeLib2* pTypeLib2 = 0;
	hr = pTypeLib->QueryInterface(IID_ITypeLib2, (void**)&pTypeLib2);
	printf("pTypeLib2 hr = %0x\n", hr);
	pTypeLib2->Release();




	// Get a pointer to the ITypeInfo interface for ISum
    ITypeInfo* pTypeInfo;
    pTypeLib->GetTypeInfoOfGuid(IID_ISum, &pTypeInfo);




	ITypeInfo2* pTypeInfo2 = 0;
	hr = pTypeInfo->QueryInterface(IID_ITypeInfo2, (void**)&pTypeInfo2);
	printf("pTypeInfo2 hr = %0x\n", hr);
	pTypeInfo2->Release();




	// Trade in the ITypeInfo pointer for an HREFTYPE
	HREFTYPE hRefTypeISum;
	pCreateTypeInfoCoClass->AddRefTypeInfo(pTypeInfo, &hRefTypeISum);

	// (10) Insert the ISum interface into the InsideCOM coclass
    pCreateTypeInfoCoClass->AddImplType(0, hRefTypeISum);

	// (11) Set ISum to be the default interface in coclass InsideCOM
	pCreateTypeInfoCoClass->SetImplTypeFlags(0, IMPLTYPEFLAG_FDEFAULT);

	// Get a pointer to the ITypeLib interface for StdOLE
    ITypeLib* pTypeLibStdOle;
	GUID GUID_STDOLE = {0x00020430,0x00,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46};
    LoadRegTypeLib(GUID_STDOLE, STDOLE_MAJORVERNUM, STDOLE_MINORVERNUM, STDOLE_LCID, &pTypeLibStdOle);

	// Get a pointer to the ITypeInfo interface for IUnknown
    ITypeInfo* pTypeInfoUnknown;
    pTypeLibStdOle->GetTypeInfoOfGuid(IID_IUnknown, &pTypeInfoUnknown);

	// (12) Declare that ISum is derived from IUnknown
    HREFTYPE hRefType;
	pCreateTypeInfoInterface->AddRefTypeInfo(pTypeInfoUnknown, &hRefType);
	pCreateTypeInfoInterface->AddImplType(0, hRefType);

	// Structures for the x, y, and retval parameters of the Sum method
	TYPEDESC tdescParams = { 0 };
    tdescParams.vt = VT_INT;
	ELEMDESC myParams[3] = { 0 };
	myParams[0].tdesc.vt = VT_INT;
	myParams[0].tdesc.lptdesc = &tdescParams;
	myParams[1].tdesc.vt = VT_INT;
	myParams[1].tdesc.lptdesc = &tdescParams;
	myParams[2].tdesc.vt = VT_PTR;
	myParams[2].tdesc.lptdesc = &tdescParams;
	myParams[2].paramdesc.wParamFlags = PARAMFLAG_FRETVAL|PARAMFLAG_FOUT;

	// Additional data describing the Sum method and its return value
	TYPEDESC tdescUser = { 0 };
    FUNCDESC FuncDesc = { 0 };
	FuncDesc.funckind = FUNC_PUREVIRTUAL;
    FuncDesc.invkind = INVOKE_FUNC;
    FuncDesc.callconv = CC_STDCALL;

	// Parameters
	FuncDesc.cParams = 3;
	FuncDesc.lprgelemdescParam = myParams;

	// HRESULT return value
    FuncDesc.elemdescFunc.tdesc.vt = VT_HRESULT;
    FuncDesc.elemdescFunc.tdesc.lptdesc = &tdescUser;

	// (13) Add the Sum method to the ISum interface
	pCreateTypeInfoInterface->AddFuncDesc(0, &FuncDesc);

	// Set names for the Sum function and its parameters
	OLECHAR* Names[4] = { L"Sum", L"x", L"y", L"retval" };
	pCreateTypeInfoInterface->SetFuncAndParamNames(0, Names, 4);

	// Assign the v-table layout
	pCreateTypeInfoInterface->LayOut();

	// Save changes to disk
	pCreateTypeLib2->SaveAllChanges();

	// Release all references
	pTypeInfoUnknown->Release();
	pTypeLibStdOle->Release();
    pTypeInfo->Release();
	pTypeLib->Release();
	pCreateTypeLib2->Release();
	pCreateTypeInfoInterface->Release();
	pCreateTypeInfoCoClass->Release();

	CoUninitialize();
	cout << "Type library created: C:\\MYLIB.TLB" << endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -