Solved

OpenProcessToken and win98

Posted on 2004-08-10
6
313 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

Title # Comments Views Activity
Dev Express grid collapse 2 52
How to convert memory stream to PDF file 6 216
RESTRequest Parameter 4 77
migrate this code to work on android 1 44
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…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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