Invalid Space ID: d4js1mk7Sr374meJe5avMc

root/trunk/RegisterTypeLibrary/RegisterTypeLibrary.cpp

User picture

Author: alax

Revision: 304 («Previous)

(Jan 05 16:11 2009 UTC) About 3 years ago


  

 
Show/hide line numbers
////////////////////////////////////////////////////////////
// Copyright (C) Roman Ryltsov, 2008-2009
// Created by Roman Ryltsov roman@alax.info
// 
// $Id$

#include "stdafx.h"

///////////////////////////////////////////////////////////
// CModule

class CModule :
	public CAtlExeModuleT<CModule>,
	public CWtlExeModuleT<CModule>
{
private:

public:
// CModule
	CModule() throw()
	{
	}
	VOID Process(CPath sPath, const CString& sOptions)
	{
		CComPtr<ITypeLib> pTypeLib;
		__C(LoadTypeLib(CStringW(sPath), &pTypeLib));
		_A(pTypeLib);
		TLIBATTR LibraryAttributes;
		ZeroMemory(&LibraryAttributes, sizeof LibraryAttributes);
		_ATLTRY
		{
			_tprintf(_T("Type Library Information:\n"));
			sPath.Canonicalize();
			_tprintf(_T("  File Path: %s\n"), sPath);
			if(sPath.IsRelative())
			{
				TCHAR pszDirectory[MAX_PATH] = { 0 };
				if(GetCurrentDirectory(DIM(pszDirectory), pszDirectory))
				{
					CPath sName = sPath;
					sPath.Combine(pszDirectory, sName);
				}
				__D(!sPath.IsRelative(), E_UNNAMED); // The path should be absolute
				_tprintf(_T("  File Full Path: %s\n"), sPath);
			}
			CComBSTR sName;
			CComBSTR sDocumentationString;
			DWORD nHelpContext = 0;
			CComBSTR sHelpFileName;
			__C(pTypeLib->GetDocumentation(-1, &sName, &sDocumentationString, &nHelpContext, &sHelpFileName));
			_tprintf(_T("  Name: %s\n"), CString(sName));
			if(wcslen(sDocumentationString))
				_tprintf(_T("  Documentation String: %s\n"), CString(sDocumentationString));
			if(sHelpFileName && (wcslen(sHelpFileName) || nHelpContext))
				_tprintf(_T("  Help File Name and Context: %s, %d\n"), CString(sHelpFileName), nHelpContext);
			TLIBATTR* pLibraryAttributes = NULL;
			__C(pTypeLib->GetLibAttr(&pLibraryAttributes));
			_ATLTRY
			{
				LibraryAttributes = *pLibraryAttributes;
				_tprintf(_T("  GUID: %s\n"), CString(_PersistHelper::StringFromIdentifier(pLibraryAttributes->guid)));
				_tprintf(_T("  LCID: %d\n"), pLibraryAttributes->lcid);
				_tprintf(_T("  Platform: %d\n"), pLibraryAttributes->syskind);
				_tprintf(_T("  Version: %d.%d\n"), pLibraryAttributes->wMajorVerNum, pLibraryAttributes->wMinorVerNum);
				_tprintf(_T("  Flags: 0x%x\n"), pLibraryAttributes->wLibFlags);
			}
			_ATLCATCHALL()
			{
				pTypeLib->ReleaseTLibAttr(pLibraryAttributes);
				_ATLRETHROW;
			}
			pTypeLib->ReleaseTLibAttr(pLibraryAttributes);
			_tprintf(_T("\n"));
		}
		_ATLCATCHALL()
		{
			_tprintf(_T("Warning: Failed to query type library information from file %s\n"), sPath);
		}
		if(sOptions.FindOneOf(_T("uU")) < 0)
		{
			if(sOptions.FindOneOf(_T("qQ")) < 0)
			{
				_tprintf(_T("Registering...\n"));
				__C(RegisterTypeLib(pTypeLib, const_cast<LPWSTR>((LPCWSTR) CStringW((LPCTSTR) sPath)), NULL));
				_tprintf(_T("Registered.\n"));
			}
		} else
		{
			_tprintf(_T("Unregistering...\n"));
			__D(!IsEqualGUID(LibraryAttributes.guid, GUID_NULL), E_UNNAMED);
			__C(UnRegisterTypeLib(LibraryAttributes.guid, LibraryAttributes.wMajorVerNum, LibraryAttributes.wMinorVerNum, LibraryAttributes.lcid, LibraryAttributes.syskind));
			_tprintf(_T("Unregistered.\n"));
		}
	}
};

////////////////////////////////////////////////////////////
// Main

int _tmain(int argc, _TCHAR* argv[])
{
	CModule Module;
	_ATLTRY
	{
		if(argc < 2)
		{
			_tprintf(_T("Syntax: RegisterTypeLibrary [-<options>] <file-path>\n"));
			_tprintf(_T("Options:\n"));
			_tprintf(_T("  -u  Unregister type library\n"));
			_tprintf(_T("  -q  Query information (no registration action taken)\n"));
			return 1;
		}
		CString sOptions;
		CPath sPath;
		if(argc > 2)
		{
			sOptions = argv[1];
			if(sOptions.Left(1) != _T("-") && sOptions.Left(1) != _T("/"))
				sOptions.Empty();
			sPath = argv[2];
		} else
			sPath = argv[1];
		Module.Process(sPath, sOptions);
	}
	_ATLCATCH(Exception)
	{
		CComHeapPtr<TCHAR> pszMessage;
		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, Exception, 0, reinterpret_cast<LPTSTR>((LPTSTR*) &pszMessage), 0, NULL);
		CString sMessage(pszMessage);
		sMessage.TrimRight(_T("\t\r\n ."));
		_tprintf(_T("Fatal: Error 0x%08x (%s)\t\n"), (HRESULT) Exception, sMessage);
		return (INT) (HRESULT) Exception;
	}
	_ATLCATCHALL()
	{
		_tprintf(_T("Fatal: Fatal error\t\n"));
		return (INT) E_UNNAMED;
	}
	return 0;
}