飘遥的Blog

C/C++/.NET
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

发布一个注册类型库(TypeLib)的小工具

Posted on 2010-09-23 08:57  Zzx飘遥  阅读(3550)  评论(1编辑  收藏  举报
用于在注册表中注册和反注册TypeLib。使用方法:可以用RegTypeLib.exe /? 查看帮助。
RegTypeLib v1.0 (c) Zhenxing Zhou

A tool to register TypeLib or unregister TypeLib
Using RegTypeLib.exe /? to show help
Using RegTypeLib.exe /r <TypeLib file path> to register TypeLib
Using RegTypeLib.exe /u <TypeLib guid> to unregister TypeLib
Example:
        RegTypeLib.exe /r D:\a.tlb
        RegTypeLib.exe /u {bb6df56e-cace-11dc-9992-0019b93a3a84}

源码如下:
#include <Windows.h>
#include
<stdio.h>

void ShowErrorMessage();
void ShowUsage();

int wmain(int argc, WCHAR* argv[])
{
    
if(argc < 2)
    {
        ShowErrorMessage();
        
return 1;
    }

    
if(wcslen(argv[1]) != 2)
    {
        ShowErrorMessage();
        
return 1;
    }

    
if(argv[1][0] != L'/' && argv[1][0] != L'-')
    {
        ShowErrorMessage();
        
return 1;
    }

    
if(argv[1][1] == L'?')
    {
        ShowUsage();
        
return 0;
    }

    
if(argc < 3)
    {
        ShowErrorMessage();
        
return 1;
    }

    
if(tolower(argv[1][1]) == L'r')
    {
        HANDLE hFile
= CreateFile(argv[2], 0, 0, NULL, OPEN_EXISTING, 0, NULL);

        
if(hFile == INVALID_HANDLE_VALUE)
        {
            wprintf(L
"The file %s does not exists!\r\n", argv[2]);
            
return 1;
        }

        CloseHandle(hFile);

        HRESULT hr
= S_OK;
        ITypeLib
*pTypeLib;
        
        hr
= LoadTypeLibEx(argv[2], REGKIND_REGISTER, &pTypeLib);

        
if(FAILED(hr))
        {
            wprintf(L
"Register TypeLib failed. Error Code: %lu", hr);
            
return 1;
        }
        
else
        {
            wprintf(L
"Register TypeLib succeed.");
            pTypeLib
->Release();
        }
    }
    
else if(tolower(argv[1][1]) == L'u')
    {
        HRESULT hr
= S_OK;
        GUID guid;

        hr
= CLSIDFromString(argv[2], &guid);

        
if(FAILED(hr))
        {
            wprintf(L
"Can't convert %s to GUID. Error Code: %lu", argv[2], hr);
        }
        
else
        {
            hr
= UnRegisterTypeLib(guid, 1, 0, LANG_NEUTRAL, SYS_WIN32);

            
if(FAILED(hr))
            {
                wprintf(L
"Can't unregister the TypeLib. Error Code: %lu", hr);
                
return 1;
            }
            
else
            {
                wprintf(L
"Unregister TypeLib %s successfully.", argv[2]);
            }
        }
    }
    
else
    {
        ShowErrorMessage();
        
return 1;
    }

    
return 0;
}

void ShowErrorMessage()
{
    wprintf(L
"The syntax of the command is incorrect.\r\n\r\n");

    ShowUsage();
}

void ShowUsage()
{
    wprintf(L
"RegTypeLib v1.0 (c) Zhenxing Zhou\r\n\r\n");
    wprintf(L
"A tool to register TypeLib or unregister TypeLib\r\n");
    wprintf(L
"Using RegTypeLib.exe /? to show help\r\n");
    wprintf(L
"Using RegTypeLib.exe /r <TypeLib file path> to register TypeLib\r\n");
    wprintf(L
"Using RegTypeLib.exe /u <TypeLib guid> to unregister TypeLib\r\n");
    wprintf(L
"Example:\r\n");
    wprintf(L
"\tRegTypeLib.exe /r D:\\a.tlb\r\n");
    wprintf(L
"\tRegTypeLib.exe /u {bb6df56e-cace-11dc-9992-0019b93a3a84}\r\n");
}

下载:下载源文件及可执行文件