Solved

WMI code fails when app runs as NT Service

Posted on 2004-09-27
9
199 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
  • 4
  • 3
9 Comments
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:xy15973
Comment Utility
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
Comment Utility
Where, and how, does it fail?
0
 

Author Comment

by:xy15973
Comment Utility
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
Comment Utility
PAQed, with points refunded (500)

Computer101
E-E Admin
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
changePi Challenge 15 75
array11 challenge 16 50
wordmultiple challenge 12 87
How can i compile this github project?? 2 38
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 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

10 Experts available now in Live!

Get 1:1 Help Now