Article View: borland.public.delphi.nativeapi
Article #12107HOW to check registry key write permission for current user
From: Alex Sporik
Date: Sat, 23 Nov 2002 17:18
Date: Sat, 23 Nov 2002 17:18
98 lines
3382 bytes
3382 bytes
I'm use folowing code for this:
function GetRegistryKeySecurityDescriptor(Key:HKEY;Path:string;var
lsd:DWORD):PSecurityDescriptor;
var
tsd:PSecurityDescriptor;
h:HKEY;
begin
lsd:=0;
Result:=nil;
if RegOpenKey(key,PChar(Path),h)=ERROR_SUCCESS then
begin
lsd:=SizeOf(TSecurityDescriptor);
tsd:=AllocMem(lsd);
InitializeSecurityDescriptor(tsd,SECURITY_DESCRIPTOR_REVISION);
if
RegGetKeySecurity(h,GROUP_SECURITY_INFORMATION,tsd,lsd)=ERROR_INSUFFICIENT_B
UFFER then
begin
ReAllocMem(tsd,lsd);
if
RegGetKeySecurity(h,GROUP_SECURITY_INFORMATION,tsd,lsd)=ERROR_SUCCESS then
if IsValidSecurityDescriptor(tsd) then Result:=tsd;
end;
RegCloseKey(h);
end;
end;
and
function GetRegistryKeyAccess(sd:PSecurityDescriptor;const
DesiredAccess:DWORD):Boolean;
var
th:THandle;
dwPrivilegeLength,dwGrantedAccess,dwDesiredAccess:DWORD;
gmMapping:TGenericMapping;
psPrivilege:TPrivilegeSet;
fStatus:LongBool;
begin
Result:=False;
ImpersonateSelf(SecurityImpersonation);
if OpenThreadToken(GetCurrentThread,TOKEN_QUERY,False,th) then
begin
dwPrivilegeLength:=SizeOf(TPrivilegeSet);
dwGrantedAccess:=0;
dwDesiredAccess:=2;
fStatus:=FALSE;
gmMapping.GenericRead:=1;
gmMapping.GenericWrite:=2;
gmMapping.GenericExecute:=0;
gmMapping.GenericAll:=1 or 2;
if IsValidSecurityDescriptor(sd) then
begin
if
AccessCheck(sd,th,dwDesiredAccess,gmMapping,psPrivilege,dwPrivilegeLength,dw
GrantedAccess,fStatus) then
begin
Result:=fStatus;
end
else
MessageBox(GetDesktopWindow,PChar(SysErrorMessage(GetLastError)),nil,0);
end;
CloseHandle(th);
end;
RevertToSelf;
end;
and use in code
function IsKeyProtected(Key:HKEY;RegPath:string):Boolean;
var
sd:PSecurityDescriptor;
lsd:DWORD;
begin
Result:=False;
if Win32Platform<>VER_PLATFORM_WIN32_NT then Exit; // under Win9x/Me all
keys unprotected
if not KeyExists(Key,RegPath) then Exit;
sd:=GetRegistryKeySecurityDescriptor(Key,RegPath,lsd);
Result:=not GetRegistryKeyAccess(sd,KEY_WRITE);
FreeRegistryKeySecurityDescriptor(sd,lsd);
end;
____________________________________________________________________________
__
Last function says that SecurityDesctriptor is invalid. ??????????? What?
Why IsValidSecurityDescriptor() say OK but I got error in result ????
Does Anybody know what wrong in code above?
Alex
Technical Details
Message-ID:
<3ddfb885@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-05!sn-xit-03!sn-xit-06!sn-xit-04!supernews.com!news.maxwell.syr.edu!fu-berlin.de!news.uni-x.net!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f94dd,5ae98291d23902c9,start X-Google-Attributes: gidf94dd,public X-Google-ArrivalTime: 2002-11-23 10:15:31 PST From: "Alex Sporik" <Alex_Sporik@hotmail.com> Newsgroups: borland.public.delphi.nativeapi Subject: HOW to check registry key write permission for current user Date: Sat, 23 Nov 2002 19:18:20 +0200 Lines: 105 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 NNTP-Posting-Host: 194.158.192.114 Message-ID: <3ddfb885@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1038071941 194.158.192.114 (23 Nov 2002 09:19:01 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-05!sn-xit-03!sn-xit-06!sn-xit-04!supernews.com!news.maxwell.syr.edu!fu-berlin.de!news.uni-x.net!newsgroups.borland.com!not-for-mail