Diono
asked on
OpenProcessToken and win98
Hi,
I am executing the following code in win98 and get the following error when the return value is passed to Win32Check:- the function can only be run in win32 mode.
The offending function seems to be OpenProcessToken, because the last message(I inserted) is 'got here 4'(in the code below). All that I want to do is get the username and domain he has logged onto under win 98.
Many Thanks,
Dion.
procedure GetForegroundProcessUser(o ut UserName, UserDomain: string);
var
Wnd: THandle;
ProcessID: Cardinal;
Process, ProcessToken: THandle;
Buffer: Pointer;
BufferSize: Cardinal;
UserNameSize, UserDomainSize, NameUse: Cardinal;
aRes: bool;
begin
showmessage('got here 0');
Wnd := GetForegroundWindow;
showmessage('got here 1');
ProcessID := 0;
GetWindowThreadProcessId(W nd, @ProcessID);
showmessage('got here 2');
Process := OpenProcess(PROCESS_ALL_AC CESS, False, ProcessID);
try
showmessage('got here 3');
aRes := OpenProcessToken(Process, TOKEN_QUERY, ProcessToken);
showmessage('got here 4');
sysutils.Win32Check(aRes);
showmessage('got here 5');
try
Buffer := nil;
BufferSize := 0;
GetTokenInformation(Proces sToken, TokenUser, nil, 0, BufferSize);
showmessage('got here 6');
if GetLastError <> ERROR_INSUFFICIENT_BUFFER then
RaiseLastWin32Error;
Buffer := AllocMem(BufferSize);
try
// Win32Check(GetTokenInforma tion(Proce ssToken, TokenUser, Buffer, BufferSize, BufferSize));
UserNameSize := 0;
UserDomainSize := 0;
NameUse := 0;
showmessage('got here 7');
LookupAccountSid(nil, PSidAndAttributes(Buffer)^ .Sid, nil, UserNameSize, nil, UserDomainSize, NameUse);
if GetLastError <> ERROR_INSUFFICIENT_BUFFER then
RaiseLastWin32Error;
showmessage('got here 7');
SetLength(UserName, UserNameSize - 1);
SetLength(UserDomain, UserDomainSize - 1);
// Win32Check(LookupAccountSi d(nil, PSidAndAttributes(Buffer)^ .Sid, PChar(UserName), UserNameSize,
// PChar(UserDomain), UserDomainSize, NameUse));
finally
FreeMem(Buffer);
end;
finally
CloseHandle(ProcessToken);
end;
finally
CloseHandle(Process);
end;
end;
I am executing the following code in win98 and get the following error when the return value is passed to Win32Check:- the function can only be run in win32 mode.
The offending function seems to be OpenProcessToken, because the last message(I inserted) is 'got here 4'(in the code below). All that I want to do is get the username and domain he has logged onto under win 98.
Many Thanks,
Dion.
procedure GetForegroundProcessUser(o
var
Wnd: THandle;
ProcessID: Cardinal;
Process, ProcessToken: THandle;
Buffer: Pointer;
BufferSize: Cardinal;
UserNameSize, UserDomainSize, NameUse: Cardinal;
aRes: bool;
begin
showmessage('got here 0');
Wnd := GetForegroundWindow;
showmessage('got here 1');
ProcessID := 0;
GetWindowThreadProcessId(W
showmessage('got here 2');
Process := OpenProcess(PROCESS_ALL_AC
try
showmessage('got here 3');
aRes := OpenProcessToken(Process, TOKEN_QUERY, ProcessToken);
showmessage('got here 4');
sysutils.Win32Check(aRes);
showmessage('got here 5');
try
Buffer := nil;
BufferSize := 0;
GetTokenInformation(Proces
showmessage('got here 6');
if GetLastError <> ERROR_INSUFFICIENT_BUFFER then
RaiseLastWin32Error;
Buffer := AllocMem(BufferSize);
try
// Win32Check(GetTokenInforma
UserNameSize := 0;
UserDomainSize := 0;
NameUse := 0;
showmessage('got here 7');
LookupAccountSid(nil, PSidAndAttributes(Buffer)^
if GetLastError <> ERROR_INSUFFICIENT_BUFFER then
RaiseLastWin32Error;
showmessage('got here 7');
SetLength(UserName, UserNameSize - 1);
SetLength(UserDomain, UserDomainSize - 1);
// Win32Check(LookupAccountSi
// PChar(UserDomain), UserDomainSize, NameUse));
finally
FreeMem(Buffer);
end;
finally
CloseHandle(ProcessToken);
end;
finally
CloseHandle(Process);
end;
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well, for retrieving the username, you can use the GetUsername API in Windows. Or this function:
function UserName: string;
var
AName: PChar;
ASize: DWORD;
begin
ASize := 128;
AName := StrAlloc(ASize);
if Windows.GetUserName(AName, ASize) then
Result := AName
else
Result := '';
StrDispose(AName);
end;
For the domain name you'd normally use SSPI which is described by MS at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthn/security/sspi.asp but I don't know how well this is supported by any Windows 98 system. Since the default W98 system doesn't have any user validation, I do think you have a helluvajob checking all the requirements. Anyway, I once had the same task as you and just told my employer that it would take too much time to develop. I advised him to put W2K as a minimum requirement for our software and he listened to that advise...
function UserName: string;
var
AName: PChar;
ASize: DWORD;
begin
ASize := 128;
AName := StrAlloc(ASize);
if Windows.GetUserName(AName,
Result := AName
else
Result := '';
StrDispose(AName);
end;
For the domain name you'd normally use SSPI which is described by MS at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthn/security/sspi.asp but I don't know how well this is supported by any Windows 98 system. Since the default W98 system doesn't have any user validation, I do think you have a helluvajob checking all the requirements. Anyway, I once had the same task as you and just told my employer that it would take too much time to develop. I advised him to put W2K as a minimum requirement for our software and he listened to that advise...
ASKER
Thanks,
Dion.