Is there a way for Windows Service to Read/Write HKCU Reg key ?

Hi:

I'm writing a Windows Service program which needs to read/write the 
HKEY_CURRENT_USER registry key for the current logged on user. I know that 
for a Windows service does not have concept of HKCU since it is always 
running in SYSTEM's context. But is there a way (trick?) for a service to 
access the HKCU registry key?

Thanks In Advance !
Polaris 


0
etpolaris (100)
6/10/2005 5:51:45 AM
vc.mfc 33608 articles. 0 followers. Follow

6 Replies
1396 Views

Similar Articles

[PageSpeed] 14

Hello

Try, from a user application (the one that is logged on, or the current 
user), to send the services the user's SID.

Then access the HKCU through HKEY_USERS\users_sid\............

Hope that helps,
Elias
"Polaris" <etpolaris@hotmail.com> wrote in message 
news:OPbH9BYbFHA.3132@TK2MSFTNGP09.phx.gbl...
> Hi:
>
> I'm writing a Windows Service program which needs to read/write the 
> HKEY_CURRENT_USER registry key for the current logged on user. I know that 
> for a Windows service does not have concept of HKCU since it is always 
> running in SYSTEM's context. But is there a way (trick?) for a service to 
> access the HKCU registry key?
>
> Thanks In Advance !
> Polaris
> 


0
lallous (134)
6/10/2005 7:01:38 AM
Hi Polaris!

> I'm writing a Windows Service program which needs to read/write the 
> HKEY_CURRENT_USER registry key for the current logged on user. I know that 
> for a Windows service does not have concept of HKCU since it is always 
> running in SYSTEM's context. But is there a way (trick?) for a service to 
> access the HKCU registry key?

1. Get a token to an user for example via:
    - LogonUser
    - OpenProcessToken
    - DuplcateToken

2. Use "LoadUserProfile" to load the user-profile.
    Then you have the handle to the "HKEY_CURRENT_USER"
    registry key in the "PROFILEINFO.Profile" entry
    (with KEY_ALL_ACCESS).

-- 
Greetings
   Jochen

    My blog about Win32 and .NET
    http://blog.kalmbachnet.de/
0
6/10/2005 7:42:06 AM
I'm thinking it would make more sense to update the HKEY_USERS hive
instead.  Get the SID of the logged-on user.

Polaris wrote:
> Hi:
>
> I'm writing a Windows Service program which needs to read/write the
> HKEY_CURRENT_USER registry key for the current logged on user. I know that
> for a Windows service does not have concept of HKCU since it is always
> running in SYSTEM's context. But is there a way (trick?) for a service to
> access the HKCU registry key?
> 
> Thanks In Advance !
> Polaris

0
6/10/2005 6:22:50 PM
Or if you already have the user's token and you don't want to force
profile load in your service (that is, you just want to use whatever
HKCU is currently associated with the token), you should call the
RegOpenCurrentUser API:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/regopencurrentuser.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/services_and_the_registry.asp

-- 
This posting is provided "AS IS" with no warranties, and confers no
rights.

"Jochen Kalmbach [MVP]" wrote:

>> I'm writing a Windows Service program which needs to read/write the 
>> HKEY_CURRENT_USER registry key for the current logged on user. I know 
>> that for a Windows service does not have concept of HKCU since it is 
>> always running in SYSTEM's context. But is there a way (trick?) for a 
>> service to access the HKCU registry key?
>
> 1. Get a token to an user for example via:
>    - LogonUser
>    - OpenProcessToken
>    - DuplcateToken
>
> 2. Use "LoadUserProfile" to load the user-profile.
>    Then you have the handle to the "HKEY_CURRENT_USER"
>    registry key in the "PROFILEINFO.Profile" entry
>    (with KEY_ALL_ACCESS).


0
pavel1 (2)
6/12/2005 7:45:21 AM
Thanks for your help guys!

After many tries, it "balsamically" works (accessing current user's HKCU 
within a NT service), here is a sequence of APIs called:

