Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

adding printer driver using system.management

577 views
Skip to first unread message

gv

unread,
Jul 30, 2003, 12:06:56 PM7/30/03
to
I have been successful at adding a printer driver and printer using wmi
script, but converting it to c# has been a challenge.
Can anyone help me with the conversion? Below is the script, and below that
is the code I've tried already, along with the error that I'm getting.

The following works great
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & strComputer & "\root\cimv2")
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege"

Set objDriver = objWMIService.Get("Win32_PrinterDriver")

objDriver.Name = "HP LaserJet 4200 PS"
objDriver.SupportedPlatform = "x86"
objDriver.Version = "Windows 2000, Windows XP and Windows Server 2003"
objDriver.FilePath = "C:\\hp4200ps.inf"
objDriver.InfName = "hp4200ps.inf"

intResult = objDriver.AddPrinterDriver(objDriver)
WScript.Echo intResult

The following gives me an error stating "Not found" on the 3rd line below
(SetPropertyValue) as well as setting a property using the brackets []

ManagementClass mc = new ManagementClass("Win32_PrinterDriver");
ManagementBaseObject inParams =
mc.GetMethodParameters("AddPrinterDriver");

inParams.SetPropertyValue("InfName", "hp4200ps.inf");

// inParams["InfName"] ="hp4200ps.inf";
inParams["DriverName"] = "HP LaserJet 4200 PS";
inParams["SupportedPlatform"] = "x86";
inParams["Version"] = "Windows 2000, Windows XP and Windows Server
2003";
inParams["FilePath"] = @"C:\hp4200ps.inf";

ManagementBaseObject outParams = mc.InvokeMethod("AddPrinterDriver",
inParams, null);

uint retVal = (uint)outParams["ReturnValue"];

Console.WriteLine("Win32_PrinterDriver::Create returned {0}",
outParams["ReturnValue"]);

Any help will be greatly appreciated.
Thanks


Andy Cheung [MSFT]

unread,
Jul 30, 2003, 4:05:25 PM7/30/03
to
The reason of the Not Found error is because AddPrinterDriver method takes
only one input parameter. The parameter name is DriverInfo, and it takes an
instance of Win32_PrinterDriver according to PSDK, although the method
appears to work fine if you use the Win32_PrinterDriver class to supply
input data. I prefer to spawn a new instance of Win32_PrinterDriver in this
case. e.g.

ManagementClass mc = new ManagementClass("Win32_PrinterDriver");
ManagementBaseObject inParams = mc.GetMethodParameters("AddPrinterDriver");

ManagementObject driver = new
ManagementClass("Win32_PrinterDriver").CreateInstance();
driver["Name"] = "HP LaserJet 4200 PS";
...
inParams["DriverInfo"] = driver;


ManagementBaseObject outParams = mc.InvokeMethod("AddPrinterDriver",
inParams, null);

...

--
Andy Cheung
Microsoft WMI Test Engineer
This posting is provided "As Is" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


<gv> wrote in message news:#92#aSrVDH...@TK2MSFTNGP10.phx.gbl...

gv

unread,
Jul 30, 2003, 5:50:00 PM7/30/03
to
Fantastic, much thanks. Although I'm getting a return value of 87 (and msdn
says the only return values for addprinterdriver are 0, 5, 1787), its still
a lot farther than I was earlier today.
Thanks again
gv


"Andy Cheung [MSFT]" <ha...@online.microsoft.com> wrote in message
news:OWmarXtV...@TK2MSFTNGP12.phx.gbl...

Andy Cheung [MSFT]

unread,
Jul 30, 2003, 7:29:08 PM7/30/03
to
According to Error Lookup, 87 means the parameter is incorrect. Please
verify the parameters used.

--
Andy Cheung
Microsoft WMI Test Engineer
This posting is provided "As Is" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


<gv> wrote in message news:uXQ3HSuV...@TK2MSFTNGP09.phx.gbl...

gv

unread,
Jul 31, 2003, 11:29:15 AM7/31/03
to
Inching slowly ahead. The reason for the 87, was the infName property
needed the path as well as the file name. See below. But just like the
script returned an error 5, for not having the privileges, so is the code
below, so the script needed the this line,
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege
So do I need to add the Security_ property, then add the Privileges property
to that previous property, then Invoke the AddAsString method with the value
of "SeLoadDriverPrivilege"?
If so, I can't figure out the syntax for that, and I am unable to find any
examples on MSDN, or any newsgroups.
Any thoughts?

Thus far, the following has correct parameters (and returns error 5 for not
having the correct privileges).

ManagementClass mc = new ManagementClass("Win32_PrinterDriver");

ManagementObject driver = new
ManagementClass("Win32_PrinterDriver").CreateInstance();
ManagementBaseObject inParams =
mc.GetMethodParameters("AddPrinterDriver");

driver["Name"] = "HP LaserJet 4200 PS";

driver["SupportedPlatform"] = "Windows NT x86";
driver["Version"] = "3";
driver["FilePath"] = @"C:\Driver";
driver["InfName"] = @"C:\Driver\hp4200ps.inf";
driver["Description"] = "HP LaserJet 4200 PS";

inParams["DriverInfo"] = driver;
ManagementBaseObject outParams = mc.InvokeMethod("AddPrinterDriver",
inParams, null);

uint retVal = (uint)outParams["ReturnValue"];

"Andy Cheung [MSFT]" <ha...@online.microsoft.com> wrote in message

news:OwgUhJvV...@tk2msftngp13.phx.gbl...

gv

unread,
Jul 31, 2003, 12:48:43 PM7/31/03
to
Apparently setting privileges is different in code than in script. I had to
set up a ConnectionOptions object, set its EnablePrivileges property, and
tie a ManagementScope object with a ManagementClass object. The following
worked. Hopefully I can save someone else from spinning their wheels for
some time.
Thanks Andy for your time.

ConnectionOptions options = new ConnectionOptions();
options.EnablePrivileges = true;
ManagementScope ms = new ManagementScope("\\root\\CIMV2", options);
ms.Connect();

ManagementClass mc = new ManagementClass("Win32_PrinterDriver");

mc.Scope = ms;


ManagementObject driver = new
ManagementClass("Win32_PrinterDriver").CreateInstance();
ManagementBaseObject inParams =
mc.GetMethodParameters("AddPrinterDriver");

driver["Name"] = "HP LaserJet 4200 PS";
driver["SupportedPlatform"] = "Windows NT x86";
driver["Version"] = "3";
driver["FilePath"] = @"C:\Driver";
driver["InfName"] = @"C:\Driver\hp4200ps.inf";
driver["Description"] = "HP LaserJet 4200 PS";

inParams["DriverInfo"] = driver;

ManagementBaseObject outParams = mc.InvokeMethod("AddPrinterDriver",
inParams, null);

uint retVal = (uint)outParams["ReturnValue"];

<gv> wrote in message news:ePVYBi3V...@tk2msftngp13.phx.gbl...

0 new messages