Solved

WMI code fails when app runs as NT Service

Posted on 2004-09-27
9
202 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
autoit - check if option is checked in another program 2 127
numbers ascending pyramid 101 191
Path to Python 9 118
Advice on Xojo as a development tool over VB. 4 52
I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

813 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

11 Experts available now in Live!

Get 1:1 Help Now