LogonUser
LoadUserProfile
ImpersonateLoggedonUser
Use the hProfile obtained from LoadUserProfile to access the HKCU.
RevertToSelf
UnloadUserProfile
CloseHandle(hToken).

The reason it "basically works" is that I have to popup a message to make 
the whole thing work, without popping up the message, RegQueryValue will 
fail with "File not found" error. I'm still trying to find out why popping 
up a message makes such difference. Any hint is welcome.

Thanks in Advance!
Polaris


"Polaris" <etpolaris@hotmail.com> wrote in message 
news:OPbH9BYbFHA.3132@TK2MSFTNGP09.phx.gbl...
> Hi:
>
> I'm writing a Windows Service program which needs to read/write the 
> HKEY_CURRENT_USER registry key for the current logged on user. I know that 
> for a Windows service does not have concept of HKCU since it is always 
> running in SYSTEM's context. But is there a way (trick?) for a service to 
> access the HKCU registry key?
>
> Thanks In Advance !
> Polaris
> 


0
etpolaris (100)
6/15/2005 7:32:45 AM
If you have an agent running as the user call your service, then you
can simply impersonate (e.g. RpcImpersonateClient, etc), then call
RegOpenCurrentUser().

"Polaris" <etpolaris@hotmail.com> wrote in message 
news:OMbB5xXcFHA.3036@TK2MSFTNGP10.phx.gbl...
> Thanks for your help guys!
>
> After many tries, it "balsamically" works (accessing current user's HKCU 
> within a NT service), here is a sequence of APIs called:
>
> LogonUser
> LoadUserProfile
> ImpersonateLoggedonUser
> Use the hProfile obtained from LoadUserProfile to access the HKCU.
> RevertToSelf
> UnloadUserProfile
> CloseHandle(hToken).
>
> The reason it "basically works" is that I have to popup a message to make 
> the whole thing work, without popping up the message, RegQueryValue will 
> fail with "File not found" error. I'm still trying to find out why popping 
> up a message makes such difference. Any hint is welcome.
>
> Thanks in Advance!
> Polaris
>
>
> "Polaris" <etpolaris@hotmail.com> wrote in message 
> news:OPbH9BYbFHA.3132@TK2MSFTNGP09.phx.gbl...
>> Hi:
>>
>> I'm writing a Windows Service program which needs to read/write the 
>> HKEY_CURRENT_USER registry key for the current logged on user. I know 
>> that for a Windows service does not have concept of HKCU since it is 
>> always running in SYSTEM's context. But is there a way (trick?) for a 
>> service to access the HKCU registry key?
>>
>> Thanks In Advance !
>> Polaris
>>
>
> 


0
richardw1 (1)
6/20/2005 5:18:31 AM
Reply:

Similar Artilces:

How to Change the file associated with IE's Windows Media Player Add-on
Hello, My home computer is not playing streaming videos that are embedded in websites correctly, while my office computer can. Both machines use Internet Explorer as the web browser. I compare their settings under IE's "Manage Add-ons" and find one difference between them. At home, the file associated with Windows Media Player under the IE Manage Add-on dialog box is msdxm.ocx. At work, the file is wmpdxm.dll. I want to change the home computer IE Manage Add-on setting to be the same as that for office computer. How can I change the setting to point to th...

capturing the keyboard keys
hi, i have to capture the message which are rasied when ever there key press occur on keyboard.i have to catch that message and process it and send it back as usual for exmaple: if in keyboard we press" k" then we have to find it and transform as "L" and transform to what ever current application. How can i do that one by, koti "Koti" <koti@nannacomputers.com> wrote in message news:%23jeS8MIiFHA.1464@TK2MSFTNGP14.phx.gbl... > hi, > > i have to capture the message which are rasied when ever there key > press occur on keyboard.i...

Best Way ?
G'day All, Looking for some information. With Outlook you can have all your contacts...address, email address, yada, yada, yada. Can I use this information in to say mail merge with Word and email a letter to the ppl with email addresses? Or is this better done with a Access? The reason I ask is that we want to deliver an email to all our clients with the latest promotions. And want it to be a bit more automated? Any information or pointers in the right direction would be appreciated. Have a good one, Scott V You can use either Access or Outlook as the data source for a ma...

