get list of printer ports - c#

I'm looking for a way to list all printer ports on the current PC. I want to list them and put them into a combo box. I was going to create a list and pull that in every time the cbo must load, however I then thought about if people add their own ports and some printers install themselves to new ports. Because of this just stating a generic list of ports might not be the correct way.
I am using DOS print process to the printer port directly and in the settings need the user to select which port to print to.
I'm sure someone has a quick way, or a way they can point me to. I saw a couple ways online however It completely boggled my mind and I don't like using code I can't understand, and don't have the time to understand it right now unfortunately. I am sure there must be a class of somesort in C# already created to do this. Most of the other ways I saw to me appeared to be importing several dlls and or running through a few registeries.
And assistance is greatly appreciated
UPDATE just to be clear I want a way that will simply return to me LPT1 LPT2 COM1....

Related

How do I cancel recent prints with printDocument.print() in C#?

I have a C# application that prints simple texts correctly with printDocument Class.
The problem is that I want to cancel recent print jobs or Remove them or Purge print queue, before call a new .print() function!
I don't know what I have to do that, it seems PrintDocument doesn't push print jobs onto printer queue! because I dont see any job there.
thanks for help to "clear recent print jobs created with printDocumnet.print()"
Ali.
You should be able to with WMI as long as you have the rights
http://sandit27.wordpress.com/2008/05/12/how-to-cancel-printing-in-c/
I did a lot of work with printers a while back and the way that I achieved most of the functionality I wanted was through PInvoke.
As the answer from #Tsukasa states you "can" use WMI but that's another nightmare that's very difficult to deal with if you plan on actually deploying your solution anywhere other than your own machine.
As I'd mentioned: my suggestion would be to look into PInvoke. Keep in mind though that it has been a number of years/.NET Framework versions since I've worked in the print industry so Microsoft may have released a more elegant, easy-to-use solution that is native to .NET.
Here is a list of Print Spooler functions from the MSDN
Basically what you're going to want to do if you go this route is this:
Find the named handle to the print device in the Registry. This can be done with the registry classes available in the .NET framework. The name of the printer to windows will start with a "#" (or two) followed by some garbled junk and then a GUID. The key where all your printers are installed in the registry is here:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers
Call OpenPrinter with the printer name.
Call EnumJobs with the handle retrieved from OpenPrinter.
Call SetJob passing the Job Id retrieved from EnumJobs for the job you want to cancel. Be sure to pass the JOB_CONTROL_DELETE Flag to cancel the job.
Call ClosePrinter, passing the handle retrieved from OpenPrinter. Don't forget this step. Windows/The print spooler gets very cranky if you do.
Again I can't stress enough that there is most-likely a better, more .NET way to do this. I suggest exhausting your options there first.
Good luck.

Getting unique hardware ID changes everytime C#

I want to make a program and sell it, so I was thinking to make some kind of licensing system. What I've found on google is this:
http://www.codeproject.com/Articles/28678/Generating-Unique-Key-Finger-Print-for-a-Computer
I am running Windows 7 (32 bit) in VirtualBox from Ubuntu. I get the same UID, but when I restart my Windows, I get a new UID. I am just thinking that some people will buy my program and they may run it from VirtualBox, so it would be a problem.
Do you guys have any suggestions?
Even if it's an easier way, but something that won't change even if I restart my VBox.
You need to not get so bogged down in the whole DRM process, there are always smarter people out there and someone will work out how to circumvent it.
If you're really concerned:
Set up an online license server to hold your users licenses
Prevent the application from doing anything until the user enters a valid license
This licence would be validated over the internet by your service
You could restrict the number of "active" applications using your service that way
Think along the lines of how iTunes allows you to authorise up to 5 computers to access your media library.
Even if a lot of time has passed, I want to add a possible solution to this question, pointing out something that happened to me trying to generate a fingerprint with exactly that code and facing the same problem.
The identifier function retrieves only the first not-empty value of wmiProperty for each wmiClass. The first important thing you should be aware of is this: what is returned for every wmiClass might not belong to the same ManagementObject. Hence, if something changes to "not the first object", this might affect also your fingerprint.
Be advised that using Win32_DiskDrive as wmiClass is dangerous since plugging in a USB device often results in this device being recognized as the first one (instead of your hard drive), changing the fingerprint as you plug-in or out your device. Even if this doesn't happen, still one of the wmiProperty could be taken from your USB device if your main drive has it empty (e.g. the disk signature, which happened to me).
A possible solution to that would be excluding all USB devices by checking InterfaceType and excluding all those who match "USB", or at least that's what I did.

Server side printing of PDF files over the network installed Printers

