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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.


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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Code works but I need to redo it and assign values to Model 7 42
use of unassigned local variable 'id' 6 38
Where did System.Data.Objects go? 2 23
Printing 1 50
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now