CreateFile (kernel32)
Last changed: RomanRII-98.208.78.243

.
Summary

C# Signature:

[DllImport("kernel32.dll", SetLastError=true)]
static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess,
   uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
   uint dwFlagsAndAttributes, IntPtr hTemplateFile);

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

You can use the IntPtr from Createfile with FileStream. This is usefull for opening devices such as Com1:, Lpt1: and Prn.

For example:

IntPtr ptr = CreateFile(filename,access, share,0, mode,0, IntPtr.Zero);
/* Is bad handle? INVALID_HANDLE_VALUE */
if (ptr.ToInt32() == -1)
{
    /* ask the framework to marshall the win32 error code to an exception */
    Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
}
else
{
    return new FileStream(ptr,access);
}

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
CreateFile on MSDN
Documentation
FileStream on MSDN
Documentation
Marshal on MSDN

Thought I post a sample code to help out anyone else who may need help : )

/// Sample code provided as is.

/// Sample Composed by Wijaya "Wi" T

/// References:

/// http://www.dotnettalk.net/Needed_help_on_printing_from_C_and_printer-6941579-1297-a.html

/// http://www.dotnet247.com/247reference/msgs/16/84730.aspx

/// http://www.groupsrv.com/dotnet/viewtopic.php?t=72572

/// http://www.pinvoke.net/default.aspx/kernel32.CreateFile

/// Usage:

/// Initialize this class in your main code (see below),

/// and walla you are now printing!

///

/// e.g.:

/// PrintFactory objPrintFactory = new PrintFactory();

/// Thank you:

/// Thank you all who have been contributing to printing to LPT port.

/// God Bless you! : )

/// Addt'l info:

/// Don't forget to reference:

/// using System.Runtime.InteropServices;

/// using System.IO;

///

public class PrintFactory

{

    public const short FILE_ATTRIBUTE_NORMAL = 0x80;
    public const short INVALID_HANDLE_VALUE = -1;
    public const uint GENERIC_READ = 0x80000000;
    public const uint GENERIC_WRITE = 0x40000000;
    public const uint CREATE_NEW = 1;
    public const uint CREATE_ALWAYS = 2;
    public const uint OPEN_EXISTING = 3;        

    [DllImport("kernel32.dll", SetLastError=true)]
    static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess,
        uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
        uint dwFlagsAndAttributes, IntPtr hTemplateFile);

    public PrintFactory()
    {
        IntPtr ptr = CreateFile("LPT1", GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

        /* Is bad handle? INVALID_HANDLE_VALUE */
        if (ptr.ToInt32() == -1)
        {
            /* ask the framework to marshall the win32 error code to an exception */
            Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());                
        }
        else
        {                                    
            String Temp = "This is a test print";

            //This is the cut command on Star TSP 600 Printer
            char[] prnCmdCut = {(char)27, (char)100, (char)51};                

            FileStream lpt = new FileStream(ptr,FileAccess.ReadWrite);
            Byte[] Buff = new Byte[1024];

            //Check to see if your printer support ASCII encoding or Unicode.
            //If unicode is supported, use the following:
            //    Buff = System.Text.Encoding.Unicode.GetBytes(Temp);
            Buff = System.Text.Encoding.ASCII.GetBytes(Temp);
            lpt.Write(Buff,0,Buff.Length);                     

            Buff = System.Text.Encoding.ASCII.GetBytes(prnCmdCut);
            lpt.Write(Buff,0,Buff.Length);

            lpt.Close();
        }
    }

}