Рейтинговые книги
Читем онлайн Советы по Delphi. Версия 1.4.3 от 1.1.2001 - Валентин Озеров

Шрифт:

-
+

Интервал:

-
+

Закладка:

Сделать
1 ... 59 60 61 62 63 64 65 66 67 ... 123

  begin

   if EqualSid(ptg^.Groups[iGroup].Sid, psidAdmin) then begin

    Result := TRUE;

   break;

  end;

  Inc(iGroup);

 end;

 FreeSid(psidAdmin);

end;

Два метода в одном флаконе:

#include

#include

#include

#pragma hdrstop

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

// My thanks to Jerry Coffin ([email protected])

// 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[i].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;

}

//****************************************************************************// 

Как узнать язык Windows по умолчанию?

Одной строкой 

Nomadic лаконично отвечает:

GetSystemDefaultLCID

GetLocaleInfo

GetLocalUserList — возвращает список пользователей (Windows NT, Windows 2000)

Кондратюк Виталий предлагает следующий код:

unit Func;

interface

uses Sysutils, Classes, Stdctrls, Comctrls, Graphics, Windows;

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

{$EXTERNALSYM NetUserEnum}

function NetUserEnum(servername: LPWSTR; level, filter: DWORD; bufptr: Pointer; prefmaxlen: DWORD; entriesread, totalentries, resume_handle: LPDWORD): DWORD; stdcall; external 'NetApi32.dll' Name 'NetUserEnum';

function NetApiBufferFree(Buffer: Pointer{LPVOID}): DWORD; stdcall; external 'NetApi32.dll' Name 'NetApiBufferFree';

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

procedure GetLocalUserList(ulist: TStringList);

implementation

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

// возвращает список пользователей локального хоста

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

procedure GetLocalUserList(ulist: TStringList);

const

 NERR_SUCCESS                     =  0;

 FILTER_TEMP_DUPLICATE_ACCOUNT    =  $0001;

 FILTER_NORMAL_ACCOUNT            =  $0002;

 FILTER_PROXY_ACCOUNT             =  $0004;

 FILTER_INTERDOMAIN_TRUST_ACCOUNT =  $0008;

 FILTER_WORKSTATION_TRUST_ACCOUNT =  $0010;

 FILTER_SERVER_TRUST_ACCOUNT      =  $0020;

type

 TUSER_INFO_10 = record

usri10_name, usri10_comment, usri10_usr_comment, usri10_full_name: PWideChar;

 end;

 PUSER_INFO_10 = ^TUSER_INFO_10;

var

 dwERead, dwETotal, dwRes, res: DWORD;

 inf: PUSER_INFO_10;

 info: Pointer;

 p: PChar;

 i: Integer;

begin

 if ulist=nil then Exit;

 ulist.Clear;

 info  := nil;

 dwRes := 0;

 res := NetUserEnum(nil, 10, FILTER_NORMAL_ACCOUNT, @info, 65536, @dwERead, @dwETotal, @dwRes);

 if (res<>NERR_SUCCESS) or (info=nil) then Exit;

 p := PChar(info);

 for i:=0 to dwERead-1 do begin

  inf := PUSER_INFO_10(p + i*SizeOf(TUSER_INFO_10));

  ulist.Add(WideCharToString(PWideChar((inf^).usri10_name)));

 end;

 NetApiBufferFree(info);

end;

end

Каков способ обмена информацией между приложениями Win32 – Win16?

Nomadic предлагает следующее:

Пользуйтесь сообщением WM_COPYDATA.

Для Win16 константа определена как $004A, для Win32 смотрите в WinAPI Help.

#define WM_COPYDATA 0x004A

/*

* lParam of WM_COPYDATA message points to…

*/

typedef struct tagCOPYDATASTRUCT {

 DWORD dwData;

 DWORD cbData;

 PVOID lpData;

} COPYDATASTRUCT, *PCOPYDATASTRUCT;

Остановка и запуск сервисов

Postmaster предлагает следующий код:

Unit1.dfm

object Form1: TForm1

 Left = 192

 Top = 107

 Width = 264

 Height = 121

 Caption = 'Сервис'

 Color = clBtnFace

 Font.Charset = DEFAULT_CHARSET

 Font.Color = clWindowText

 Font.Height = -11

 Font.Name = 'MS Sans Serif'

 Font.Style = []

 OldCreateOrder = False

 PixelsPerInch = 96

 TextHeight = 13

 object Label1: TLabel

  Left = 2

  Top = 8

  Width = 67

  Height = 13

  Caption = 'Имя сервиса'

 end

 object Button1: TButton

  Left = 4

  Top = 56

  Width = 95

  Height = 25

  Caption = 'Стоп сервис'

  TabOrder = 0

  OnClick = Button1Click

 end

 object Button2: TButton

  Left = 148

  Top = 56

  Width = 95

  Height = 25

  Caption = 'Старт сервис'

  TabOrder = 1

  OnClick = Button2Click

 end

 object Edit1: TEdit

  Left = 0

  Top = 24

  Width = 241

  Height = 21

  TabOrder = 2

  Text = 'Messenger'

 end

end

Unit1.pas

unit Unit1;

interface

uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Winsvc;

type TForm1 = class(TForm)

 Button1: TButton;

 Button2: TButton;

 Edit1: TEdit;

 Label1: TLabel;

 procedure Button1Click(Sender: TObject);

 procedure StopService(ServiceName: String);

 procedure Button2Click(Sender: TObject);

 procedure StartService(ServiceName: String);

private

{ Private declarations }

public

{ Public declarations }

end;

var Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);

begin

 StopService(Edit1.Text);

end;

procedure TForm1.StopService(ServiceName: String);

var

 schService, schSCManager: DWORD;

 p: PChar;

 ss: _SERVICE_STATUS;

begin

 p:=nil;

 schSCManager:= OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);

 if schSCManager = 0 then RaiseLastWin32Error;

 try

  schService:=OpenService(schSCManager, PChar(ServiceName), SERVICE_ALL_ACCESS);

  if schService = 0 then RaiseLastWin32Error;

  try

   if not ControlService(schService, SERVICE_CONTROL_STOP, SS) then RaiseLastWin32Error;

  finally

   CloseServiceHandle(schService);

  end;

 finally

  CloseServiceHandle(schSCManager);

 end;

1 ... 59 60 61 62 63 64 65 66 67 ... 123
На этой странице вы можете бесплатно читать книгу Советы по Delphi. Версия 1.4.3 от 1.1.2001 - Валентин Озеров бесплатно.

Оставить комментарий