Solved

OpenProcessToken and win98

Posted on 2004-08-10
6
308 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
  • 2
6 Comments
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now