关于系统编程的问题(200分)

  • 主题发起人 主题发起人 fstao
  • 开始时间 开始时间
F

fstao

Unregistered / Unconfirmed
GUEST, unregistred user!
如果客户端(winxp)没有加入域时,访问服务器(win2000 高级服务器版)时,
就会弹出登录用户名和密码,当我输入服务器的登录用户名和密码时就可进入
服务器。我想用delphi编程的方法访问服务器时输入用户名和密码就可以进入
服务器,请问如何编写代码?
 
已知: User Name、Password、Domain,如何验证该User为该Domain的合法User?
:Pipi. 时间:00-12-9 22:51:48 ID:413980
使用LogonUser看能不能成功。(仅能在nt下使用
不能在win9x上使用)
目前好象只能使用LogonUser,如果你熟悉LSA API的话或许LSA好用一些,但太复杂了。
进程调用LogonUser需要有SE_TCB_NAME特权,在特定情况下也需要
SE_CHANGE_NOTIFY_NAME特权。

获得特权可以参考下面的示例:

if( !OpenProcessToken(
GetCurrentProcess(),
TOKEN_QUERY | TOKEN_ADJUST_DEFAULT | TOKEN_ADJUST_GROUPS | TOKEN_ADJUST_PRIVILEGES,
&hProcessToken ) )
// Error Process;

if( !LookupPrivilegeValueA( NULL, lpPrivilegeName, &(tp.Privileges[0].Luid) ) )
// Error Process;

tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

if( !AdjustTokenPrivileges(
hProcessToken,
FALSE,
&tp,
sizeof(TOKEN_PRIVILEGES) + sizeof( LUID_AND_ATTRIBUTES ) * 2,
NULL,
NULL ) )
// Error Process;

CloseHandle( hProcessToken );
:热水 时间:00-12-11 18:26:27 ID:415513
补充:
Procedure Login(sUserID:WideString;sAPassWord):Boolean;
var
sAID:String;
LL:Handle;
begin
sAID:=sUerID;//重点Pchar不支持WideString
sDomainName:=....//读取注册表

Flag:=LogonUser(pchar(sAID),pchar(sDomainName),pchar(sAPassWord),LOGON32_LOGON_NETWORK,LOGON32_PROVIDER_DEFAULT,LL);
If not Flag then
begin
Result:=False
else
Result:=True;
 
如何知道机器所在域(Domain)以及主域控制器名字?
使用PDC函数即可。
type
NET_API_STATUS = DWORD;

var
NTNetGetDCName : function(Server, Domain: pWideChar; var DC: pWideChar):
NET_API_STATUS; StdCall;
NTNetApiBufferFree: function(lpBuffer: Pointer):
NET_API_STATUS; StdCall;

function GetPDCName: string;
const
NTlib = 'NETAPI32.DLL';
var
pDomain : PWideChar;
LibHandle : THandle;
begin
Result := '';
LibHandle := LoadLibrary(NTlib);
if LibHandle = 0 then
raise Exception.Create('Unable to map library: ' + NTlib);
try
@NTNetGetDCName := GetProcAddress(Libhandle, 'NetGetDCName');
@NTNetApiBufferFree := GetProcAddress(Libhandle, 'NetApiBufferFree');
try
if NTNetGetDCName(nil, nil, pDomain) = 0 then
Result := WideCharToString(pDomain);
finally
NTNetApiBufferFree(pDomain);
end;
finally
FreeLibrary(Libhandle);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GetPDCName);
end;
*************************
下面的函数获取域名:

function GetDomainName: AnsiString;
type
WKSTA_INFO_100 = record
wki100_platform_id: Integer;
wki100_computername: PWideChar;
wki100_langroup: PWideChar;
wki100_ver_major: Integer;
wki100_ver_minor: Integer;
end;

WKSTA_USER_INFO_1 = record
wkui1_username: PChar;
wkui1_logon_domain: PChar;
wkui1_logon_server: PChar;
wkui1_oth_domains: PChar;
end;
type
//Win9X ANSI prototypes from RADMIN32.DLL and RLOCAL32.DLL

TWin95_NetUserGetInfo = function(ServerName, UserName: PChar; Level: DWORD; var
BfrPtr: Pointer): Integer;
stdcall;
TWin95_NetApiBufferFree = function(BufPtr: Pointer): Integer;
stdcall;
TWin95_NetWkstaUserGetInfo = function(Reserved: PChar; Level: Integer; var
BufPtr: Pointer): Integer;
stdcall;

//WinNT UNICODE equivalents from NETAPI32.DLL

TWinNT_NetWkstaGetInfo = function(ServerName: PWideChar; level: Integer; var
BufPtr: Pointer): Integer;
stdcall;
TWinNT_NetApiBufferFree = function(BufPtr: Pointer): Integer;
stdcall;

function IsWinNT: Boolean;
var
VersionInfo: TOSVersionInfo;
begin
VersionInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
Result := GetVersionEx(VersionInfo);
if Result then
Result := VersionInfo.dwPlatformID = VER_PLATFORM_WIN32_NT;
end;
var

Win95_NetUserGetInfo: TWin95_NetUserGetInfo;
Win95_NetWkstaUserGetInfo: TWin95_NetWkstaUserGetInfo;
Win95_NetApiBufferFree: TWin95_NetApiBufferFree;

WinNT_NetWkstaGetInfo: TWinNT_NetWkstaGetInfo;
WinNT_NetApiBufferFree: TWinNT_NetApiBufferFree;

WSNT: ^WKSTA_INFO_100;
WS95: ^WKSTA_USER_INFO_1;

EC: DWORD;
hNETAPI: THandle;
begin
try

Result := '';

if IsWinNT then
begin
hNETAPI := LoadLibrary('NETAPI32.DLL');
if hNETAPI <> 0 then
begin @WinNT_NetWkstaGetInfo := GetProcAddress(hNETAPI, 'NetWkstaGetInfo');
@WinNT_NetApiBufferFree := GetProcAddress(hNETAPI, 'NetApiBufferFree');

EC := WinNT_NetWkstaGetInfo(nil, 100, Pointer(WSNT));
if EC = 0 then
begin
Result := WideCharToString(WSNT^.wki100_langroup);
WinNT_NetApiBufferFree(Pointer(WSNT));
end;
end;
end
else
begin
hNETAPI := LoadLibrary('RADMIN32.DLL');
if hNETAPI <> 0 then
begin @Win95_NetApiBufferFree := GetProcAddress(hNETAPI, 'NetApiBufferFree');
@Win95_NetUserGetInfo := GetProcAddress(hNETAPI, 'NetUserGetInfoA');

EC := Win95_NetWkstaUserGetInfo(nil, 1, Pointer(WS95));
if EC = 0 then
begin
Result := WS95^.wkui1_logon_domain;
Win95_NetApiBufferFree(Pointer(WS95));
end;
end;
end;

finally
if hNETAPI <> 0 then
FreeLibrary(hNETAPI);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GetDomainName);
end;
 
最简单的方法,用NET USE 登陆W2K
NET USE [devicename | *] [//computername/sharename[/volume] [password | *]]
[/USER:[domainname/]username]
[/USER:[dotted domain name/]username]
[/USER:[username@dotted domain name]
[[/DELETE] | [/PERSISTENT:{YES | NO}]]
 
多人接受答案了。
 
后退
顶部