Gone through the already existing posts on this topic, but my requirement is some what bit different.
Print jobs over the Network installed printers, upon a action in client side browser. Web server has got all printers in it, based on the client data, we need to print with client given configuration straight away, sending the job to printer(may be a different locations - but this is not a problem since the printers are in same network). I have seen printing architecture, Win32_print Class, SNMP, System.drawing.Printing, System.Printing.... but couldn't find the way to achieve this.. (Asp.net,C#)
-Files are been already existed in a server folder, just I need to pick up and send to a printer with client Selected Configuration...
update:I have to use JDF approach because all the files are PDF files. I am able to create JDF files with the selected configuration but sending this JDF to printer(JDF enabled) making me tough... There is JMF which post the JDF to printer, but not sure how to implement it.. still finding the ways, will be great if some one helps in this.
Something I can answer! 8-) I'm actually working on a project that uses this, right now.
Do not use Acrobat Reader as any sort of server application. It has a User Interface that will pop up on a screen that doesn't exist, at an unexpected time and freeze your app, waiting for a click that will never happen.
Use Ghostscript. Ghostscript is free, Open Source and will work perfectly for what you want. Out-of-the-box it consists of a DLL and a command line inhterface, however there is an API and a ton of different interfaces including .Net, and versions for both Windows and Linux.
On a more "overview" type of note, the reason you can drag a PDF into a printer on your workstation is because Acrobat Reader contains the code and hooks to allow this. Windows doesn't actually contain native functionality to print PDFs.

c# serial port redirection

Is there a way to, in c# write a serial port redirector? I have an app that is coded to use port 1 (com1:) however, my port on the handheld (Intermec CN50) is always 3. I do not have access to modify the mobile software, however I would like to redirect any data going out to 1 to copy it to 3 and any data coming in to copy it from 3 to 1, so the app doesnt know the difference.
I know this is a difficult issue, but I am sure one of you guys out there have an answer.
I sure dont.
Thanks
Chris
No, there isn't. You have a couple alternatives, depending on the device and your exact needs.
The first, and simplest, is to redirect the driver. If your device doesn't already have a COM1 device defined, you can modify the registry in HKLM\drivers\builtin and change the Index for your specific port from 3 to 1. That will cause device.exe to expose the port as COM1 and not COM3. In my experience this isn't likely to work, though, as most serial ports are set to let the OS auto-assign the index, meaning COM1 and COM2 are already in use. You can, however, still work around this. You simply have to explicitly assign an Index to every instance at or below 3 for the device. Move index 1 to 3 and index 3 to 1 and you effectively swap the ports.
Here are some examples - there are a few because it depends on how the OEM set up driver initialization. Here's an example of a driver with no explicit port named (in this case I2C, but it works the same way):
Note there is a Prefix and an Index. This driver is going to enumerate as "I2C0:". If you changes the Index value to something else, say '2', then it would enumerate as "I2C2:".
In some cases the driver can provide a specific port name, like this one:
Note here that the Port value matches Prefix plus Index. No idea what would happen if you manually set them different, so don't do that. If you wanted to move this one to COM3, you'd change Index to 3 and Port to COM3, but you have to make sure there's nothing else in the registry already set to use those values. You don't want two driver instances trying to enumerate at the same Index. If you do, I think the behavior is that the second one just gets incremented, but it's best to explicitly set the values as unique so you know what's happening.
If that doesn't meet your needs, you'd have to write an actual device driver to do the port move/aggregation. It would work a lot like the existing GPS Intermediate Driver (GPSID) under Windows Mobile and would allow you to redirect the serial data to any port you want. This, however, must be written in C because managed code (in the CF) cannot export native symbols so there's no way to get device.exe to load a C# assembly.

Windows clipboard CRLF/LF passing wrong for one user

We have a pair of applications. One is written in C# and uses something like:
string s = "alpha\r\nbeta\r\ngamma\r\ndelta";
// Actually there's wrapper code here to make sure this works.
System.Windows.Forms.Clipboard.SetDataObject(s, true);
To put a list of items onto the clipboard. Another application (in WinBatch) then picks up the list using a ClipGet() function. (We use the clipboard functions to give people the option of editing the list in notepad or something, without having to actually cut-and-paste every time.)
In this particular environment, we have many users on one system via Citrix. Many using these pairs of programs.
Just one user is having the problem where the line delimiters in the text are getting switched from CRLF to LF somewhere between the SetDataObject() and the CLipGet(). I could explain this in a mixed Unix/Windows environment, but there is no Unix here. No unix-y utilities anywhere near this system either. Other users on the same server, no problems at all. It's like something in Windows/Citrix is being "helpful" when we really don't want it, but just for this one guy.
Ideas?
Have you tried clearing their profile on Citrix? That seems to be the solution to many many user specific Citrix problems.
Does Environment.NewLine behave differently on Citrix environments? If so, it may give you a good option that works for all users instead of \r\n.

Categories

Resources