Thread: Readfile() - Newbi help

  1. #1
    Registered User
    Join Date
    Apr 2003

    Readfile() - Newbi help

    #include <stdio.h>
    #include <windows.h>
    #include <pt_ioctl.c>
    int __cdecl main(void)
        char quit; 
        DWORD dwRead;
        OVERLAPPED osReader = {0};
        unsigned char value;
        unsigned char inBuffer;
        quit = '\0';
        while (quit != 'q')
            HANDLE hComm;
            hComm = CreateFile("COM1",  
                    GENERIC_READ | GENERIC_WRITE, 
            value = ReadFile(hComm, inBuffer, 12, &dwRead, &osReader);
        return 0;
    Well, any help on readfile. I want a quick fix, and my brain is hurting to the point i need to sleep, but i need to get this done.


  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    >>any help on readfile. I want a quick fix
    Ask a well thought out question, you'll get a well thought out answer.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2003
    Haha funny.

    All i need is to be able to use readfile to get the input from my device.

    Easy eh??

    So, any helpfull ideas which will help me...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Considering that it shouldn't even compile, let alone run, what were you expecting

    > value = ReadFile(hComm, inBuffer, 12, &dwRead, &osReader);
    1. check that hComm is valid, by actually checking the status return of the CreateFile()
    2. the 2nd parameter is supposed to be a pointer to where you want the answer stored. This is not a pointer, you should be getting error messages
    3. the 3rd parameter is the buffer length, and 12 most assuredly is not it's length.

    char inBuffer[12];
    value = ReadFile(hComm, inBuffer, sizeof(inBuffer), &dwRead, &osReader);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Also, since you are providing an overlapped structure, you should really wait for completion by allocating an event handle, storing that in the overlapped structure, and then WaitForSingleObject or similar BEFORE closing the port. Closing the port immediately after the read will simply cause the read operation to be cancelled as the version of ReadFile you are using is Asynchronous (ie. runs in the background and may return before anything has been done).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using ReadFile, WriteFile, strSafe
    By talz13 in forum C++ Programming
    Replies: 0
    Last Post: 02-08-2005, 11:37 AM
  2. ReadFile function fails.
    By dpkeller in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2004, 09:20 PM
  3. fread readfile
    By samsam1 in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2003, 01:14 PM
  4. Using API function ReadFile()
    By Echidna in forum Windows Programming
    Replies: 4
    Last Post: 10-23-2001, 10:33 PM
  5. ReadFile?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 10-23-2001, 01:13 AM
Website Security Test