.NET Framework Bookmark and Share   
 index > .NET Base Class Library > CreateFile() + USB
 

CreateFile() + USB

Hi fellas, I'm using the CreateFile Win32 API tosend commandsto my Zebra printer:

Code Snippet

[DllImport("kernel32.dll", SetLastError = true)]

static extern SafeFileHandle CreateFile(string lpFileName, FileAccess dwDesiredAccess,

uint dwShareMode, IntPtr lpSecurityAttributes, FileMode dwCreationDisposition,

uint dwFlagsAndAttributes, IntPtr hTemplateFile);

for lpFileName I use "LPT1:" and everything runs fine. Now I need to do the same thing but for the USB port, but I've been looking around and didn't find a solution. How do I get the link to the USB port? Is there an API that returns the name of the port so I can use it in the lpFileName string?

Thanks for any help,

Regards,

Fábio

Fábio Franco

Hi Fábio,

What is your purpose to write this program?

If you just want to print something in your program, I think you can check the following article, and the printer is encapsulated for the user, and the OS will do everything for you (ensure the driver was installed).

http://support.microsoft.com/kb/322091

And if you want to manipulate USB printer in your program, I think you should know all the parameters of the hardware exactly, such as device name, port number, and so on. And I think use the assemble language, and C/C++ will be more convenient to access a hardware.

Regards,

Xun

Xun Ye

I think USB ports aren't treated as ports but as adresses and this would depend on the device. The question is how to get the device so I can use CreateFile with it.

Fábio Franco

No one?

Fábio Franco
Maybe you can still connect the printer at properties/Ports to LPTx: and use it the sameway?
boothwine
Nope, that won't work when I try to connect to a USB port.

Fábio Franco
What do you exactly have?

A USB-printer port and than your zebra printer
Or do you have a USB printer?

And what said was, connect the printer via it properties to say LPT1:
and still use Createfile("LPT1:"....
boothwine

Sorry if I didn't make this clear. I currently use CreateFile("LPT1:"... to send commands to zebra printers here. But now there are new desktop PCs that do not come with a parallelport, so I can't use LPT1, instead, the zebra printer gets connected through the USB port, so it is not an option to use a LPT1 port to send commands to it, so I need to send commands through USB ports. However, it is not so simple to connect to an USB port as it is to LPT1, I still couldn't figure out how to identify the USB port used by the printer, so I could send commands via CreateFile Win32 API.

What I really want is to find the printer in one of the USB ports and use it to connect to it through CreateFile.

What would be the string to replace "LPT1:"? How to get this string, specially dinamically, without resorting to literal strings, is there an API that might help me with that?

Maybe now it is a little more clear on what I am trying to do?

Thanks for your time.

Regards,

Fábio

Fábio Franco

Hi Fábio,

What is your purpose to write this program?

If you just want to print something in your program, I think you can check the following article, and the printer is encapsulated for the user, and the OS will do everything for you (ensure the driver was installed).

http://support.microsoft.com/kb/322091

And if you want to manipulate USB printer in your program, I think you should know all the parameters of the hardware exactly, such as device name, port number, and so on. And I think use the assemble language, and C/C++ will be more convenient to access a hardware.

Regards,

Xun

Xun Ye

Thanks Xun, the RawPrinterHelper was what I needed, but I thought I could use CreateFile Win32 API for the USB port the same way I used for the LPT1 port, just using CreateFile API. But I think this will do. The reason I need this is because I work with Zebra label printer and I often need to send commands to it in its programming language (EPL2).

But thanks for the help.

Regards,

Fábio

Fábio Franco

HiFábio,

I have the same problem.

If you have found a solution please say me.

My problem seems like yours, I need to replace the LPT1: by another pointer to my printer.

This pointer to me need to be FIXED.

Then, you have some solutions by I don't know exactly if they work for you, for me not because I need to use the same configuration in all computers, and unfortunelly same things change.

Solution 1:

// Sharing the printer it's the easiest way to achieve it.

\\127.0.0.1\SharedPrinter

// If your USB generates a COM port.

\\.\COM10

// If you don't need to have the name fixed

\\computername\SharedPrinter

I need another solution, i need to reach the shared printar locally, but without using the "127.0.0.1" because the infra-structure team here does not like this solution. they told me that this address is for an exclusive use.. just for tests.

Thanks in advance,

Wagner

Wagner Bertolini Junior
Hi. i'm working with USB Zebra barcode printer and have a solution for you! open your command prompt-> go to C:\ -> type command net use lpt2: \\path of printer /persistant:yes For more info. Search "net use" command. Instade of LPT1 use LPT2 in your code,it will work. This is working for me :).But i don't want to do like this so i'm writing some VC++ code to do same task (my writefile API is returning true but i'm not able to print).
  • Proposed As Answer byJyotiranjanr Tuesday, September 22, 2009 9:41 AM
  •  
Jyotiranjanr

You can use google to search for other answers

Custom Search

More Threads

• explicit interface implementation of ICollection in Generic Classes
• Problem with CodeDomProvider's Temporary Files
• Help me about ThreadStart in C#
• Adding picture to System.Windows.Forms.DataGird 1.1
• How to create an proprietary framework in .NET 3.5
• Choosing function according to Windows Version in C#
• .NET 1.1 in Visual Studio 2005
• Dinamically created Enum
• Compatibility
• how to implement auto casting?