Check printer if connected to pc or not - c#

I need to write some code that would prompt a message "printer is connected" when the printer is plugged into the computer and also prompt a message "printer not connected" when I plug out the printer from computer. I also want to list the printers available through a combobox. How can I do this in C# using Visual Studio?

You should use Winspool.lib
C# Signature :
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
EDIT:
You can also use this
foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
//Add in combo box
}

to immediately get the pop-up that new Printer Found/Disconnected... you must have to run some code in background continuously Windows Service is the best for that.. and using the below code you can get the installed printer so first store the currently installed printer in list and after each 10(or any you want) second get the installed printer again if difference found propmt the message accordingly..
this is the snippet to get the installed printer..
private string[] GetAvailablePrinters()
{
var installedPrinters = new string[PrinterSettings.InstalledPrinters.Count];
PrinterSettings.InstalledPrinters.CopyTo(installedPrinters, 0);
var printers = new List<string>();
var printServers = new List<string>();
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
foreach (var printer in searcher.Get())
{
var serverName = #"\\" + printer["SystemName"].ToString().TrimStart('\\');
if (!printServers.Contains(serverName))
printServers.Add(serverName);
}
foreach (var printServer in printServers)
{
var server = new PrintServer(printServer);
try
{
var queues = server.GetPrintQueues();
printers.AddRange(queues.Select(q => q.Name));
}
catch (Exception)
{
// Handle exception correctly
}
}
return printers.ToArray();
}
You might need to add the System.Management, System.Drawing, System.Printing references in you project..

Related

How to set up a virtual printer and monitor with UniDrv or XPS driver v3 in Windows

I am trying to create a virtual printer to capture print outs from applications in my own application.
I have successfully implemented one that uses the Microsoft PostScript driver and produces ps files. (A lot of code extracted from different open source projects)
However, due to licensing issues with GhostScript on production servers, (it is NOT free for business solutions), I want to implement a different driver that produces XPS files or any other format that I can use to extract text, convert to a PDF, extract images of each page, etc.
The code that I am using with the postscript driver and actually works quite well is as follows:
// Declare the AddPrinterDriver as extern.
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool AddPrinterDriver(String pName, UInt32 Level, ref DRIVER_INFO_3 pDriverInfo);
// Create a function to call it.
public void AddPrinterDriver(string driverName, string driverPath, string dataPath, string configPath, string helpPath)
{
DRIVER_INFO_3 di = new DRIVER_INFO_3();
di.cVersion = 3;
di.pName = driverName;
di.pEnvironment = null;
di.pDriverPath = driverPath;
di.pDataFile = dataPath;
di.pConfigFile = configPath;
di.pHelpFile = helpPath;
di.pDependentFiles = "";
di.pMonitorName = null;
di.pDefaultDataType = "RAW";
if (!AddPrinterDriver(null, 3, ref di))
{
Exception exc = new Win32Exception(Marshal.GetLastWin32Error());
throw exc;
}
}
Install Printer method (without proper validation and logging):
public void InstallVirtualPrinter()
{
// Declare file names for PostScript printer driver. (Preinstalled in Vista and Up)
string driverFileName = "PSCRIPT5.DLL";
string configFileName = "PS5UI.DLL";
string helpFileName = "PSCRIPT.HLP";
string dataFileName = "MyCustomConfig.PPD";
string driverPath = null;
string dataPath = null;
string configPath = null;
string helpPath = null;
try
{
//Set Printer Driver Path and Files.
string printerDriverPath = Path.Combine(GetPrinterDirectory(), "3");
// Set the path for the driver files.
if (!String.IsNullOrWhiteSpace(printerDriverPath))
{
driverPath = string.Format("{0}\\{1}", printerDriverPath, driverFileName);
dataPath = string.Format("{0}\\{1}", printerDriverPath, dataFileName);
configPath = string.Format("{0}\\{1}", printerDriverPath, configFileName);
helpPath = string.Format("{0}\\{1}", printerDriverPath, helpFileName);
}
// Add Printer Monitor
if (!DoesMonitorExist(PrinterMonitorName))
{
AddPrinterMonitor(PrinterMonitorName);
}
// Add Printer Port
if (!DoesPrinterPortExist(PrinterPortName))
{
AddPrinterPort(PrinterPortName, PrinterMonitorName);
}
// Add Printer Driver
if (!DoesPrinterDriverExist(PrinterDriverName))
{
//
//
//
//
// This references the above method in this SO question.
AddPrinterDriver(PrinterDriverName, driverPath, dataPath, configPath, helpPath);
//
// This fails when trying with a driver different than PScript.
//
}
// Add Printer
if (!DoesPrinterExist(PrinterName))
{
InstallPrinter(PrinterName, PrinterPortName, PrinterDriverName);
}
// Configure Virtual Port
ConfigureVirtualPort(PrinterMonitorName, PrinterPortName);
// Restart Spool Service
RestartSpoolService();
log.Info("Virtual printer installation completed successfully");
return;
}
catch (Exception exc)
{
log.ErrorFormat("An exception has been raise when attempting to install the printer \n{0}", exc);
}
}
So here is the question:
How can I use a different driver, like UniDrv or XPS to implement the virtual printer/monitor?.
I have tried with UniDrv by replacing the following lines in the code above:
string driverFileName = "unidrv.dll";
string dataFileName = "sample.GPD";
string configFileName = "unidrvui.dll";
string helpFileName = "unidrv.hlp";
When I run the method AddPrinterDriver I get an exception indicating "The system cannot find the file specified".
It doesn't say what file is missing. I assume there may be missing some dependencies, or the sample.GPD file that I found is not good.
Any help will be greatly appreciated.
Before executing AddPrinterDriver, install all files (unidrv.dll, unidrvui.dll, sample.gpd) in the driver path ie., spool/drivers/x86Orx64 path.

