Windows 7 kullanıcı yönetici mi?

Delphi'de kod yazma ile ilgili sorularınızı bu foruma yazabilirsiniz.
Cevapla
Extended
Üye
Mesajlar: 25
Kayıt: 01 Oca 2008 05:49

Windows 7 kullanıcı yönetici mi?

Mesaj gönderen Extended »

Öncelikle merhabalar benim windows 7de registry'e ulaşmam için kullanıcının yönetici izinlerine sahip olması gerekiyor. İnternetten bulduğum bi unit bu işlemi yapıyor güya ama düzgün çalışmıyordu yani hiç bi zaman yönetici değilim yada hep yöneticiyim diyordu. Elinizde bu yönetici olup olmadığını kontrol eden kod varsa ve bana gönderebilirseniz çok sevinirim...
ertemsoft
Üye
Mesajlar: 129
Kayıt: 19 Nis 2005 01:22
İletişim:

Re: Windows 7 kullanıcı yönetici mi?

Mesaj gönderen ertemsoft »

mrb experts-exchange de bulduğum bir kodu yazıyorum size ama denemedim C++ belki bişeyler çıkarabilirsiniz
#include <windows.h>
#include <stdio.h>
#include <lm.h>
#pragma hdrstop



#pragma comment( lib, "netapi32.lib" )



// My thanks to Jerry Coffin (jcoffin@taeus.com)
// for this much simpler method.
bool jerry_coffin_method()
{
bool result;
DWORD rc;
wchar_t user_name[256];
USER_INFO_1 *info;
DWORD size = sizeof( user_name );

GetUserNameW( user_name, &size);

rc = NetUserGetInfo( NULL, user_name, 1, (byte **) &info );
if ( rc != NERR_Success )
return false;

result = info->usri1_priv == USER_PRIV_ADMIN;

NetApiBufferFree( info );
return result;
}



bool look_at_token_method()
{
int found;
DWORD i, l;
HANDLE hTok;
PSID pAdminSid;
SID_IDENTIFIER_AUTHORITY ntAuth = SECURITY_NT_AUTHORITY;

byte rawGroupList[4096];
TOKEN_GROUPS& groupList = *( (TOKEN_GROUPS *) rawGroupList );

if ( ! OpenThreadToken( GetCurrentThread(), TOKEN_QUERY, FALSE, &hTok ) )
{
printf( "Cannot open thread token, trying process token [%lu].\n",
GetLastError() );
if ( ! OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &hTok ) )
{
printf( "Cannot open process token, quitting [%lu].\n",
GetLastError() );
return 1;
}
}

// normally, I should get the size of the group list first, but ...
l = sizeof rawGroupList;
if ( ! GetTokenInformation( hTok, TokenGroups, &groupList, l, &l ) )
{
printf( "Cannot get group list from token [%lu].\n",
GetLastError() );
return 1;
}

// here, we cobble up a SID for the Administrators group, to compare to.
if ( ! AllocateAndInitializeSid( &ntAuth, 2, SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &pAdminSid ) )
{
printf( "Cannot create SID for Administrators [%lu].\n",
GetLastError() );
return 1;
}

// now, loop through groups in token and compare
found = 0;
for ( i = 0; i < groupList.GroupCount; ++ i )
{
if ( EqualSid( pAdminSid, groupList.Groups.Sid ) )
{
found = 1;
break;
}
}

FreeSid( pAdminSid );
CloseHandle( hTok );
return !!found;
}

int main()
{
bool j, l;

j = jerry_coffin_method();
l = look_at_token_method();

printf( "NetUserGetInfo(): The current user is %san Administrator.\n",
j? "": "not " );
printf( "Process token: The current user is %sa member of the Administrators group.\n",
l? "": "not " );

return 0;
}
Bilgi Paylaştıkça Çoğalır
RamazanG
Üye
Mesajlar: 73
Kayıt: 16 Tem 2010 03:38

Re: Windows 7 kullanıcı yönetici mi?

Mesaj gönderen RamazanG »

Kullanıcı Adminmi bu uygulamayla anlayabilirsin, ama istediğin win7 de registry e ulaşmaksa uygulamanda UAC kullanmalısın.

Kod: Tümünü seç

function IsAdmin: Boolean;
var 
  hAccessToken: THandle; 
  ptgGroups: PTokenGroups; 
  dwInfoBufferSize: DWORD; 
  psidAdministrators: PSID; 
  x: Integer; 
  bSuccess: BOOL; 
begin 
  Result := False;
  bSuccess := OpenThreadToken(GetCurrentThread, TOKEN_QUERY, 
                              True,hAccessToken); 
  if not bSuccess then 
  begin 
    if GetLastError = ERROR_NO_TOKEN then 
    bSuccess := OpenProcessToken(GetCurrentProcess, 
                          TOKEN_QUERY,hAccessToken); 
  end; 
  if bSuccess then 
  begin 
    GetMem(ptgGroups, 1024); 
    bSuccess := GetTokenInformation(hAccessToken,   
             TokenGroups,ptgGroups, 1024, dwInfoBufferSize); 
    CloseHandle(hAccessToken); 
    if bSuccess then 
    begin 
      AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2, 
        SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 
        0, 0, 0, 0, 0, 0, psidAdministrators); 
      {$R-} 
      for x := 0 to ptgGroups.GroupCount - 1 do 
        if EqualSid(psidAdministrators, 
                     ptgGroups.Groups[x].Sid) then 
        begin 
          Result := True; 
          Break; 
        end; 
      {$R+} 
      FreeSid(psidAdministrators); 
    end; 
    FreeMem(ptgGroups); 
  end; 
end;
Cevapla