怎样得到当前用户所在的用户组?

fengerfeifei 2003-07-11 12:27:26
怎样得到windows 2000的用户所在的用户组?
...全文
230 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Delphi_Li 2003-07-11
  • 打赏
  • 举报
回复
C++的例子(Bill Gates提供的!!!)

#ifndef UNICODE
#define UNICODE
#endif

#include <stdio.h>
#include <assert.h>
#include <windows.h>
#include <lm.h>

int wmain(int argc, wchar_t *argv[])
{
LPGROUP_USERS_INFO_0 pBuf = NULL;
DWORD dwLevel = 0;
DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
DWORD dwEntriesRead = 0;
DWORD dwTotalEntries = 0;
NET_API_STATUS nStatus;

if (argc != 3)
{
fwprintf(stderr, L"Usage: %s \\\\ServerName UserName\n", argv[0]);
exit(1);
}
//
// Call the NetUserGetGroups function, specifying level 0.
//
nStatus = NetUserGetGroups(argv[1],
argv[2],
dwLevel,
(LPBYTE*)&pBuf,
dwPrefMaxLen,
&dwEntriesRead,
&dwTotalEntries);
//
// If the call succeeds,
//
if (nStatus == NERR_Success)
{
LPGROUP_USERS_INFO_0 pTmpBuf;
DWORD i;
DWORD dwTotalCount = 0;

if ((pTmpBuf = pBuf) != NULL)
{
fprintf(stderr, "\nGlobal group(s):\n");
//
// Loop through the entries;
// print the name of the global groups
// to which the user belongs.
//
for (i = 0; i < dwEntriesRead; i++)
{
assert(pTmpBuf != NULL);

if (pTmpBuf == NULL)
{
fprintf(stderr, "An access violation has occurred\n");
break;
}

wprintf(L"\t-- %s\n", pTmpBuf->grui0_name);

pTmpBuf++;
dwTotalCount++;
}
}
//
// If all available entries were
// not enumerated, print the number actually
// enumerated and the total number available.
//
if (dwEntriesRead < dwTotalEntries)
fprintf(stderr, "\nTotal entries: %d", dwTotalEntries);
//
// Otherwise, just print the total.
//
printf("\nEntries enumerated: %d\n", dwTotalCount);
}
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
//
// Free the allocated buffer.
//
if (pBuf != NULL)
NetApiBufferFree(pBuf);

return 0;
}
tccb 2003-07-11
  • 打赏
  • 举报
回复
可以用 WNetGetUser() 这个函数来得到用户名
可以用 NetUserGetGroups() 这个函数来得到用户组列表
DWGZ 2003-07-11
  • 打赏
  • 举报
回复
unit Unit1;

interface

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

type
USER_INFO_1=record
usri1_name:pwidechar;
usri1_password:pwidechar;
usri1_password_age:dword;
usri1_priv:dword;
usri1_home_dir:pwidechar;
usri1_comment:pwidechar;
usri1_flags:dword;
usri1_script_path:pwidechar;
end;
buffer=^User_info_1;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
function NetUserAdd(Server:PWideChar;Level:DWORD;Buf:pointer;ParmError:dword):LongInt;stdcall; external 'netapi32.dll';

procedure TForm1.Button1Click(Sender: TObject);
var buf:buffer;
rlt:LongInt;
begin
try
getMem(buf,sizeOf(User_info_1));
buf^.usri1_name :='test';
buf^.usri1_password :='test';
buf^.usri1_password_age:=0;
buf^.usri1_priv :=1;
buf^.usri1_home_dir :=nil;
buf^.usri1_comment:=nil;
buf^.usri1_flags :=1;
buf^.usri1_script_path :=nil;

rlt:=NetUserAdd(nil,1,pointer(Buf), 0);
freemem(buf);
except
end;
end;
end.
DWGZ 2003-07-11
  • 打赏
  • 举报
回复
可以参考一下下面的程序下面是添加用户的
DWGZ 2003-07-11
  • 打赏
  • 举报
回复
NET_API_STATUS NetUserGetGroups(
LPWSTR servername,
LPWSTR username,
DWORD level,
LPBYTE *bufptr,
DWORD prefmaxlen,
LPDWORD entriesread,
LPDWORD totalentries
);
belecyrus 2003-07-11
  • 打赏
  • 举报