print device configuration page programmatically under windows

I'm trying to get the total pages count from all the printers connected to a computer. To do that i try in different ways but nothing give me the expected result:
SNMP: Very useful but impossible use it on a local printer.
Win32_printer: No way to get the total page count of a network printer and more of the local printers not store the page count on it.
printui.dll: total page count not supported.
Now i'm trying a different approach: there is a way to programmatically print the device configuration page (Example: THAT) to a file? you can print it whitout connect the printer to a PC's so it shoud be stored somewhere in some digital format. the only thing i found on internet is the Bidi Communication but i can't found any documentation that can help me... I not need a particular programming language, but if possible i prefere c#,c++ or java.
#Soner Gönül: Ok, so i choose c# just because is the language that i use to write the snmp test, but examples in other languages are appreciated:
SNMP:
SNMP snmp = new SNMP();
// Aprire la connessione verso la stampante
try
{
snmp.Open(IPAddressOfPrinter, CommunityString, Retries, TimeoutInMS);
} catch {
MessageBox.Show("Unable to reach the printer");
}
//Sides Counter
lblSides.Text = snmp.Get(".1.3.6.1.2.1.43.10.2.1.4.1.1").ToString();
rtbLog.Text += "Printer Counter: " + lblSides.Text;
rtbLog.Text += " Sides.\n";
That code give me the number of sides printed by the printer, that is ok, but need that the printer is a network printer, on a USB printer i can't use SNMP.
Win32_Printer:
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope(#"\\" + System.Environment.MachineName.ToString() + #"\root\cimv2"); //yeah... i know #"root\cimv2" is enought, i notice only now...
scope.Connect();
ObjectQuery PRNquery = new ObjectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher PRNsearcher = new ManagementObjectSearcher(scope, PRNquery);
ManagementObjectCollection PRNqueryCollection = PRNsearcher.Get();
foreach (ManagementObject m in PRNqueryCollection)
{
MessageBox.Show(m["JobCountSinceLastReset"].Tostring());
//But the result is Zero or random error the 90% of the times.
}
priuntui.dll:
If i launch
rundll32 printui.dll PrintUIEntry /something
or try to export the report with:
public static class PrintTest
{
[DllImport("printui.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern void PrintUIEntryW(IntPtr hwnd,
IntPtr hinst, string lpszCmdLine, int nCmdShow);
public static void Print(string printerName)
{
var printParams = string.Format(#"/f C:\test.dat /Xg /n{0}", printerName);
PrintUIEntryW(IntPtr.Zero, IntPtr.Zero, printParams, 0);
}
}
i not found a page count report, but only generic information.

Can I get the icon associated with an installed printer through .Net?

I know how to get the list of all of the installed printers on a machine with .Net:
foreach (String printer in PrinterSettings.InstalledPrinters)
{
Console.WriteLine(printer.ToString());
}
Console.ReadLine();
InstalledPrinters is just a list of strings though. Is there any way to get the installed printer objects that contain both the name and the icon image that I would ordinarily see under "Devices and Printers" in the Windows Explorer?
The icon is normally embedded into either one of the dll files or the main EXE, look at the System.Drawing.Icon static methods, the link below is for WinForms, its slightly different with WPF as you have to create an ImageSource from the extracted icon stream.
How to: Extract the Icon Associated with a File in Windows Forms
C# code for this task:
public static class PrinterIcons
{
public static Dictionary<string, Icon> GetPrintersWithIcons(IntPtr hwndOwner)
{
Dictionary<string, Icon> result = new Dictionary<string, Icon>();
Shell32.IShellFolder iDesktopFolder = Shell32.GetDesktopFolder();
try
{
IntPtr pidlPrintersFolder;
if (Shell32.SHGetFolderLocation(hwndOwner, (int)Shell32.CSIDL.CSIDL_PRINTERS, IntPtr.Zero, 0, out pidlPrintersFolder) == 0)
try
{
StringBuilder strDisplay = new StringBuilder(260);
Guid guidIShellFolder = Shell32.IID_IShellFolder;
IntPtr ptrPrintersShellFolder;
iDesktopFolder.BindToObject(pidlPrintersFolder, IntPtr.Zero, ref guidIShellFolder, out ptrPrintersShellFolder);
Object objPrintersShellFolder = Marshal.GetTypedObjectForIUnknown(ptrPrintersShellFolder, Shell32.ShellFolderType);
try
{
Shell32.IShellFolder printersShellFolder = (Shell32.IShellFolder)objPrintersShellFolder;
IntPtr ptrObjectsList;
printersShellFolder.EnumObjects(hwndOwner, Shell32.ESHCONTF.SHCONTF_NONFOLDERS, out ptrObjectsList);
Object objEnumIDList = Marshal.GetTypedObjectForIUnknown(ptrObjectsList, Shell32.EnumIDListType);
try
{
Shell32.IEnumIDList iEnumIDList = (Shell32.IEnumIDList)objEnumIDList;
IntPtr[] rgelt = new IntPtr[1];
IntPtr pidlPrinter;
int pceltFetched;
Shell32.STRRET ptrString;
while (iEnumIDList.Next(1, rgelt, out pceltFetched) == 0 && pceltFetched == 1)
{
printersShellFolder.GetDisplayNameOf(rgelt[0],
Shell32.ESHGDN.SHGDN_NORMAL, out ptrString);
if (Shell32.StrRetToBuf(ref ptrString, rgelt[0], strDisplay,
(uint)strDisplay.Capacity) == 0)
{
pidlPrinter = Shell32.ILCombine(pidlPrintersFolder, rgelt[0]);
string printerDisplayNameInPrintersFolder = strDisplay.ToString();
Shell32.SHFILEINFO shinfo = new Shell32.SHFILEINFO();
Shell32.SHGetFileInfo(pidlPrinter, 0, out shinfo, (uint)Marshal.SizeOf(shinfo), Shell32.SHGFI.PIDL | Shell32.SHGFI.AddOverlays | Shell32.SHGFI.Icon);
Icon printerIcon = (Icon)Icon.FromHandle(shinfo.hIcon).Clone();
Shell32.DestroyIcon(shinfo.hIcon);
result.Add(printerDisplayNameInPrintersFolder, printerIcon);
}
}
}
finally
{
Marshal.ReleaseComObject(objEnumIDList);
}
}
finally
{
Marshal.ReleaseComObject(objPrintersShellFolder);
}
}
finally
{
Shell32.ILFree(pidlPrintersFolder);
}
}
finally
{
Marshal.ReleaseComObject(iDesktopFolder);
}
return result;
}
}
Beware, that printer names in result dictionary will be printer names shown in Printers shell folder, and they can be different from printer names, used in PrinterSettings class (for example, network printers in Printers shell folder can be shown as " on ", and word "on" depends from windows localization and can be not machine network name). I don`t know yet how to get "real" printer name from IShellFolder to use it with standart PrinterSettings class.
Anyway, this code loads printers system icons, so you can use it for you task.
Upd: Shell32 class code, used in this code can be found here (too big for answer): http://pastebin.com/thJuWx45

How to enumerate audio out devices in c#

I would like to know how to get a list of the installed audio out devices (waveOut) on a machine
OS: Windows (XP, Vista, 7)
Framework: .Net 3.5
Language: c#
When iterating through this list I would like to get information like Identifier, Manufacturer, ... per device.
Any hints?
Here is code to enumerate audio devices in C#, using WMI (reference System.Management).
ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
"SELECT * FROM Win32_SoundDevice");
ManagementObjectCollection objCollection = objSearcher.Get();
foreach (ManagementObject obj in objCollection)
{
foreach (PropertyData property in obj.Properties)
{
Console.Out.WriteLine(String.Format("{0}:{1}", property.Name, property.Value));
}
}
Which results in output something like:
Availability:
Caption:USB Audio Device
ConfigManagerErrorCode:0
ConfigManagerUserConfig:False
CreationClassName:Win32_SoundDevice
Description:USB Audio Device
DeviceID:USB\VID_047F&PID_0CA1&MI_00\6&2C037688&0&0000
DMABufferSize:
ErrorCleared:
ErrorDescription:
InstallDate:
LastErrorCode:
Manufacturer:(Generic USB Audio)
MPU401Address:
Name:USB Audio Device
PNPDeviceID:USB\VID_047F&PID_0CA1&MI_00\6&2C037688&0&0000
PowerManagementCapabilities:
PowerManagementSupported:False
ProductName:USB Audio Device
Status:OK
StatusInfo:3
SystemCreationClassName:Win32_ComputerSystem
SystemName:
Availability:
Caption:Realtek AC'97 Audio for VIA (R) Audio Controller
ConfigManagerErrorCode:0
ConfigManagerUserConfig:False
CreationClassName:Win32_SoundDevice
Description:Realtek AC'97 Audio for VIA (R) Audio Controller
DeviceID:PCI\VEN_1106&DEV_3059&SUBSYS_09011558&REV_60\3&61AAA01&1&8D
DMABufferSize:
ErrorCleared:
ErrorDescription:
InstallDate:
LastErrorCode:
Manufacturer:Realtek
MPU401Address:
Name:Realtek AC'97 Audio for VIA (R) Audio Controller
PNPDeviceID:PCI\VEN_1106&DEV_3059&SUBSYS_09011558&REV_60\3&61AAA01&1&8D
PowerManagementCapabilities:
PowerManagementSupported:False
ProductName:Realtek AC'97 Audio for VIA (R) Audio Controller
Status:OK
StatusInfo:3
SystemCreationClassName:Win32_ComputerSystem
SystemName:
Availability:
WMI annoyingly does not appear to distinguish simply between input and output devices for audio. However, using the managed interface to DirectSound, and the DevicesCollection class, as below (reference Microsoft.DirectX.DirectSound), we can get a lot more sound-oriented information.
DevicesCollection devColl = new DevicesCollection();
foreach (DeviceInformation devInfo in devColl)
{
Device dev = new Device(devInfo.DriverGuid);
//use dev.Caps, devInfo to access a fair bit of info about the sound device
}
In Windows Vista and above you can use IMMDeviceEnumerator which is wrapped for you by NAudio in order to enumerate audio endpoint devices. For example:
var enumerator = new MMDeviceEnumerator();
foreach (var endpoint in
enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active))
{
Console.WriteLine(endpoint.FriendlyName);
}
here is an example
Add a reference to System.Management
ManagementObjectSearcher mo = new ManagementObjectSearcher("select * from Win32_SoundDevice");
foreach (ManagementObject soundDevice in mo.Get())
{
Console.WriteLine(soundDevice.GetPropertyValue("DeviceId"));
Console.WriteLine(soundDevice.GetPropertyValue("Manufacturer"));
// etc
}
/// <summary>
/// The DirectSoundEnumerate function enumerates the DirectSound Odrivers installed in the system.
/// </summary>
/// <param name="lpDSEnumCallback">callback function</param>
/// <param name="lpContext">User context</param>
[DllImport("dsound.dll", EntryPoint = "DirectSoundEnumerateA", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
static extern void DirectSoundEnumerate(DevicesEnumCallback lpDSEnumCallback, IntPtr lpContext);
And the callback should by like this:
private static bool DevicesEnumCallbackHandler(IntPtr lpGuid, IntPtr lpcstrDescription, IntPtr lpcstrModule, IntPtr lpContext)
Check waveOutGetNumDevs API
[DllImport("winmm.dll", SetLastError = true)]
public static extern uint waveOutGetNumDevs();
Returns the number of devices. A return value of zero means that no devices are present or that an error occurred.
http://msdn.microsoft.com/en-us/library/dd743860(v=vs.85).aspx

Get Firefox URL?

How can I get the url from a running instance of firefox using .NET 2.0 windows/console app? C# or VB codes will do.
Thanks!
Building on Rob Kennedy's answer and using NDde
using NDde.Client;
class Test
{
public static string GetFirefoxURL()
{
DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
dde.Connect();
string url = dde.Request("URL", int.MaxValue);
dde.Disconnect();
return url;
}
}
NB: This is very slow. It takes a few seconds on my computer. The result will look something like this :
"http://stackoverflow.com/questions/430614/get-firefox-url","Get Firefox URL? - Stack Overflow",""
More info on browser DDE here.
For most browsers, including Internet Explorer, Navigator, Firefox, and Opera, the supported and sanctioned way of doing this is to use DDE. The topic name in all of them is WWW_GetWindowInfo; only the name of the target window varies. That technique will be difficult for you, though, because .Net doesn't support DDE. If you can find a way to get around that limitation, you'll be all set.
it seems that this might be difficult, here's some discussion on it: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/c60b1699-9fd7-408d-a395-110c1cd4f297/
You may want to check into the source code of WatiN. Their next version is open source and supports firefox, so I would imagine the functionality for doing this is in it.
Use MozRepl: https://github.com/bard/mozrepl/wiki/ + mozRepl .NET Connector: http://mozreplconnector.codeplex.com/releases/view/17398
var connect = new MozReplConnectDotNet.MozReplConnect(4242);
connect.Connect();
Console.WriteLine(connect.SendRecieve("gBrowser.currentURI.spec"));
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr parentHandle,
IntPtr childAfter, string className, IntPtr windowTitle);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd,
int msg, int wParam, StringBuilder ClassName);
private static string GetURL(IntPtr intPtr, string programName, out string url)
{
string temp=null;
if (programName.Equals("chrome"))
{
var hAddressBox = FindWindowEx(intPtr, IntPtr.Zero, "Chrome_OmniboxView", IntPtr.Zero);
var sb = new StringBuilder(256);
SendMessage(hAddressBox, 0x000D, (IntPtr)256, sb);
temp = sb.ToString();
}
if (programName.Equals("iexplore"))
{
foreach (InternetExplorer ie in new ShellWindows())
{
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(ie.FullName);
if (fileNameWithoutExtension != null)
{
var filename = fileNameWithoutExtension.ToLower();
if (filename.Equals("iexplore"))
{
temp+=ie.LocationURL + " ";
}
}
}
}
if (programName.Equals("firefox"))
{
DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
dde.Connect();
string url1 = dde.Request("URL", int.MaxValue);
dde.Disconnect();
temp = url1.Replace("\"","").Replace("\0","");
}
url = temp;
return temp;
}
Please do following to run this code
Add Reference > Com > Microsoft.Internet.Controls from VS.NET in your project
Download the bin from http://ndde.codeplex.com/ for DdeClient class and add it to your project
Please Let me know if any issue
Poor man's solution, if anything else fails: activate the Firefox window, send Ctrl+L (activates address bar), send Ctrl+C (copy selection, ie. URL, to clipboard) and read the clipboard.
Lot of issues with this method (among them it does strange stuff for the user if they are in front of the computer) so it is only a backup solution...
try this one:
//get all running process of firefox
Process[] procsfirefox = Process.GetProcessesByName("firefox");
foreach (Process firefox in procsfirefox)
{
//the firefox process must have a window
if (firefox.MainWindowHandle == IntPtr.Zero)
{
continue;
}
AutomationElement sourceElement = AutomationElement.FromHandle(firefox.MainWindowHandle);
AutomationElement editBox = sourceElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Search with Google or enter address"));
// if it can be found, get the value from the editbox
if (editBox != null)
{
ValuePattern val = ((ValuePattern)editBox.GetCurrentPattern(ValuePattern.Pattern));
Console.WriteLine("\n Firefox URL found: " + val.Current.Value);
}
//-----------------------------find titlebar element for site title---------------------------------//
AutomationElement elmTitleBar = sourceElement.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TitleBar));
if (elmTitleBar != null)
Console.WriteLine("\n Firefox TitleBar found: " + elmTitleBar.Current.Name);
}
full source code:https://github.com/Moeedahmad899/GetFirefoxURL

Categories