Readfile() - Newbi help

This is a discussion on Readfile() - Newbi help within the C++ Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <windows.h> #include <pt_ioctl.c> int __cdecl main(void) { char quit; DWORD dwRead; OVERLAPPED osReader = {0}; unsigned ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    16

    Readfile() - Newbi help

    Code:
    #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')
        {
            
            OpenPortTalk();
            
            HANDLE hComm;
            hComm = CreateFile("COM1",  
                    GENERIC_READ | GENERIC_WRITE, 
                    0, 
                    0, 
                    OPEN_EXISTING,
                    FILE_FLAG_OVERLAPPED,
                    0);
                    
            value = ReadFile(hComm, inBuffer, 12, &dwRead, &osReader);
        
            ClosePortTalk();
        
        }
        
        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.

    Thanks.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>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
    Posts
    16
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21