Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

NT Service Continued....

Posted on 2001-07-07
3
Medium Priority
?
381 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
[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
3 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 800 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

704 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