home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2002 April
/
Chip_2002-04_cd1.bin
/
zkuste
/
delphi
/
kolekce
/
d56
/
NT.ZIP
/
Demos
/
MainDemo
/
Unit1.pas
< prev
Wrap
Pascal/Delphi Source File
|
2002-01-18
|
13KB
|
354 lines
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, NTServerGetInfo, NTAllAccess, NTUserName, NTLogon,
NTGetUserDomain, NTDisconnectDlg, NTConnectDlg, NTComputerName,
NTChangePassword, NTAddUser, NTGetUserInfo, NTLocalGetInfo,
NTAddLocalGroup, NTWksUserGetInfo, NTWksGetInfo, NTSecurityDecls, NTTools;
type
TForm1 = class(TForm)
NTWksGetInfo1: TNTWksGetInfo;
NTWksUserGetInfo1: TNTWksUserGetInfo;
NTAddLocalGroup1: TNTAddLocalGroup;
NTLocalGetInfo1: TNTLocalGetInfo;
NTGetUserInfo1: TNTGetUserInfo;
NTAddUser1: TNTAddUser;
NTChangePassword1: TNTChangePassword;
NTComputerName1: TNTComputerName;
NTConnectDlg1: TNTConnectDlg;
NTDisconnectDlg1: TNTDisconnectDlg;
NTGetUserDomain1: TNTGetUserDomain;
NTLogon1: TNTLogon;
NTUserName1: TNTUserName;
NTAllAccess1: TNTAllAccess;
NTServerGetInfo1: TNTServerGetInfo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Button9: TButton;
Button10: TButton;
Button11: TButton;
Button12: TButton;
Button13: TButton;
Button14: TButton;
Button15: TButton;
GroupBox1: TGroupBox;
Edit1: TEdit;
Label1: TLabel;
Button16: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button10Click(Sender: TObject);
procedure Button11Click(Sender: TObject);
procedure Button12Click(Sender: TObject);
procedure Button13Click(Sender: TObject);
procedure Button14Click(Sender: TObject);
procedure Button15Click(Sender: TObject);
procedure Button16Click(Sender: TObject);
function GetErrorMessage(Code:Integer):String;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
// Use TNTConnectDlg to display the Network Connection Dialog
// Windows SDK calls used: WNetConnectionDialog
NTConnectDlg1.Execute;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
// Use TNTConnectDlg to display the Disconnect Network Dialog
// Windows SDK calls used: WNetDisconnectDialog
NTDisconnectDlg1.Execute;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
// Use TNTLogon to log onto a domain or local machine
// Windows SDK calls used: LogonUser
// method 1
NTLogon1.UserName:='guest';
NTLogon1.Password:='guest';
NTLogon1.DomainName:='.'; // use local machine
NTLogon1.LogonProvider:=LOGON32_PROVIDER_DEFAULT;
NTLogon1.LogonType:=LOGON32_LOGON_NETWORK;
NTLogon1.Logon;
If NTLogon1.ErrorCode<>0 Then
Begin
Application.MessageBox(PChar(GetErrorMessage(NTLogon1.ErrorCode)),'NTTools Demo',mb_OK + mb_ICONINFORMATION);
Exit;
End;
// method 2
NTLogon1.UserName:='guest@MyDomain';
NTLogon1.Password:='guest';
NTLogon1.DomainName:=''; // keep blank if domain is included in username with @
NTLogon1.LogonProvider:=LOGON32_PROVIDER_DEFAULT;
NTLogon1.LogonType:=LOGON32_LOGON_NETWORK;
NTLogon1.Logon;
If NTLogon1.ErrorCode<>0 Then
Begin
Application.MessageBox(PChar(GetErrorMessage(StrToInt(Edit1.Text))),'NTTools Demo',mb_OK + mb_ICONINFORMATION);
Exit;
End;
// method 3
NTLogon1.UserName:='guest';
NTLogon1.Password:='guest';
NTLogon1.DomainName:='MyDomain';
NTLogon1.LogonProvider:=LOGON32_PROVIDER_DEFAULT;
NTLogon1.LogonType:=LOGON32_LOGON_NETWORK;
NTLogon1.Logon;
If NTLogon1.ErrorCode<>0 Then
Begin
Application.MessageBox(PChar(GetErrorMessage(NTLogon1.ErrorCode)),'NTTools Demo',mb_OK + mb_ICONINFORMATION);
Exit;
End;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
// Use TNTGetUserDomain to retrieve the domain name where an account is kept
// Windows SDK calls used: LookupAccountName
NTGetUserDomain1.UserID:='guest';
NTGetUserDomain1.Execute;
Application.MessageBox(PChar('Domain:' + #10 + NTGetUserDomain1.DomainName),'NTTools Demo',mb_OK + mb_ICONINFORMATION);
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
// Use TNTUserName to retrieve the current user
// Windows SDK calls used: GetUserName
Application.MessageBox(PChar('Current User:' + #10 + NTUserName1.UserName),'NTTools Demo',mb_OK + mb_ICONINFORMATION);
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
// Use TNTAllAccess to modify a process's DACL information to allow any other process to end it
// Windows SDK calls used: InitializeSecurityDescriptor, SetSecurityDescriptorDACL, SetKernelObjectSecurity
NTAllAccess1.Execute;
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
// Use TNTServerGetInfo to retrieve server information
// Windows SDK calls used: NetServerGetInfo
// Method 1
NTServerGetInfo1.Server:='\\GVOS'; //NT 4 and earlier needs \\ in front of servername
NTServerGetInfo1.Execute;
// Method 2
NTServerGetInfo1.Server:=''; // local computer
NTServerGetInfo1.Execute;
// see TNTServerGetInfo.pas for all items returned
// values returned in TNTServerGetInfo.ServerType:
// SV_TYPE_WORKSTATION 0x00000001 All LAN Manager workstations
// SV_TYPE_SERVER 0x00000002 All LAN Manager servers
// SV_TYPE_SQLSERVER 0x00000004 Any server running with Microsoft SQL Server
// SV_TYPE_DOMAIN_CTRL 0x00000008 Primary domain controller
// SV_TYPE_DOMAIN_BAKCTRL 0x00000010 Backup domain controller
// SV_TYPE_TIMESOURCE 0x00000020 Server running the Timesource service
// SV_TYPE_AFP 0x00000040 Apple File Protocol servers
// SV_TYPE_NOVELL 0x00000080 Novell servers
// SV_TYPE_DOMAIN_MEMBER 0x00000100 LAN Manager 2.x Domain Member
// SV_TYPE_LOCAL_LIST_ONLY 0x40000000 Servers maintained by the browser
// SV_TYPE_PRINT 0x00000200 Server sharing print queue
// SV_TYPE_DIALIN 0x00000400 Server running dial-in service
// SV_TYPE_XENIX_SERVER 0x00000800 Xenix server
// SV_TYPE_MFPN 0x00004000 Microsoft File and Print for Netware
// SV_TYPE_NT 0x00001000 Windows NT (either Workstation or Server)
// SV_TYPE_WFW 0x00002000 Server running Windows for Workgroups
// SV_TYPE_SERVER_NT 0x00008000 Windows NT non-DC server
// SV_TYPE_POTENTIAL_BROWSER 0x00010000 Server that can run the Browser service
// SV_TYPE_BACKUP_BROWSER 0x00020000 Server running a Browser service as backup
// SV_TYPE_MASTER_BROWSER 0x00040000 Server running the master Browser service
// SV_TYPE_DOMAIN_MASTER 0x00080000 Server running the domain master Browser
// SV_TYPE_DOMAIN_ENUM 0x80000000 Primary Domain
// SV_TYPE_WINDOWS 0x00400000 Windows 95 or later
// SV_TYPE_ALL 0xFFFFFFFF All servers
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
// Use TNTChangePassword to change a user's password
// Windows SDK calls used: NetUserChangePassword
NTChangePassword1.UserName:='guest';
NTChangePassword1.DomainName:='.';
NTChangePassword1.OldPassword:='guest';
NTChangePassword1.NewPassword:='xxx';
NTChangePassword1.Execute;
end;
procedure TForm1.Button9Click(Sender: TObject);
begin
// Use TNTAddLocalGroup to add groups to the local machine
// Windows SDK calls used: NetLocalGroupAdd
NTAddLocalGroup1.Server:='.';
NTAddLocalGroup1.GroupName:='NewGroup';
NTAddLocalGroup1.Comment:='NT Tools Added This Group';
NTAddLocalGroup1.Execute;
end;
procedure TForm1.Button10Click(Sender: TObject);
begin
// Use TNTWksUserGetInfo to retrieve user workstation information
// Windows SDK calls used: NetWkstaUserGetInfo
NTWksUserGetInfo1.Execute;
// see TNTWksUserGetInfo.pas for all items returned
end;
procedure TForm1.Button11Click(Sender: TObject);
begin
// Use TNTWksUserGetInfo to retrieve workstation information
// Windows SDK calls used: NetWkstaGetInfo
// Method 1
NTWksGetInfo1.Server:='.'; // use default server
NTWksGetInfo1.Execute;
// Method 2
NTWksGetInfo1.Server:='MyServerName'; // use specific server
NTWksGetInfo1.Execute;
// see NTWksGetInfo.pas for all items returned
end;
procedure TForm1.Button12Click(Sender: TObject);
begin
// Use TNTLocalGetInfo to retrieve user workstation information
// Windows SDK calls used: NetLocalGroupGetInfo
// Method 1
NTLocalGetInfo1.Server:='.'; // use default server
NTLocalGetInfo1.Execute;
// Method 2
NTLocalGetInfo1.Server:='MyServerName'; // use specific server
NTLocalGetInfo1.Execute;
// see NTLocalGetInfo.pas for all items returned
end;
procedure TForm1.Button13Click(Sender: TObject);
begin
// Use TNTComputerName to retrieve the current computer name
// Windows SDK calls used: GetComputerName
Application.MessageBox(PChar('Computer Name:' + #10 + NTComputerName1.ComputerName),'NTTools Demo',mb_OK + mb_ICONINFORMATION);
end;
procedure TForm1.Button14Click(Sender: TObject);
begin
// Use TNTAddUser to add a user to a group
// Windows SDK calls used: NetUserAdd
// Method 1
NTAddUser1.Server:='.'; // use local machine
NTAddUser1.UserName:='NewUser';
NTAddUser1.Password:='secret';
NTAddUser1.Privileges:=USER_PRIV_USER; // see NetUserAdd in Windows SDK for all values
NTAddUser1.Flags:=UF_NORMAL_ACCOUNT; // see NetUserAdd in Windows SDK for all values
NTAddUser1.HomeDir:='C:\Temp';
NTAddUser1.Comment:='This is a new user';
NTAddUser1.Execute;
// Method 2
NTAddUser1.Server:='\\GVOS'; // use specific server
NTAddUser1.UserName:='NewUser';
NTAddUser1.Password:='secret';
NTAddUser1.Privileges:=USER_PRIV_USER; // see NetUserAdd in Windows SDK for all values
NTAddUser1.Flags:=UF_NORMAL_ACCOUNT; // see NetUserAdd in Windows SDK for all values
NTAddUser1.HomeDir:='C:\Temp';
NTAddUser1.Comment:='This is a new user';
NTAddUser1.Execute;
// see NTAddUser.pas for all items returned
end;
procedure TForm1.Button15Click(Sender: TObject);
begin
// Use TNTGetUserInfo to add a user to a group
// Windows SDK calls used: NetUserGetInfo
NTGetUserInfo1.UserName:='guest';
NTGetUserInfo1.Server:='.'; // use local server
// see NTGetUserInfo.pas for all items returned
end;
function TForm1.GetErrorMessage(Code:Integer):String;
var
hErrLib:THandle;
msg:PChar;
flags:Integer;
function MAKELANGID(p,s:word):Integer;
begin
result:=(s shl 10) or p
end;
begin
hErrLib:=LoadLibraryEx ('netmsg.dll', 0, LOAD_LIBRARY_AS_DATAFILE);
try
flags:=FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_IGNORE_INSERTS or FORMAT_MESSAGE_FROM_SYSTEM;
if hErrLib<>0 then flags := flags or FORMAT_MESSAGE_FROM_HMODULE;
if FormatMessage(flags,pointer(hErrLib),code,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),PChar(@msg),0,Nil)<>0 Then
try
result:=msg;
finally
LocalFree(Integer(msg));
end
finally
if hErrLib <> 0 then FreeLibrary (hErrLib)
end
end;
procedure TForm1.Button16Click(Sender: TObject);
var Tmp:String;
begin
Tmp:=GetErrorMessage(StrToInt(Edit1.Text));
Application.MessageBox(PChar(Tmp),'NTTools Demo',mb_OK + mb_ICONINFORMATION);
end;
end.