473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Use WMI into a Windows Service

1
Hi all.
I'm trying to realise a service that can install a printer queue given an ip address, a queue name and a driver name. The driver is already installed on the workstation.
The service has to be installed on every workstation.
The ip address is used to create the TCP/IP port.

I tried to realise all this in a c# program (.NET Framework 4.0) and it works like a charm. (all items are created, it runs under my own account)

I post some sample code below (the TCP/IP port creation):

Expand|Select|Wrap|Line Numbers
  1.  //create port
  2. ManagementClass portClass = new ManagementClass("Win32_TCPIPPrinterPort");
  3. ManagementObject portObject = portClass.CreateInstance();
  4.  
  5. string portName = "IP_" + ip;
  6. portObject["Name"] = portName;
  7.                                 portObject["HostAddress"] = ip;
  8. portObject["PortNumber"] = 9100;
  9. portObject["Protocol"] = 1;
  10. portObject["SNMPCommunity"] = "public";
  11. portObject["SNMPEnabled"] = true;
  12. portObject["SNMPDevIndex"] = 1;
  13.  
  14. PutOptions options = new PutOptions();
  15. options.Type = PutType.UpdateOrCreate;
  16. //put a newly created object to WMI objects set             
  17. portObject.Put(options);
  18.  
Then I tried to put the same code in a Windows service. The service watches a folder and if there is a new file reads it to retrieve information. After reading, it tries to create the TCP port and the queue.

The service always gives "Access denied" exception on the portObject.Put(options) line (in the code I posted below). I tried to execute it both as LOCAL SYSTEM and my own account, but it is the same.

I have Windows XP Pro SP3 32bit. Note that on the laptop of a colleague of mine the same service is actually working!

I tried also to put a fixed user:

Expand|Select|Wrap|Line Numbers
  1. ConnectionOptions co = new ConnectionOptions();
  2. co.Impersonation = ImpersonationLevel.Impersonate;
  3. co.Authentication = AuthenticationLevel.Packet;
  4. co.Timeout = new TimeSpan(0, 0, 30);
  5. co.EnablePrivileges = true;
  6. co.Username = "myusername";
  7. co.Password = "mypassword";
  8.  
and in this case I have an exception stating: "User credentials cannot be used for local connections".

I'm going crazy with this... anybody has any idea? any help will be greatly appreciated.

Best regards.
Feb 7 '12 #1
0 1312

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Michael Riggio | last post by:
Is there a way to have a windows service instantiate a class that is a web service, which will then be accessible to clients via HTTP? Thanks, -Mike
1
by: Artur Kowalski | last post by:
I have a NotifyIcon in my Windows Service project and I am trying to add a ContextMenu to this NotifyIcon or use some of the mouse events. Everything isn't working. I think so base class of the...
3
by: belgiozen | last post by:
Hi, I have a working windows service,it is looking for files on the disk and when some of the files are cupdated it calls an executable. But it takes a lot of time(about 10 minutes) to run the...
2
by: Neslihan ERDEM | last post by:
Every body Hi first of all I say Why do I need Windows Service / Every Day I create XML file . I writed a XML web service And .I join this servis Windows service. I create Windows Service that...
9
by: SP | last post by:
Hi All, I wrote a windows service which is supposed to stop after specified amount of time. I am calling OnStop() after specified time. OnStop() methods executed but I dont see the service...
3
by: Nathan Kovac | last post by:
I have a feeling I am missing something simple, but I just can't find it. Perhaps someone can give me a lead on where to look. I will describe the issue then post my code to the web service. My...
3
by: Doug Bailey | last post by:
I am trying to control a Windows Service via a Web Service interface. (I am developing in .NET2003) I am using the ServiceController object which allows me to read the state of the services with...
2
by: deko | last post by:
When to use a privileged user thread rather than a windows service? That's the question raised in a previous post . It was suggested that if the service needs to interact with a WinForms app...
5
by: sonali_reddy123 | last post by:
Hello all, I am trying to develop an application which will run as a windows service. The application should have Normal options available with service like start, stop and pause but along...
5
by: dm3281 | last post by:
I'm really starting to hate writing services -- or trying to, anyway. Why do I need to rename my project to the service name? Why do I need to set the "ServiceName" property to my service name?...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.