Solved

NT Service Continued....

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

15 Experts available now in Live!

Get 1:1 Help Now