Enumerating Printer Drivers Help Please

B

Brett Mostert

Hi,

I need to be able to Enumerate through Printer Drivers, ports, printers
and so fourth. And even add printers, ports, and drivers and setup printers.

Sofar i can do all of the following except anything to do with drivers.

I have added a reference to the PRNADMIN.DLL from MS Windows 2003 Tool
Kit into my project to do all of the above, but when i try to enumerate
printers i received the following error....

The data area passed to a system call is too small.
System.Runtime.InteropServices.ComException (0x8007007A)

Any help please? My code is below. (Basically the same for enumerating
ports - at least that works, even the VB6.0 code given my MS in the
examples throughs the same error)

using PRNADMINLib;

public static bool AddPrinterDriver(string strServerName)
{
try
{
strServerName = "";
PrintMasterClass oPrinterMaster = new PrintMasterClass();
object oRefDriver = strServerName;
object oDrivers;
oDrivers = oPrinterMaster.get_Drivers(ref oRefDriver);
DriverCollection oDriverCollection = (DriverCollection)oDrivers;

foreach (Driver oDriver in oDriverCollection)
{
System.Windows.Forms.MessageBox.Show(oDriver.MonitorName);
System.Windows.Forms.MessageBox.Show(oDriver.ConfigFile);
System.Windows.Forms.MessageBox.Show(oDriver.DataFile);
System.Windows.Forms.MessageBox.Show(oDriver.DriverVersion);
System.Windows.Forms.MessageBox.Show(oDriver.DriverArchitecture);
System.Windows.Forms.MessageBox.Show(oDriver.Environment);
System.Windows.Forms.MessageBox.Show(oDriver.HelpFile);
System.Windows.Forms.MessageBox.Show(oDriver.InfFile);
System.Windows.Forms.MessageBox.Show(oDriver.ModelName);
System.Windows.Forms.MessageBox.Show(oDriver.Path);
System.Windows.Forms.MessageBox.Show(oDriver.ServerName);
System.Windows.Forms.MessageBox.Show(oDriver.Version.ToString());
}
return true;
}
catch (Exception err)
{
System.Windows.Forms.MessageBox.Show(err.Message.ToString() + " | " +
err.ToString() + "|" + err.Source.ToString());
return false;
}
}
 
W

Willy Denoyette [MVP]

Brett Mostert said:
Hi,

I need to be able to Enumerate through Printer Drivers, ports, printers
and so fourth. And even add printers, ports, and drivers and setup
printers.

Sofar i can do all of the following except anything to do with drivers.

I have added a reference to the PRNADMIN.DLL from MS Windows 2003 Tool Kit
into my project to do all of the above, but when i try to enumerate
printers i received the following error....

The data area passed to a system call is too small.
System.Runtime.InteropServices.ComException (0x8007007A)

Any help please? My code is below. (Basically the same for enumerating
ports - at least that works, even the VB6.0 code given my MS in the
examples throughs the same error)

using PRNADMINLib;

public static bool AddPrinterDriver(string strServerName)
{
try
{
strServerName = "";
PrintMasterClass oPrinterMaster = new PrintMasterClass();
object oRefDriver = strServerName;
object oDrivers;
oDrivers = oPrinterMaster.get_Drivers(ref oRefDriver);
DriverCollection oDriverCollection = (DriverCollection)oDrivers;

foreach (Driver oDriver in oDriverCollection)
{
System.Windows.Forms.MessageBox.Show(oDriver.MonitorName);
System.Windows.Forms.MessageBox.Show(oDriver.ConfigFile);
System.Windows.Forms.MessageBox.Show(oDriver.DataFile);
System.Windows.Forms.MessageBox.Show(oDriver.DriverVersion);
System.Windows.Forms.MessageBox.Show(oDriver.DriverArchitecture);
System.Windows.Forms.MessageBox.Show(oDriver.Environment);
System.Windows.Forms.MessageBox.Show(oDriver.HelpFile);
System.Windows.Forms.MessageBox.Show(oDriver.InfFile);
System.Windows.Forms.MessageBox.Show(oDriver.ModelName);
System.Windows.Forms.MessageBox.Show(oDriver.Path);
System.Windows.Forms.MessageBox.Show(oDriver.ServerName);
System.Windows.Forms.MessageBox.Show(oDriver.Version.ToString());
}
return true;
}
catch (Exception err)
{
System.Windows.Forms.MessageBox.Show(err.Message.ToString() + " | " +
err.ToString() + "|" + err.Source.ToString());
return false;
}
}

IMO this COM server (prnadmin.dll) has not been designed(tested) to be used
from non-scripting languages (VBS or JS), everything else like VB.NET, C#
and C++ fails to set/get some properties.
Unless you need this to run on NT4, I would suggest you to use
System.Management and WMI for this.

Willy.
 
B

Brett Mostert

Willy said:
IMO this COM server (prnadmin.dll) has not been designed(tested) to be used
from non-scripting languages (VBS or JS), everything else like VB.NET, C#
and C++ fails to set/get some properties.
Unless you need this to run on NT4, I would suggest you to use
System.Management and WMI for this.

Willy.
Hi, first of all thanx for the reply :), but i have tried using VBS
already, and it throws the same error, even the sample microsoft
provides in there .vbs file throws the same error?

Any thoughts?

Thanx
Brett
 
W

Willy Denoyette [MVP]

Brett Mostert said:
Hi, first of all thanx for the reply :), but i have tried using VBS
already, and it throws the same error, even the sample microsoft provides
in there .vbs file throws the same error?

Any thoughts?

Thanx
Brett

This works for me on both W2K3 and XP, using prnadmin.dll version 5.2.3790.0
..

Dim oMaster
set oMaster = CreateObject("PrintMaster.PrintMaster.1")
for each oDriver in oMaster.Drivers("")
wscript.echo "DriverName : " & oDriver.ModelName
next

Using any non-scripting language (C++, C#, VB.NET) to enum the drivers fail
with HRESULT 0x8007007A, other interfaces like Printer, Port and Form work
as expected.

Willy.
 
B

Brett Mostert

Willy said:
This works for me on both W2K3 and XP, using prnadmin.dll version 5.2.3790.0
.

Dim oMaster
set oMaster = CreateObject("PrintMaster.PrintMaster.1")
for each oDriver in oMaster.Drivers("")
wscript.echo "DriverName : " & oDriver.ModelName
next

Using any non-scripting language (C++, C#, VB.NET) to enum the drivers fail
with HRESULT 0x8007007A, other interfaces like Printer, Port and Form work
as expected.

Willy.
Hey, hmm yes your are right, is there away i could do this using C#?
I need to be able to add printer ports, drivers, printers and administer
them?

thanx
Brett
 
Top