回复
Delphi_li的代码不错,我喜欢!
不过将PrefMaxLen := $FFFFFFFF; 改成这样更好: PrefMaxLen := Dword(-1);
更好哟!
Delphi_Li 2003-07-11
  • 打赏
  • 举报
回复
naughtyboy((笨!奔!)) :你的程序能用吗???

数据获取不全,并且有内存漏洞!!!
naughtyboy 2003-07-11
  • 打赏
  • 举报
回复
type
Local_Group_Info_0 = record
lgrui0_name : LPCWSTR;
end;

function NetUserGetLocalGroups(servername: LPCWSTR;username: LPCWSTR;level: DWORD;
flags: DWORD;var buffer: Pointer;prefmaxlen: DWORD; entriesread: LPDWORD;totalentries:LPDWORD): DWORD;
stdcall;external 'netapi32.dll';

procedure TMainForm.Button2Click(Sender: TObject);
var
er,te: DWORD;
pp: ^Local_Group_Info_0;
usergrp : string;
begin
NetUserGetLocalGroups(nil,'naughtyboy',0,0,Pointer(pp),1024,@er,@te);
usergrp := WideCharToString(pp^.lgrui0_name);
showmessage(usergrp);
end;

//你只需要自己改改就行了,naughtyboy是用户名
belecyrus 2003-07-11
  • 打赏
  • 举报
回复
楼上的代码没有问题,给分吧!^_^
Delphi_Li 2003-07-11
  • 打赏
  • 举报
回复
我写的代码:

unit Main;

interface

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

type
TFormMain = class(TForm)
EditUser: TEdit;
Label1: TLabel;
Label2: TLabel;
ListGroup: TListBox;
BtnStart: TButton;
BtnClose: TButton;
procedure BtnCloseClick(Sender: TObject);
procedure BtnStartClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
FormMain: TFormMain;

implementation

{$R *.dfm}

Type
NET_API_STATUS = DWORD;

PLocalGroupUserInfo_0 = ^TLocalGroupUserInfo_0;
TLocalGroupUserInfo_0 = Record
GroupName : LPWSTR;
End;

Function NetUserGetLocalGroups( ServerName : LPCWSTR; UserName : LPCWSTR;
Level : DWORD; Flags : DWORD; Buf : Pointer;
PrefMaxLen : DWORD; Var EntriesRead : DWORD;
Var TotalEntries : DWORD ) :
NET_API_STATUS; StdCall; External 'NETAPI32.DLL';
Function NetApiBufferFree( Buffer : Pointer ) :
NET_API_STATUS; StdCall; External 'NETAPI32.DLL';

procedure TFormMain.BtnCloseClick(Sender: TObject);
begin
Close;
end;

procedure TFormMain.BtnStartClick(Sender: TObject);
Const
NERR_Success = 0;
LG_INCLUDE_INDIRECT = 1;
Var
wName : WideString;
dwResult : DWORD;
P : PLocalGroupUserInfo_0;
PrefMaxLen : DWORD;
EntriesRead : DWORD;
TotalEntries : DWORD;
I : Integer;
Buf : Pointer;
wGroup : WideString;
begin
wName := EditUser.Text;
PrefMaxLen := $FFFFFFFF;
EntriesRead := 0;
TotalEntries := 0;
Buf := Nil;
dwResult := NetUserGetLocalGroups( Nil, PWideChar( wName ), 0, LG_INCLUDE_INDIRECT,
@Buf, PrefMaxLen, EntriesRead, TotalEntries );
If dwResult = NERR_Success Then
Begin
If Buf <> Nil Then
Begin
P := PLocalGroupUserInfo_0( Buf );
ListGroup.Items.Clear;
For I := 0 To EntriesRead - 1 Do
Begin
wGroup := P^.GroupName;
ListGroup.Items.Add( wGroup );
Inc( P );
End;
NetApiBufferFree( Buf );
End;
End;
end;

end.
Delphi_Li 2003-07-11
  • 打赏
  • 举报
回复
我帮你吧!!!

20分钟后给你一个Delphi的例子!!!
fengerfeifei 2003-07-11
  • 打赏
  • 举报
回复
大家来帮帮忙啊
fengerfeifei 2003-07-11
  • 打赏
  • 举报
回复
有没有
DELPHI的列子?
fengerfeifei 2003-07-11
  • 打赏
  • 举报
回复
DWGZ()

buf^.grui0_name 返回是'?$#13#7'的窜,不对啊

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