Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using XYNTService project to create my service. When this service runs, inside this service I am reading some registry value located inside HKEY_CURRENT_USER using the project Registry Wrapper Class (CRegistry)[^]. But I am not able to read them inside windows service. If I create any MFC application and then read that registry then I am able to read it. One more thing, my service XYNTService.exe is running as "SYSTEM" inside TaskManager can that be the problem. If that is the problem then where should I make change to run it as "Administrator" [my current user name] in XYNTService project.

Thanks
Rahul
Posted
Updated 13-Jun-23 18:01pm

You are right: the problem stays on how your service is running.
You can fix your problem in two ways:


  • make the service run in the context of the logged-on user and add the flag SERVICE_INTERACTIVE_PROCESS when you create the service (or check the correspondant check-box from the Sevice Control Manager)
  • make the service run in the context of a specific user: in this case when you access the registry subtree HKEY_CURRENT_USER you are accessing the subtree not of the currently logged-on user, but the subtree related to the account you chose to run your service on
 
Share this answer
 
I already have that flag. Here is the code which I use

SC_HANDLE schService = CreateService
	( 
		schSCManager,	/* SCManager database      */ 
		pName,			/* name of service         */ 
		pName,			/* service name to display */ 
		SERVICE_ALL_ACCESS,        /* desired access          */ 
		SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS , /* service type            */ 
		SERVICE_AUTO_START,      /* start type              */ 
		SERVICE_ERROR_NORMAL,      /* error control type      */ 
		pPath,			/* service's binary        */ 
		NULL,                      /* no load ordering group  */ 
		NULL,                      /* no tag identifier       */ 
		NULL,                      /* no dependencies         */ 
		NULL,                      /* LocalSystem account     */ 
		NULL
		); 


How to run service in current user's context?
 
Share this answer
 
Yuppy!! I found the problem why I was not able to read registry.

When I was specifying HKEY_CURRENT_USER in either RegOpenKeyEx or CRegistry::KeyExists then Service was searching inside HKEY_USERS\.default\

I still don't know why it is doing so but I am able to read only those entries which come inside HKEY_USERS\.default\.

But if I specify HKEY_LOCAL_MACHINE then I still don't know where it searches.
Now I am curious about "Will it behave same in all PCs?"

If I specify
("HKEY_CURRENT_USERS","Softwares\\Microsoft")
then it searches for

inside
HKEY_USERS\.default\Softwares\Microsoft


Thanks,
Rahul
 
Share this answer
 
v2
I am also not able to read registry from windows service -
----------------------
dim SubKey as string="key\\subkey"
Dim oeg As Microsoft.Win32.RegistryKey
oeg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", True)
oeg = oReg.CreateSubKey(SubKey)
oeg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\" & SubKey)
if oeg is nothing then
Diagnostics.EventLog.WriteEntry("oeg is nothing")
end if
Dim strD As String = oeg.GetValue("UseSet", "").ToString
Diagnostics.EventLog.WriteEntry(strD.tostring)
-----------

but i am getting nothing in oeg and strDay
I am using windows 7 ultimate 64 bit with Microsoft VS 2010 and .Net 4.0.3xxx
 
Share this answer
 
v2
Comments
rahul.kulshreshtha 25-Oct-10 8:55am    
Make your key inside
HKEY_USERS\.default\Softwares\

and read it as
oeg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", True)
oeg = oReg.CreateSubKey(SubKey)
oeg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\" & SubKey)
gr8Ashish 25-Oct-10 8:59am    
thanx i am now trying what you(Rahul) said. . :)
gr8Ashish 25-Oct-10 9:04am    
But what happened when service is install for all user using the system
This is only for user currently login and install service for him what you said and what i am using before is..

but for other user it not work i think i am still working on it . ..
can't i use hkey_local_machine\\software (but on windows server 2008 ) i am not able to read it via simple applciation(or with windows service also)
gr8Ashish 25-Oct-10 9:20am    
now my checking done registry is working but service is giving error later on
I update it later
but my above question still there that for multi user windows system (and specially windows server 2008/2003) "hkey_users\.default\softwares\ " is working or not ?
thnx for your help . ..
rahul.kulshreshtha 28-Oct-10 9:06am    
I didn't tried it on any server edition.
HKEY_USERS\.default\Softwares\ should be accessed in all users if you want to create user-specific setting then you can create sub keys inside your main key. One sub-key for each user.
LOCAL_MACHINE will not be accessible.
Unfortunately I am not experienced in VB :)
now my service is run no problem with registry thanx to rahul kulshreshtha

now i need to use timer in my service. what i have done is -
-------------------code-------
<small>
Protected Overrides Sub OnStart(ByVal args() As String)
   ...some code..
Reset1:
   Diagnostics.EventLog.WriteEntry("timer start")
   tim1.Enabled = True
   Diagnostics.EventLog.WriteEntry("timer done")
   ...some code .. .
   GoTo Reset1
End Sub

Private Sub tim1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tim1.Tick
   tim1.Enabled = False

   Diagnostics.EventLog.WriteEntry("inside timer")
   ...some code.. calling some other function here
   Diagnostics.EventLog.WriteEntry("quiting to timer")
   tim1.Enabled = true
End Sub
</small>

------------end---

and in event log i get -

------------eventlog entry--
timer start
timer done
timer start
timer done
timer start
timer done
...
...

------------end---

means i am not able get code from tim1_tick()

-----desired event log is ----
timer start
inside timer
quiting to timer
timer done

timer start
inside timer
quiting to timer
timer done
...
...

------end---

Please help me how to use timer ?
i am using dotnet 4.0.3.x on windows 7 ultimate x64 bit with visual studio 2010 professional
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900