Find Window
Hi, I did exactly as Microsoft wants me to do (according to Oneapp32 sample) in order to create only one instance of my program. In my app class in Init App I register the class, but it never finds it (when one instance is already running) in Init instance using FindWindow(className, NULL). Please help me, thaks, Janiv. "Janiv Ratson" <janiv@aoe6.net> �������/�������� � �������� ���������: news:%235yQsG7dEHA.2888@TK2MSFTNGP10.phx.gbl... > Hi, > I did exactly as Microsoft wants me to do (according to Oneapp32 sample) in > order to create only one instance of my pr...

Control-Home key behaviour
Not neccessarily a coding issue but this forum has a far greater knowledge depth than others. Recently the above key combination rather than making cell A1 the active cell now makes the column A cell in the current row the active cell. Ctrl_Home in VBE achieves the correct result, that is, the cursor is relocated to the first line & character of the module. I have tried 'application.onkey "^{Home}" "" ' in the intermediate window but without any effect. I have also removed any add-ins and disabled auto opening macro files. Any clues ? -- Ken ...

Is there a way to color code Outlook Contact File Folder Labels?
Is there a way to color code Outlook Contact File Folder Labels? No. -- Diane Poremsky [MVP - Outlook] Author, Teach Yourself Outlook 2003 in 24 Hours Coauthor, OneNote 2003 for Windows (Visual QuickStart Guide) Author, Google and Other Search Engines (Visual QuickStart Guide) Outlook Tips: http://www.outlook-tips.net/ Outlook & Exchange Solutions Center: http://www.slipstick.com Join OneNote Tips mailing list: http://www.onenote-tips.net/ "isailhbca" <isailhbca@discussions.microsoft.com> wrote in message news:4DBD10F7-FAA0-4102-A4C3-E2697908EFE8@microsoft.com... >...

A way to create sub-categories?
I want to group contacts by category and then a sub-category for easy viewing. Apart from creating a new field for sub-category is there a way of doing this? Outlook doesn't seem to support sub-categories. Thanks Nope. -- Patricia Cardoza Outlook MVP Author - Special Edition Using Microsoft Office Outlook 2003 Lead Author - Access 2003 VBA Programmer's Reference Author - Absolute Beginner's Guide to Microsoft OneNote 2003 http://blogs.officezealot.com/cardoza http://www.cardozasolutions.com "Casebase" <junkmail@casebase.net> wrote in message news:%23rx5a...

Report showing which users have access to a particular window/repo
Is there a report that can be printed (or a particular table that can be looked at) that will list all users that have access to a particular window or report? We're on GP 10, upgraded from GP 9, so all the roles and tasks are CNV_..... In particular, we need a quick and easy way to see which users have access the the payroll check history report. Sure there is. Abreviated instructions follow but detailed ones can be found in our Dynamics GP Security Handbook on page II-38. Load the Security Resource Table first by going to Maintenance, select Clear Tables (BE CAREFUL)...

shortcut key question!
Can someone tell me what the shortcut key to paste values only? I thought it would be easy to find but it is proving to be elusive. -- Message posted from http://www.ExcelForum.com This site has a good list of shortcut key combos. However, I don't believe there is a paste value only short cut. http://www.cpearson.com/excel/KeyboardShortcuts.htm You may want to try editing your toolbars and add a button for paste-special. Then all you would have to do is select values only and ok. >-----Original Message----- >Can someone tell me what the shortcut key to paste values only? &...

is there a way to crop a corner out of a rectangular image in Visi
Using subtract on two shapes accomplishes what i want to do, but when the first shape is a .gif image, it does not work. No, the "boolean operations", as they're called, do not operate on foreign objects like bitmaps and controls. Visio has crop functionality for imported graphics, but it is only rectangular. You could draw a small white triangle and give it "no line". Then place it over the corner of your bitmap. -- Hope this helps, Chris Roth Visio MVP "doug" <doug@discussions.microsoft.com> wrote in message news:29D479A6-6099-4051-B7A0-1...

