Solved

NT Service Continued....

Posted on 2001-07-07
3
374 Views
Last Modified: 2010-04-06
I posted a question recently that was answered pretty quickly...

--- from previous question ---

http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=20145483

I had written a small monitoring program for Windows 98 and have started to convert it into a service
for NT.

So far it looks good, i've used a template from http://www.jgsoftware.com/nt.htm -> the vpcsrvce.zip and the service installs, accesses
all my modules and functions from the form and everything is great...until... i use the FindWindow command.

--- end from previous question ---

I am still working on this application, i've come accross two problems.

(1) The GetUserName function returns 'SYSTEM' always, because my service is started from the system account. Is there a way to retrieve the logged on user name?

(2) Everynow and then when logged in with a standard user account (not the account i installed the service from) i get a message box popping up saying 'The operation completed successfully' with the title 'GetLastError'. I know this is coming from the standard uService.pas routines but i'm not sure where from.... so how can i get rid of these popups?

Thanks.
0
Comment
Question by:frangers99
3 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 200 total points
ID: 6262360
(1)

A rough example of impersonating the logged-in user in a service:

function GetCurrentUser: String;
var h: HWND;
    dwProcessId: DWORD;
    hProcess, hToken: THandle;
    a: array[0..255] of Char;
    s: Cardinal;
begin
  h := FindWindow('Progman', nil);
  GetWindowThreadProcessId(h, @dwProcessId);
  hProcess := OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);
  OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, hToken);
  ImpersonateLoggedOnUser(hToken);
  s := sizeof(a);
  GetUserName(a, s);
  Result := a;
  RevertToSelf;
  CloseHandle(hToken);
  CloseHandle(hProcess);
end;


Maybe you can try

  h := GetDesktopWindow;

instead of

  h := FindWindow('Progman', nil);


(2)

Remove some writeln's...
0
 

Author Comment

by:frangers99
ID: 6311097
i tried the get user name function.... havent thoroughly tested it yet, but seems to work fine. I still can't track down that GetLastError msg.

One other problem... how do you make an attempt to close the main form the same as trying to close the process (ie like every other delphi application!)
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 8702565
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept Epsylon's comment as answer

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Thank you,
Russell

EE Cleanup Volunteer
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

16 Experts available now in Live!

Get 1:1 Help Now