home *** CD-ROM | disk | FTP | other *** search
/ PC Expert 29 / Pce29cd.iso / RUNIMAGE / DELPHI40 / DEMOS / ACTIVEX / TREGSVR / TREGSVR.DPR < prev    next >
Text File  |  1998-06-16  |  5KB  |  176 lines

  1. program TRegSvr;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils, Windows, ActiveX, ComObj, RegConst;
  7.  
  8. type
  9.   TRegType = (rtAxLib, rtTypeLib, rtExeLib);
  10.   TRegAction = (raReg, raUnreg);
  11.   TRegProc = function : HResult; stdcall;
  12.   TUnRegTlbProc = function (const libID: TGUID; wVerMajor, wVerMinor: Word;
  13.     lcid: TLCID; syskind: TSysKind): HResult; stdcall;
  14.  
  15.  
  16. const
  17.   ProcName: array[TRegAction] of PChar = (
  18.     'DllRegisterServer', 'DllUnregisterServer');
  19.   ExeFlags: array[TRegAction] of string = (' /regserver', ' /unregserver');
  20.  
  21. var
  22.   RegType: TRegType = rtAxLib;
  23.   RegAction: TRegAction = raReg;
  24.   QuietMode: Boolean = False;
  25.   FileName: string;
  26.   RegProc: TRegProc;
  27.   LibHandle: THandle;
  28.   OleAutLib: THandle;
  29.   UnRegTlbProc: TUnRegTlbProc;
  30.  
  31.  
  32. procedure OutputStr(S: string);
  33. begin
  34.   if not QuietMode then 
  35.   begin
  36.     CharToOEM(PChar(S), PChar(S));
  37.     Writeln(S);
  38.   end;
  39. end;
  40.  
  41. function DecodeOptions: Boolean;
  42. var
  43.   i: Integer;
  44.   FileStart: Boolean;
  45.   Param, FileExt: string;
  46. begin
  47.   Result := False;
  48.   if ParamCount = 0 then Exit;
  49.   FileName := '';
  50.   for i := 1 to ParamCount do
  51.   begin
  52.     Param := ParamStr(i);
  53.     FileStart := not (Param[1] in ['-', '/']);
  54.     if FileStart then
  55.     begin
  56.       if FileName = '' then FileName := Param
  57.       else FileName := FileName + ' ' + Param;
  58.       // strip open and/or close quote if present
  59.       if (FileName[1] = '"') then
  60.       begin
  61.         if (FileName[Length(FileName)] = '"') then
  62.           FileName := Copy(FileName, 2, Length(FileName) - 2)
  63.         else if FileName[1] = '"' then Delete(FileName, 1, 1);
  64.       end;
  65.     end
  66.     else
  67.     begin
  68.       if Length(Param) < 2 then Exit;
  69.       case Param[2] of
  70.         'U', 'u': RegAction := raUnreg;
  71.         'Q', 'q': QuietMode := True;
  72.         'T', 't': RegType := rtTypeLib;
  73.       end;
  74.     end;
  75.   end;
  76.   FileExt := ExtractFileExt(FileName);
  77.   if FileExt = '' then raise Exception.CreateFmt(SNeedFileExt, [FileName]);
  78.   if RegType <> rtTypeLib then
  79.   begin
  80.     if CompareText(FileExt, '.TLB') = 0 then RegType := rtTypeLib
  81.     else if CompareText(FileExt, '.EXE') = 0 then RegType := rtExeLib
  82.     else RegType := rtAxLib;
  83.   end;
  84.   Result := True;
  85. end;
  86.  
  87. procedure RegisterAxLib;
  88. begin
  89.   LibHandle := LoadLibrary(PChar(FileName));
  90.   if LibHandle = 0 then raise Exception.CreateFmt(SLoadFail, [FileName]);
  91.   try
  92.     @RegProc := GetProcAddress(LibHandle, ProcName[RegAction]);
  93.     if @RegProc = Nil then
  94.       raise Exception.CreateFmt(SCantFindProc, [ProcName[RegAction],
  95.         FileName]);
  96.     if RegProc <> 0 then
  97.       raise Exception.CreateFmt(SRegFail, [ProcName[RegAction], FileName]);
  98.     OutputStr(Format(SRegSuccessful, [ProcName[RegAction]]))
  99.   finally
  100.     FreeLibrary(LibHandle);
  101.   end;
  102. end;
  103.  
  104. procedure RegisterTLB;
  105. const
  106.   RegMessage: array[TRegAction] of string = (SRegStr, SUnregStr);
  107. var
  108.   WFileName, DocName: WideString;
  109.   TypeLib: ITypeLib;
  110.   LibAttr: PTLibAttr;
  111.   DirBuffer: array[0..MAX_PATH] of char;
  112. begin
  113.   if ExtractFilePath(FileName) = '' then
  114.   begin
  115.     GetCurrentDirectory(SizeOf(DirBuffer), DirBuffer);
  116.     FileName := '\' + FileName;
  117.     FileName := DirBuffer + FileName;
  118.   end;
  119.   if not FileExists(FileName) then
  120.     raise Exception.CreateFmt(SFileNotFound, [FileName]);
  121.   WFileName := FileName;
  122.   OleCheck(LoadTypeLib(PWideChar(WFileName), TypeLib));
  123.   OutputStr(Format(STlbName, [WFileName]));
  124.   OleCheck(TypeLib.GetLibAttr(LibAttr));
  125.   try
  126.     OutputStr(Format(STlbGuid, [GuidToString(LibAttr^.Guid)]) + #13#10);
  127.     if RegAction = raReg then
  128.     begin
  129.       OleCheck(TypeLib.GetDocumentation(-1, nil, nil, nil, @DocName));
  130.       DocName := ExtractFilePath(DocName);
  131.       OleCheck(RegisterTypeLib(TypeLib, PWideChar(WFileName), PWideChar(DocName)));
  132.     end
  133.     else begin
  134.       OleAutLib := GetModuleHandle('OLEAUT32.DLL');
  135.       if OleAutLib <> 0 then
  136.         @UnRegTlbProc := GetProcAddress(OleAutLib, 'UnRegisterTypeLib');
  137.       if @UnRegTlbProc = nil then raise Exception.Create(SCantUnregTlb);
  138.       with LibAttr^ do
  139.         OleCheck(UnRegTlbProc(Guid, wMajorVerNum, wMinorVerNum, LCID, SysKind));
  140.     end;
  141.   finally
  142.     TypeLib.ReleaseTLibAttr(LibAttr);
  143.   end;
  144.   OutputStr(Format(STlbRegSuccessful, [RegMessage[RegAction]]));
  145. end;
  146.  
  147. procedure RegisterEXE;
  148. var
  149.   SI: TStartupInfo;
  150.   PI: TProcessInformation;
  151. begin
  152.   FillChar(SI, SizeOf(SI), 0);
  153.   SI.cb := SizeOf(SI);
  154.   Win32Check(CreateProcess(PChar(FileName), PChar(FileName + ExeFlags[RegAction]),
  155.     nil, nil, True, 0, nil, nil, SI, PI));
  156.   CloseHandle(PI.hThread);
  157.   CloseHandle(PI.hProcess);
  158. end;
  159.  
  160. begin
  161.   try
  162.     if not DecodeOptions then
  163.       raise Exception.Create(SAbout + #13#10 + SUsage);
  164.     OutputStr(SAbout);
  165.     if not FileExists(FileName) then
  166.       raise Exception.CreateFmt(SFileNotFound, [FileName]);
  167.     case RegType of
  168.       rtAxLib: RegisterAxLib;
  169.       rtTypeLib: RegisterTLB;
  170.       rtExeLib: RegisterEXE;
  171.     end;
  172.   except
  173.     on E:Exception do OutputStr(E.Message);
  174.   end;
  175. end.
  176.