Solved

NT Service Continued....

Posted on 2001-07-07
3
379 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month6 days, 2 hours left to enroll

627 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