Einzelnen Beitrag anzeigen

CalganX

Registriert seit: 21. Jul 2002
Ort: Bonn
5.403 Beiträge
 
Turbo Delphi für Win32
 
#5

Re: Dateiendungen registrieren

  Alt 21. Nov 2004, 21:29
Und für den Fall, dass der ausführende User keine Adminrechte hat, hat der ehemalige User notErnie folgenden Source zusammengestellt, der allerdings von MathiasSimmack's Klasse (vorherige Beitragsanhang) unabhängig ist.
Delphi-Quellcode:
//----von Mathias Simmacks "IsAdmin.inc" (TFileTypeRegistration.zip) geklaut:

function GetAdminSid: PSID;
const
  // bekannte SIDs ... (WinNT.h)
  SECURITYNTAUTHORITY: TSIDIdentifierAuthority = (Value: (0, 0, 0, 0, 0, 5));
  // bekannte RIDs ... (WinNT.h)
  SECURITYBUILTINDOMAINRID: DWORD = $00000020;
  DOMAINALIASRIDADMINS: DWORD = $00000220;
begin
  Result := nil;
  AllocateAndInitializeSid(SECURITYNTAUTHORITY,
    2,
    SECURITYBUILTINDOMAINRID,
    DOMAINALIASRIDADMINS,
    0,
    0,
    0,
    0,
    0,
    0,
    Result);
end;

//----von Mathias Simmacks "IsAdmin.inc" (TFileTypeRegistration.zip) geklaut:

function IsAdmin: LongBool;
var
  TokenHandle: THandle;
  ReturnLength: DWORD;
  TokenInformation: PTokenGroups;
  AdminSid: PSID;
  Loop: Integer;
  wv: TOSVersionInfo;
begin
  wv.dwOSVersionInfoSize := sizeof(TOSversionInfo);
  GetVersionEx(wv);

  Result := (wv.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS);

  if (wv.dwPlatformId = VER_PLATFORM_WIN32_NT) then
  begin
    TokenHandle := 0;
    if OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, TokenHandle) then
    try
      ReturnLength := 0;
      GetTokenInformation(TokenHandle, TokenGroups, nil, 0, ReturnLength);
      TokenInformation := GetMemory(ReturnLength);
      if Assigned(TokenInformation) then
      try
        if GetTokenInformation(TokenHandle, TokenGroups,
          TokenInformation, ReturnLength, ReturnLength) then
        begin
          AdminSid := GetAdminSid;
          for Loop := 0 to TokenInformation^.GroupCount - 1 do
          begin
            if EqualSid(TokenInformation^.Groups[Loop].Sid, AdminSid) then
            begin
              Result := True; break;
            end;
          end;
          FreeSid(AdminSid);
        end;
      finally
        FreeMemory(TokenInformation);
      end;
    finally
      CloseHandle(TokenHandle);
    end;
  end;
end;

//------------------------

//////////////////////////

function WVersion: string;
var
  OSInfo: TOSVersionInfo;
begin
  Result := '3X';
  OSInfo.dwOSVersionInfoSize := sizeof(TOSVERSIONINFO);
  GetVersionEx(OSInfo);
  case OSInfo.dwPlatformID of
    VER_PLATFORM_WIN32S: begin
        Result := '3X';
        Exit;
      end;
    VER_PLATFORM_WIN32_WINDOWS: begin
        Result := '9X';
        Exit;
      end;
    VER_PLATFORM_WIN32_NT: begin
        Result := 'NT';
        Exit;
      end;
  end; //case
end;

procedure registerfiletype(Handle: HWnd; ft, key, desc, icon, prg: string);
// Benutzung z.b. für ZIP-Dateien:
// registerfiletype(Handle, '.zip', '', 'Zip-Archiv', paramstr(0) + ',1', paramstr(0));
var
  myReg: TRegIniFile;
  wo: byte;
begin

  if WVersion = '3Xthen
  begin
  //MessageBox(Handle, 'Window 3.x wird nicht unterstützt.', 'Information', mb_IconInformation);
    exit;
  end;

  wo := pos('.', ft);

  while wo > 0 do begin
    delete(ft, wo, 1);
    wo := pos('.', ft);
  end;

  if (ft = '') or (prg = '') then exit;
  if pos('*', ft) <> 0 then exit;
  if pos('?', ft) <> 0 then exit;

  ft := '.' + ft;

  try
    myReg := TRegIniFile.create('');

    if (WVersion = '9X') or (IsAdmin = true) then
    begin
      //MessageBox(Handle, 'Admin-Rechte oder Win9x.', 'Information', mb_IconInformation);
      myReg.RootKey := HKEY_CLASSES_ROOT;
      key := ExtractFileName(Application.ExeName) + copy(ft, 2, maxint);
    end else
    begin
      myReg.RootKey := HKEY_CURRENT_USER;
      key := '\Software\Classes\.' + copy(ft, 2, maxint);
    end;

    myReg.WriteString(ft, '', key);
    myReg.WriteString(key, '', desc);

    if icon <> 'then
      myReg.WriteString(key + '\DefaultIcon', '', icon);

    myReg.WriteString(key + '\shell\open\command', '', '"' + prg + '" "%1"');

  finally
    myReg.free;
  end;

  SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
  MessageBox(Handle, PChar('Der Dateityp "*' + ft + '" wurde mit ' + ExtractFileName(Application.ExeName) + ' verknüpft.'), 'Information', mb_IconInformation);
end;

//////////////////////////
  Mit Zitat antworten Zitat