WMI code fails when app runs as NT Service

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?
xy15973Asked:
Who is Participating?
 
Computer101Connect With a Mentor Commented:
PAQed, with points refunded (500)

Computer101
E-E Admin
0
 
cookreCommented:
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
 
xy15973Author Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
cookreCommented:
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
 
cookreCommented:
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
 
xy15973Author Commented:
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
 
cookreCommented:
Where, and how, does it fail?
0
 
xy15973Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.