Parallel Port access with C#

Posted on 2004-05-01
Last Modified: 2012-06-27
I'm looking for some help on writing to the parallel port within WinXP Pro using C#.  I'm not sure how this is done and I can't find any substantial resources online.  All I want to do is be able to write a byte, that's it, nothing else.

Question by:ChaveTech

Expert Comment

ID: 10969965
This should do it:

In addition, you could always write it in a C++ com and interop to it.

Author Comment

ID: 10970057
I've been to that site and have tried it, but it's not working.  I don't know if the DLL the author provides is valid.  I don't have a problem writing it in C++, but I'm not familiar with writing in COM.  Do you have any suggestions?

Expert Comment

ID: 10970199
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.


Expert Comment

ID: 10970873

With com, all you need to do is create an ATL com object.  Use the template.  Add a method called to the COM interface you create called WriteParallelByte which takes in a byte (as an int).  You can then simply include this in a C# project by adding a reference to the COM object (dll).  .NET Studio will then automatically create an interop dll that will allow you to instantiate this com object and make the call.

Accepted Solution

philippe_leybaert earned 500 total points
ID: 11003906
If you just want to write some bytes or text to the parallel port, you can create an output file stream that maps to the parallel port:

The drawback is that you need to call unmanaged code from kernel32.dll

class myClass
   [DllImport("kernel32.dll", CharSet=CharSet.Unicode)]
   private static extern IntPtr CreateFile(
                    string lpFileName,
                    [MarshalAs(UnmanagedType.U4)] uint dwDesiredAccess,
                    [MarshalAs(UnmanagedType.U4)] int dwShareMode,
                    [MarshalAs(UnmanagedType.U4)] uint lpSecurityAttributes,
                    [MarshalAs(UnmanagedType.U4)] int dwCreationAttributes,
                    [MarshalAs(UnmanagedType.U4)] int dwFlagsAndAttributes,
                    IntPtr hTemplateFile

   public FileStream createLPT1_FS()
      IntPtr h = CreateFile("LPT1",0xC0000000,0,0,3,0,(IntPtr)0);

      return new FileStream(h,FileAccess.ReadWrite);

Expert Comment

ID: 11006866
Windows knows how to Map files like this?  I had no idea....

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Install Problem 13 34
SSRS subreport with parameter 3 47
Cant save 3D 4 23
C# Linq - Join two objects into one 3 13
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question