Parallel Port access with C#

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.

Who is Participating?
philippe_leybaertConnect With a Mentor Commented:
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);
This should do it:

In addition, you could always write it in a C++ com and interop to it.
ChaveTechAuthor Commented:
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?
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.


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.
Windows knows how to Map files like this?  I had no idea....
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.