Hello
I want to turn on a LED using C, meaning that I want to write on printer port.

but the code doesn't work.

I use char ledStatus instead of BYTE ledStatus. is there any difference??

what is the problem in this code?

#include <windows.h>
#include <conio.h>
#include <staio.h>
#define LED_ON  1

int main()
{
   HANDLE h;
   unsigned long  dwSize=1;
   int success;

   h = CreateFile(
      L"LPT1",
      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
   );

   if (INVALID_HANDLE_VALUE == h)
   {
      //Handle Error
      printf("CreateFile failed with error %d\n", GetLastError());
      exit(1);
   }
   else
   {
      printf("CreateFile1 Successful\n");
   }

   char   ledStatus;
   // turn on LED
   ledStatus = LED_ON;
   success = WriteFile(h, &ledStatus, 1, &dwSize, NULL);
   if (success)
   {
      printf("File Write Successful - %i bytes\n", dwSize);
   }
   else
   {
      printf("File Write Failed\n");
   }

   // close port
   CloseHandle(h);
   return 0;
}

I use char ledStatus instead of BYTE ledStatus. is there any difference??

BYTE is defined by Microsoft windows.h as unsigned char.

line 3: what is "staio.h"? is it "stdio.h" misspelled?

but the code doesn't work.

What to you mean by that? Does WriteFile() return success? Yes or No. Does your program compile without errors?

Does your computer even have a printer port? Most new computers don't because they are USB ports.

sorry that was "stdio.h" .
yes my program compiles without errors and my computer has printer port . i use a printer cable and i connect that some LEDs. but WriteFile() doesn't return success.

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.