Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


OpenProcessToken and win98

Posted on 2004-08-10
Medium Priority
Last Modified: 2010-04-05

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,

procedure GetForegroundProcessUser(out UserName, UserDomain: string);
  Wnd: THandle;
  ProcessID: Cardinal;
  Process, ProcessToken: THandle;
  Buffer: Pointer;
  BufferSize: Cardinal;
  UserNameSize, UserDomainSize, NameUse: Cardinal;
  aRes: bool;

 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);
    showmessage('got here 3');
    aRes := OpenProcessToken(Process, TOKEN_QUERY, ProcessToken);
    showmessage('got here 4');
    showmessage('got here 5');
      Buffer := nil;
      BufferSize := 0;
      GetTokenInformation(ProcessToken, TokenUser, nil, 0, BufferSize);
      showmessage('got here 6');
      if GetLastError <> ERROR_INSUFFICIENT_BUFFER then
      Buffer := AllocMem(BufferSize);
//        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
        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));
Question by:Diono
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
LVL 17

Accepted Solution

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

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.


Author Comment

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

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;
  AName: PChar;
  ASize: DWORD;
  ASize := 128;
  AName := StrAlloc(ASize);
  if Windows.GetUserName(AName, ASize) then
    Result := AName
    Result := '';

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...

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

604 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