Solved

OpenProcessToken and win98

Posted on 2004-08-10
6
314 Views
Last Modified: 2010-04-05
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(out 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(Wnd, @ProcessID);
  showmessage('got here 2');
  Process := OpenProcess(PROCESS_ALL_ACCESS, 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(ProcessToken, TokenUser, nil, 0, BufferSize);
      showmessage('got here 6');
      if GetLastError <> ERROR_INSUFFICIENT_BUFFER then
        RaiseLastWin32Error;
      Buffer := AllocMem(BufferSize);
      try
//        Win32Check(GetTokenInformation(ProcessToken, 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(LookupAccountSid(nil, PSidAndAttributes(Buffer)^.Sid, PChar(UserName), UserNameSize,
//          PChar(UserDomain), UserDomainSize, NameUse));
      finally
        FreeMem(Buffer);
      end;
    finally
      CloseHandle(ProcessToken);
    end;
  finally
    CloseHandle(Process);
  end;
end;
0
Comment
Question by:Diono
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 500 total points
ID: 11768582
Read http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthz/security/openprocesstoken.asp this part:

Requirements
Client: Requires Windows XP, Windows 2000 Professional, or Windows NT Workstation 3.1 and later.
Server: Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server 3.1 and later.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Advapi32.lib.

Unfortunately, Windows 98 doesn't support this function and will just return a useless error code... This is not a suprise since W98 doesn't have the same security system as W2K, NT and XP...

I even wonder if you can get the user and domain in Windows 98 because it could run without any security at all... With a single user and no domain.

0
 

Author Comment

by:Diono
ID: 11769971
surely there must be a system variable somewhere on the client holding the domain the user has logged on to!

Thanks,
Dion.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11772149
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...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question