Solved

WMI code fails when app runs as NT Service

Posted on 2004-09-27
9
203 Views
Last Modified: 2013-11-13
I have a Visual Basic application that has been written to run as a nt service, at this point the app works perfectly.
I need to add WMI code to the application for reporting purposes, but the moment I add the code the service goes into a frozen state and fails to go on.
I suspect it has to to with authentication of WMI. I'm running the service as localsystem. (PS: WMI code works fine when running as app)
Can anyone help?
0
Comment
Question by:xy15973
[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
  • 4
  • 3
9 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 12161749
What WMI calls do you make?
Is it a driver or interactive service?
How do you identify failure?

Change the service from automatic to manual to see if it works when started after a logon.

If you do any network accesses, authentication is likely required, since the local system account is just that - local.
0
 

Author Comment

by:xy15973
ID: 12166511
Sorry, maybe I should have been more specific. This service is to retrieve info (Registry and WMI) from the local machine and write it to a DB.

I'm doing calls to Win32_OperatingSystem
Is it not really a driver or interactive service, it is purely for reporting purposes that runs on a schedule that needs to report in whether someone in logged on or not.
Before I added the WMI calls the existing reporting to the DB were working fine. This gets done at a specific time I specify.

Network access authentication is fine as the Authentication to my DB is fine. My problem seems to be authentication to the local machine when trying to access it, And it only seems to affect the WMI calls I make and not the Registry API calls I make.
I have even tried a domain account that has admin rights to the local machine.

As I mentioned before, the machine reports in 100% if I don't have the WMI calls in my app.

Thanks,
RK
0
 
LVL 22

Expert Comment

by:cookre
ID: 12167198
It may be permissions, rather than authentication.  Some API calls (that's eventually where WMI goes) require permissions that you, as local system, can grant yourself.  Some of these permissions can be specified in the API calls themselves, while some must be installed in your security token via something like this snippet that grants itself shutdown privilege for a reboot(Ok, it's c#, but you'll get the idea):


hProc=Process.GetCurrentProcess().Handle;
hToken=IntPtr.Zero;
OpenProcessToken(hProc,40,ref hToken); // 40=TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY
PrivLuid=new LUID();
try {
    if (0==LookupPrivilegeValue("","SeShutdownPrivilege",ref PrivLuid))
       {
       ...Unable to look up privilege value
       }
    }
catch (Exception e)
      {
      ...oops
      }
TknPriv.PrivilegeCount=1; // Set just one
TknPriv.Privileges.Attributes=2; // SE_PRIVILEGE_ENABLED;
TknPriv.Privileges.pLuid=PrivLuid;
NewPriv=new TOKEN_PRIVILEGES();
int silly=4;
try {
    if (0==AdjustTokenPrivileges(hToken,0,ref TknPriv,16,ref NewPriv,ref silly))
       {
       ...Unable to adjust privilege value
       }
    }
catch (Exception e)
      {
      ...oops
      }


Which WMI calls do you make that fail (I've made many in services without any problems)?
0
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!

 
LVL 22

Expert Comment

by:cookre
ID: 12167228
I do note that each method of the Win32_OperatingSystem class requires special permissions, but I wouldn't expect you to have any problems accessing any of the various properties.
0
 

Author Comment

by:xy15973
ID: 12167743
I've tried what you suggested, still no luck

Dim objWMIService As WbemScripting.SWbemServicesEx
Dim objSet As WbemScripting.SWbemObjectSet
Dim objItem As WbemScripting.SWbemObjectEx
Dim iEncType As Integer
Dim strComputer As String

strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & strComputer & "\root\cimv2")

Set objSet = objWMIService.ExecQuery("Select Caption from Win32_OperatingSystem", , 48)
For Each objItem In objSet
    OSName = objItem.Caption
Next
Set objItem = Nothing
Set objSet = Nothing

iEncType = 2
Set objSet = objWMIService.ExecQuery("Select ChassisTypes from Win32_SystemEnclosure", , 48)
For Each objItem In objSet
    iEncType = objItem.ChassisTypes(0)
Next
Set objItem = Nothing
Set objSet = Nothing
0
 
LVL 22

Expert Comment

by:cookre
ID: 12170311
Where, and how, does it fail?
0
 

Author Comment

by:xy15973
ID: 12215097
Thank you for the help, unfortuantely I still had the same problem after trying different angles to solve the issue.
I then tried moving my app to .NET to see if I could get it working in that environment and SUCCESS!!
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 12453884
PAQed, with points refunded (500)

Computer101
E-E Admin
0

Featured Post

Technology Partners: 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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

740 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