Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

WMI code fails when app runs as NT Service

Posted on 2004-09-27
9
Medium Priority
?
206 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Make the most of your online learning experience.
Simple Linear Regression
Progress

715 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