?
Solved

OpenProcessToken and win98

Posted on 2004-08-10
6
Medium Priority
?
317 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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
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
Course of the Month10 days, 4 hours left to enroll

762 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