打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
LoadTypeLib
#include <windows.h>
#include <ole2.h>
int main(int argc, char* argv[])
{
CoInitialize(0);
ITypeLib* typeLib;
HRESULT hr = LoadTypeLib((wchar_t *)argv[1], typeLib);
.
.
.
CoUninitialize();
}

//--------------------------------------------------------------
function TtfAutoUpdate.pRegLib(fileName : String):boolean;
var
sFileName : String;
ptlib: ITypeLib;
begin
sFileName := fileName;
try
result := true;
OleCheck(LoadTypeLib(PWideChar(sFileName), ptlib));
OleCheck(RegisterTypeLib(ptlib, PWideChar(sFileName),nil));
pWriteLog('注册' + fileName + '成功');
Except
on e:Exception do
begin
result := false;
pWriteLog('注册' + fileName + '失败,错误:' + e.Message);
end;
end;
end;
//--------------------------------------------------------------
function TtfAutoUpdate.pUnRegLib(fileName : String):boolean;
var
sFileName : String;
ptlib : ITypeLib;
ptla : PTLibAttr;
begin
sFileName := fileName;
try
result := true;
OleCheck(LoadTypeLib(PWideChar(sFileName), ptlib));
OleCheck(ptlib.GetLibAttr(ptla));
OleCheck(UnRegisterTypeLib(ptla.guid,ptla.wMajorVerNum,ptla.wMinorVerNum,ptla.lcid,ptla.syskind));
ptlib.ReleaseTLibAttr(ptla);
pWriteLog('卸载' + fileName + '成功');
Except
on e:Exception do
begin
result := false;
pWriteLog('卸载' + fileName + '失败,错误:' + e.Message);
end;
end;
end;


  1. unit InstFnc2;

  2. {
  3.   Inno Setup
  4.   Copyright (C) 1997-2004 Jordan Russell
  5.   Portions by Martijn Laan
  6.   For conditions of distribution and use, see LICENSE.TXT.

  7.   OLE-related installation functions

  8.   $jrsoftware: issrc/Projects/InstFnc2.pas,v 1.21 2005/04/09 07:28:51 jr Exp $
  9. }

  10. interface

  11. {$I VERSION.INC}

  12. function CreateShellLink(const Filename, Description, ShortcutTo, Parameters,
  13.   WorkingDir, IconFilename: String; const IconIndex, ShowCmd: Integer;
  14.   const HotKey: Word; FolderShortcut: Boolean): String;
  15. procedure RegisterTypeLibrary(const Filename: String);
  16. procedure UnregisterTypeLibrary(const Filename: String);

  17. implementation

  18. uses
  19.   Windows, SysUtils, PathFunc, CmnFunc2, InstFunc, Main, Msgs, MsgIDs,
  20.   {$IFNDEF Delphi3orHigher} OLE2, {$ELSE} ActiveX, ComObj, {$ENDIF}
  21.   ShellAPI, ShlObj;

  22. function IsWindowsXP: Boolean;
  23. { Returns True if running Windows XP or later }
  24. begin
  25.   Result := (WindowsVersion >= Cardinal($05010000));
  26. end;

  27. function GetResultingFilename(const PF: IPersistFile;
  28.   const OriginalFilename: String): String;
  29. { Determines the actual resulting filename. IPersistFile::Save doesn't always
  30.   save to the specified filename; it may rename the extension to .pif if the
  31.   shortcut points to an MS-DOS application. }
  32. var
  33.   CurFilename: PWideChar;
  34.   OleResult: HRESULT;
  35. begin
  36.   Result := '';
  37.   CurFilename := nil;
  38.   OleResult := PF.GetCurFile(CurFilename);
  39.   { Note: Prior to Windows 2000/Me, GetCurFile succeeds but returns a NULL
  40.     pointer }
  41.   if SUCCEEDED(OleResult) and Assigned(CurFilename) then begin
  42.     if OleResult = S_OK then
  43.       Result := WideCharToString(CurFilename);
  44.     CoTaskMemFree(CurFilename);
  45.   end;
  46.   { If GetCurFile didn't work (e.g. not running Windows 2000/Me or later), we
  47.     have no choice but to try to guess the filename }
  48.   if Result = '' then begin
  49.     if NewFileExists(OriginalFilename) then
  50.       Result := OriginalFilename
  51.     else if NewFileExists(PathChangeExt(OriginalFilename, '.pif')) then
  52.       Result := PathChangeExt(OriginalFilename, '.pif')
  53.     else begin
  54.       { Neither exist? Shouldn't happen, but return something anyway }
  55.       Result := OriginalFilename;
  56.     end;
  57.   end;
  58. end;

  59. function CreateShellLink(const Filename, Description, ShortcutTo, Parameters,
  60.   WorkingDir, IconFilename: String; const IconIndex, ShowCmd: Integer;
  61.   const HotKey: Word; FolderShortcut: Boolean): String;
  62. { Creates a lnk file named Filename, with a description of Description, with a
  63.   HotKey hotkey, which points to ShortcutTo.
  64.   NOTE! If you want to copy this procedure for use in your own application
  65.   be sure to call CoInitialize at application startup and CoUninitialize at
  66.   application shutdown. See the bottom of this unit for an example. But this
  67.   is not necessary if you are using Delphi 3 and your project already 'uses'
  68.   the ComObj RTL unit. }
  69. const
  70.   CLSID_FolderShortcut: TGUID = (
  71.     D1:$0AFACED1; D2:$E828; D3:$11D1; D4:($91,$87,$B5,$32,$F1,$E9,$57,$5D));
  72. {$IFNDEF Delphi3OrHigher}
  73. var
  74.   OleResult: HRESULT;
  75.   SL: IShellLink;
  76.   PF: IPersistFile;
  77.   WideFilename: PWideChar;
  78. begin
  79.   if FolderShortcut then
  80.     OleResult := CoCreateInstance(CLSID_FolderShortcut, nil, CLSCTX_INPROC_SERVER,
  81.       IID_IShellLink, SL)
  82.   else
  83.     OleResult := E_FAIL;
  84.   { If a folder shortcut wasn't requested, or if CoCreateInstance failed
  85.     because the user isn't running Windows 2000/Me or later, create a normal
  86.     shell link instead }
  87.   if OleResult <> S_OK then begin
  88.     FolderShortcut := False;
  89.     OleResult := CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER,
  90.        IID_IShellLink, SL);
  91.     if OleResult <> S_OK then
  92.       RaiseOleError('CoCreateInstance', OleResult);
  93.   end;
  94.   PF := nil;
  95.   WideFilename := nil;
  96.   try
  97.     SL.SetPath(PChar(ShortcutTo));
  98.     SL.SetArguments(PChar(Parameters));
  99.     if WorkingDir <> '' then
  100.       SL.SetWorkingDirectory(PChar(WorkingDir));
  101.     if IconFilename <> '' then
  102.       SL.SetIconLocation(PChar(IconFilename), IconIndex);
  103.     SL.SetShowCmd(ShowCmd);
  104.     if Description <> '' then
  105.       SL.SetDescription(PChar(Description));
  106.     if HotKey <> 0 then
  107.       SL.SetHotKey(HotKey);

  108.     OleResult := SL.QueryInterface(IID_IPersistFile, PF);
  109.     if OleResult <> S_OK then
  110.       RaiseOleError('IShellLink::QueryInterface', OleResult);
  111.     { When creating a folder shortcut on 2000/Me, IPersistFile::Save will strip
  112.       off everything past the last '.' in the filename, so we keep the .lnk
  113.       extension on to give it something harmless to strip off. XP doesn't do
  114.       that, so we must remove the .lnk extension ourself. }
  115.     if FolderShortcut and IsWindowsXP then
  116.       WideFilename := StringToOleStr(PathChangeExt(Filename, ''))
  117.     else
  118.       WideFilename := StringToOleStr(Filename);
  119.     if WideFilename = nil then
  120.       OutOfMemoryError;
  121.     OleResult := PF.Save(WideFilename, True);
  122.     if OleResult <> S_OK then
  123.       RaiseOleError('IPersistFile::Save', OleResult);

  124.     Result := GetResultingFilename(PF, Filename);
  125.   finally
  126.     if Assigned(WideFilename) then
  127.       SysFreeString(WideFilename);
  128.     if Assigned(PF) then
  129.       PF.Release;
  130.     SL.Release;
  131.   end;
  132. {$ELSE}
  133. var
  134.   OleResult: HRESULT;
  135.   Obj: IUnknown;
  136.   SL: IShellLink;
  137.   PF: IPersistFile;
  138.   WideFilename: WideString;
  139. begin
  140.   if FolderShortcut then begin
  141.     try
  142.       Obj := CreateComObject(CLSID_FolderShortcut);
  143.     except
  144.       { Folder shortcuts aren't supported prior to Windows 2000/Me. Fall back
  145.         to creating a normal shell link. }
  146.       Obj := nil;
  147.     end;
  148.   end;
  149.   if Obj = nil then begin
  150.     FolderShortcut := False;
  151.     Obj := CreateComObject(CLSID_ShellLink);
  152.   end;
  153.   SL := Obj as IShellLink;
  154.   SL.SetPath(PChar(ShortcutTo));
  155.   SL.SetArguments(PChar(Parameters));
  156.   if WorkingDir <> '' then
  157.     SL.SetWorkingDirectory(PChar(WorkingDir));
  158.   if IconFilename <> '' then
  159.     SL.SetIconLocation(PChar(IconFilename), IconIndex);
  160.   SL.SetShowCmd(ShowCmd);
  161.   if Description <> '' then
  162.     SL.SetDescription(PChar(Description));
  163.   if HotKey <> 0 then
  164.     SL.SetHotKey(HotKey);

  165.   PF := SL as IPersistFile;
  166.   { When creating a folder shortcut on 2000/Me, IPersistFile::Save will strip
  167.     off everything past the last '.' in the filename, so we keep the .lnk
  168.     extension on to give it something harmless to strip off. XP doesn't do
  169.     that, so we must remove the .lnk extension ourself. }
  170.   if FolderShortcut and IsWindowsXP then
  171.     WideFilename := PathChangeExt(Filename, '')
  172.   else
  173.     WideFilename := Filename;
  174.   OleResult := PF.Save(PWideChar(WideFilename), True);
  175.   if OleResult <> S_OK then
  176.     RaiseOleError('IPersistFile::Save', OleResult);

  177.   Result := GetResultingFilename(PF, Filename);
  178.   { Delphi 3 automatically releases COM objects when they go out of scope }
  179. {$ENDIF}
  180. end;

  181. procedure RegisterTypeLibrary(const Filename: String);
  182. {$IFNDEF Delphi3OrHigher}
  183. var
  184.   WideFilename: PWideChar;
  185.   OleResult: HRESULT;
  186.   TypeLib: ITypeLib;
  187. begin
  188.   WideFilename := StringToOleStr(PathExpand(Filename));
  189.   if WideFilename = nil then
  190.     OutOfMemoryError;
  191.   try
  192.     OleResult := LoadTypeLib(WideFilename, TypeLib);
  193.     if OleResult <> S_OK then
  194.       RaiseOleError('LoadTypeLib', OleResult);
  195.     try
  196.       OleResult := RegisterTypeLib(TypeLib, WideFilename, nil);
  197.       if OleResult <> S_OK then
  198.         RaiseOleError('RegisterTypeLib', OleResult);
  199.     finally
  200.       TypeLib.Release;
  201.     end;
  202.   finally
  203.     SysFreeString(WideFilename);
  204.   end;
  205. end;
  206. {$ELSE}
  207. var
  208.   WideFilename: WideString;
  209.   OleResult: HRESULT;
  210.   TypeLib: ITypeLib;
  211. begin
  212.   WideFilename := PathExpand(Filename);
  213.   OleResult := LoadTypeLib(PWideChar(WideFilename), TypeLib);
  214.   if OleResult <> S_OK then
  215.     RaiseOleError('LoadTypeLib', OleResult);
  216.   OleResult := RegisterTypeLib(TypeLib, PWideChar(WideFilename), nil);
  217.   if OleResult <> S_OK then
  218.     RaiseOleError('RegisterTypeLib', OleResult);
  219. end;
  220. {$ENDIF}

  221. procedure UnregisterTypeLibrary(const Filename: String);
  222. type
  223.   TUnRegTlbProc = function(const libID: TGUID; wVerMajor, wVerMinor: Word;
  224.     lcid: TLCID; syskind: TSysKind): HResult; stdcall;
  225. {$IFNDEF Delphi3OrHigher}
  226. var
  227.   UnRegTlbProc: TUnRegTlbProc;
  228.   WideFilename: PWideChar;
  229.   OleResult: HRESULT;
  230.   TypeLib: ITypeLib;
  231.   LibAttr: PTLibAttr;
  232. begin
  233.   { Dynamically import UnRegisterTypeLib since older OLEAUT32.DLL versions
  234.     don't have this function }
  235.   @UnRegTlbProc := GetProcAddress(GetModuleHandle('OLEAUT32.DLL'),
  236.     'UnRegisterTypeLib');
  237.   if @UnRegTlbProc = nil then
  238.     Win32ErrorMsg('GetProcAddress');
  239.   WideFilename := StringToOleStr(PathExpand(Filename));
  240.   if WideFilename = nil then
  241.     OutOfMemoryError;
  242.   try
  243.     OleResult := LoadTypeLib(WideFilename, TypeLib);
  244.     if OleResult <> S_OK then
  245.       RaiseOleError('LoadTypeLib', OleResult);
  246.     try
  247.       OleResult := TypeLib.GetLibAttr(LibAttr);
  248.       if OleResult <> S_OK then
  249.         RaiseOleError('ITypeLib::GetLibAttr', OleResult);
  250.       try
  251.         with LibAttr^ do
  252.           OleResult := UnRegTlbProc(Guid, wMajorVerNum, wMinorVerNum, LCID, SysKind);
  253.         if OleResult <> S_OK then
  254.           RaiseOleError('UnRegisterTypeLib', OleResult);
  255.       finally
  256.         TypeLib.ReleaseTLibAttr(LibAttr);
  257.       end;
  258.     finally
  259.       TypeLib.Release;
  260.     end;
  261.   finally
  262.     SysFreeString(WideFilename);
  263.   end;
  264. end;
  265. {$ELSE}
  266. var
  267.   UnRegTlbProc: TUnRegTlbProc;
  268.   WideFilename: WideString;
  269.   OleResult: HRESULT;
  270.   TypeLib: ITypeLib;
  271.   LibAttr: PTLibAttr;
  272. begin
  273.   { Dynamically import UnRegisterTypeLib since older OLEAUT32.DLL versions
  274.     don't have this function }
  275.   @UnRegTlbProc := GetProcAddress(GetModuleHandle('OLEAUT32.DLL'),
  276.     'UnRegisterTypeLib');
  277.   if @UnRegTlbProc = nil then
  278.     Win32ErrorMsg('GetProcAddress');
  279.   WideFilename := PathExpand(Filename);
  280.   OleResult := LoadTypeLib(PWideChar(WideFilename), TypeLib);
  281.   if OleResult <> S_OK then
  282.     RaiseOleError('LoadTypeLib', OleResult);
  283.   OleResult := TypeLib.GetLibAttr(LibAttr);
  284.   if OleResult <> S_OK then
  285.     RaiseOleError('ITypeLib::GetLibAttr', OleResult);
  286.   try
  287.     with LibAttr^ do
  288.       OleResult := UnRegTlbProc(Guid, wMajorVerNum, wMinorVerNum, LCID, SysKind);
  289.     if OleResult <> S_OK then
  290.       RaiseOleError('UnRegisterTypeLib', OleResult);
  291.   finally
  292.     TypeLib.ReleaseTLibAttr(LibAttr);
  293.   end;
  294. end;
  295. {$ENDIF}

  296. procedure InitOle;
  297. var
  298.   OleResult: HRESULT;
  299. begin
  300.   OleResult := CoInitialize(nil);
  301.   if FAILED(OleResult) then
  302.     raise Exception.CreateFmt('CoInitialize failed (0x%.8x)', [OleResult]);
  303.     { ^ doesn't use a SetupMessage since messages probably aren't loaded
  304.       during 'initialization' section below, which calls this procedure }
  305. end;

  306. initialization
  307.   InitOle;
  308. finalization
  309.   CoUninitialize;
  310. end.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
delphi基础开发技巧
向WebBrowser中添加静态HTML,执行脚本,载入HTML
Crossbow病毒开放源代码-熊猫烧香源始代码
支持xml的控件
使用TWebBrowser组件保存网页为html和mht文件 - SUNSTONE的Del...
插件管理框架 for Delphi(二)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服