Multiple Browser Windows
Just recently, when clicking a hyperlink in some of the e-mail I recieve, the brower, IE6, will open up two browser windows. One window will be completely blank, the other will of course load the web page I was linking to. I'm not sure if anyone else has encountered this problem, and I didn't see any postings regarding this issue, but any help that you can provide would be greatly appreciated. I am using Outlook 2000, Windows XP Pro, IE6. ...

Merge/consolidate text cells based on unique keys ?
Hi all. I hope someone can help me out. I have an Excel worksheet with 2 columns: 'Client #' and 'Invoice #'. Every time the accounting dept. generates an invoice, a new row is added in this worksheet. Obviously this is chronological not per Client #. But for the sake of simplicity, let's assume the worksheet is already sorted by Client #, like so: A B Client # Invoice # 231 5929 231 4358 231 2185 231 6234 464 1166 464 1264 464 3432 464 1720 464 9747 791 1133 791 4930 791 5496 791 6291 989 8681 989 3023 989 7935 989 8809 989 8873 My goal is to achieve...

Outlook 2003
I have a user who is reporting that whilst creating a new mail/ replying to a mail, it will occassionally blank out all the text in the mail. If he minimizes or maximizes the mail he can breifly see all the text he has typed, but it dissapears straight away. I've not managed to recreate teh problem myself, but am hoping this is a general issue with a simple answer. Thanks in advance. ...

is there a way to change the time card repor format?
i'm using activereports in RMS to generate a custome timecard report. for my timein and timeout colums, the datatype i have assigned to the columns is vbDate. when I generate the report, the columns only show the date as mm/dd/yyyy, they do not show the time of day piece for exactly when the cashier punched in or out. is there any way to change the formatting of this so that in the report the time shows up? for example, instead of showing 2/6/2005 in the time in column, it would show 2/5/2005 8:00:00 AM. thanks in advance, kevin There is a timecard report on the Reports Library...

