Hello
i have a printer cable that i connect to it some LEDs .
i want to use CreateFile and WriteFile functions but i don't know that i should use LPT1 and LPT2 or COM1 and COM2 in CreateFile function.

h = CreateFile( 
L"LPT1", // pointer to name of the file 
GENERIC_WRITE, // access (read-write) mode 
0, // share mode
NULL, // pointer to security attributes 
OPEN_EXISTING, // how to create 
FILE_ATTRIBUTE_NORMAL, // file attributes 
NULL // handle to file with attributes to copy 
);

Which one, if any, of those you use will depend on how the cable is connected to the PC. Is it connected to LPT1, COM port, or USB? Most printers today are USB.

i use this cable for a desktop computer .
and i have another question . does this code work on windows 7?

I can't tell if that connects to a parallel or serial port. I don't see why it shouldn't work on Windows 7 as long as the hardware exists.

I found the below comment on MSDN (scroll to the bottom of the page)

Opening LPT Ports
Do not treat LPT ports as communication devices and specify OPEN_EXISTING as the dwCreationDisposition parameter. Doing so will cause the function to fail when called on Vista or a 2008 server to open an LPT port that is mapped to a network share. GetLastError() returns ERROR_FILE_NOT_FOUND.

Instead you can use the following parameters which I determined by using PROCMON.EXE from SysInternals to monitor calls to CreateFile() triggered by redirecting the output from DIR to a mapped LPT port eg. dir >lpt3

CreateFile("LPT3", GENERIC_WRITE | FILE_READ_ATTRIBUTES, FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0);

The above has been tested on XP SP3, Windows Vista SP1 and Windows 2008 Terminal Server.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.