Slow to Read Mail
Please help!!! When I open outlook and scroll from one message to another, or click on a message, it takes about a second and a half or longer to display the message. This is even if I read one message, go to a 2nd, and then return to the first (when the first one should still be in the system's cache). How do I get this program to run faster? Oh, this happens when the system is not linked to a network too. Its running on a 1.7 gighz laptop with 512 meg SDRAM. Thanks, Mark Is this Outlook 2002, by any chance? If so, try disabling the integration with Instant Messenger in To...

best way to deserialize xml
I'm at a quandry as to what's the best/fastest way to deserialize an incoming xml stream? I ran a small test on xmlserializer and on a small file it performed 10 times worse then when I used the XMLDocument and deserialized the values myself. Is the xmlserializer really that slow? I'm expecting a file about 1 MB in length and if it's that slow on a 2 K file then I can't phantom how it's going to behave on a 1 MB file. Should I just skip the xmlserializer and XML Dom and go straight to xmlreader and do the deserialization myself? This has been discussed a few times...

Can You Count This Way?
I have some cells with note type data: For ex: ax,3b7,22,xy,zz, Is there a way to count every item between the commas? Thanks. Hi try =LEN(A1)-LEN(SUBSTITUTE(A1,",","")+LEN(A1)>0 -- Regards Frank Kabel Frankfurt, Germany Timothy Wright wrote: > I have some cells with note type data: > > For ex: > > ax,3b7,22,xy,zz, > > Is there a way to count every item between the commas? > > Thanks. On Wed, 19 May 2004 20:13:53 +0200, "Frank Kabel" <frank.kabel@freenet.de> wrote: >Hi >try >=LEN(A1)-LEN(SUBSTITUTE(A1,&q...

Is there a way to Sort data by color in Excel?
I am trying to sort data by color, is that possible? see http://www.xldynamic.com/source/xld.ColourCounter.html#sorting -- HTH Bob Phillips "SRTUC" <SRTUC@discussions.microsoft.com> wrote in message news:B5121232-FB1D-46AF-94F8-04B7EAA2D90C@microsoft.com... > I am trying to sort data by color, is that possible? You're gonna need a User defined function to get the value of a color. See Chip Pearson's site for a nice version: http://www.cpearson.com/excel/colors.htm Look for: Returning The ColorIndex Of A Cell Then you could use that UDF in a separate colu...

Fastest way to load an xml file into an array in .net
Hi All VS 2005 SP1 (C#, VB.NET) I need the absolut fastest way of loading an xml-file of about 700KB into an array. I need to load about 1500 of those xml-Files into arrays on startup of my application. Therefore it needs to be as fast as possible. Please also tell me if the xml structure can be improved. the xml looks like: y: Year m: Month h: Hour value <data> <v y="1998" m="1" h="1">0.227777109151971</v> <v y="1998" m="1" h="2">0.206573903224972</v> <v y="1998" m="1"...

The "read mail" icon in toolbar
When I press the "read mail" envelope in the toolbar nothing happens. All the other icons work ok, but not this one. It would help if you explained where you are seeing that "Read Mail" = button. I'm going to assume it is on the toolbar of Internet Explorer. It sounds like your Windows Mail doesn't have all its defaults. Open the Default Programs applet, which you can access either from the Start menu or via the Control Panel, then click the first item: "Set your default programs." After a few seconds, a list of programs comes up. Click on &qu...

Is there a convenient way to save data
Is there a convenient way to save dialog data so it remembers the users last selection? I have a dialog based application that only has three user variables all of which are ints. What is the best way to make theses variables persistent for a given user on a given machine? Thanks "Jeff B" <jb@nospan.net> wrote in message news:fpDif.10451$Ay6.2336@fe13.lga... > > Is there a convenient way to save dialog data so it remembers the users last > selection? I have a dialog based application that only has three user > variables all of which are ints. What is the best w...

MFC custom window message parameters
I need to send a message from C++ class, using SendMessage(hWin, WM_MY, wParam, lParam) to one of MFC class. Please, give me an example where afx message has parameters wParam, and lParam, like this: void OnMyMessage(wParam, lParam) { } How to declare, and to manage the parameters if different from LOWORD and HIWORD. Thanks ??? Robert wrote: > I need to send a message from C++ class, using SendMessage(hWin, WM_MY, > wParam, lParam) to one of MFC class. > > Please, give me an example where afx message has parameters wParam, and > lParam, like this: > void OnMyMes...

Email service for members...
We are an association (ABC.org) that offers an email service to our members that allows them to sign up for an email address that ends in the same domain as that of the staff at our headquarters. For ex, a doctor could sign up for: drJohnson@ABC.org and provide a 3rd party email of drjohnson@yahoo.com so all email sent to the @ABC.org is forwarded to the yahoo account. Currently, our ISP maintains a list of all doctors that participate in this service and filters for email sent to those email addresses and fwd's them to the 3rd party address so they never reach our Exchange server. ...

Is there a way
G'Day, Is there a way to turn off NDR's in Ex5.5 SP4 on an NT server running SP6a ? Also, can the ability for the server to receive TELNET connections be turned off? My server isn't an open relay, but, I know it's being used to SPAM using DNR's and I can see TELNET connections in the log files.... Thank You, Admin_In_Syd i know that u can turn off the ndrs to be sent from exchange at the standard poliy setting, thats how i configured it at exchange 2000 .... there must be also a setting at exch 5.5, actually i dont remeber it .... "Steve@Home" <sprob@...

Windows Live shuts down (No warning)
Recently and I mean within the last 2 days I noticed that when I start my Windows Live and I click on one of my 3 email accounts it closes... not warning just brings me back to the Desktop. Happens more often when I select 1 certain hotmail account (I have 2 added to Windows Live). It takes about 3-4 tries of restarting Live to get it to work. Of course if I deleted any emails from my other 2 accounts they reappear as if I didn't remove anything. Its very strange. So I tried to see if there was an update, but since I don't have IE as my default browser MS was giving m...