Invalid File Handle error in my program

This is a discussion on Invalid File Handle error in my program within the Windows Programming forums, part of the Platform Specific Boards category; Hello everyone, I am having trouble figuring out where the bug is in my code. I have two functions which ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    55

    Invalid File Handle error in my program

    Hello everyone,
    I am having trouble figuring out where the bug is in my code.
    I have two functions which are used to access a serial port.
    When i combine then it works correctly ,but when i split them into smaller functions i get "INVALID_FILE_HANDLE"(getlasterror code number : 6)
    IN serial_functions
    Code:
    #include <serial_functions.h>
    void serialport_init(HANDLE hSerial)
    {
        hSerial = CreateFile("COM3",GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
        if(hSerial==INVALID_HANDLE_VALUE)
        {
            if(GetLastError()==ERROR_FILE_NOT_FOUND)
            {
                printf("Serial Port does not exist.\n");
                exit(1);
            }
            printf("Some other error occured.\n");
            exit(1);
        }
        //printf("%d\n",GetHandleInformation(hSerial, lpdwFlags));
        //getch();
        printf("Port opened succesfully.\n");
    
    
        if (!GetCommState(hSerial, &dcbSerialParams))
        {
            printf("Error getting state.\n");
            exit(2);
        }
        dcbSerialParams.BaudRate=CBR_19200;
        dcbSerialParams.ByteSize=8;
        dcbSerialParams.StopBits=ONESTOPBIT;
        dcbSerialParams.Parity=NOPARITY;
        if(!SetCommState(hSerial, &dcbSerialParams))
        {
            printf("Error setting serial port state.\n");
        }
        printf("Parameters set.\n");
        timeouts.ReadIntervalTimeout=50;
        timeouts.ReadTotalTimeoutConstant=50;
        timeouts.ReadTotalTimeoutMultiplier=10;
        timeouts.WriteTotalTimeoutConstant=50;
        timeouts.WriteTotalTimeoutMultiplier=10;
        if(!SetCommTimeouts(hSerial, &timeouts))
        {
            printf("Error setting timeouts.\n");
            exit(3);
        }
        printf("Timeouts set.\n");
    }
    void writedata(HANDLE hSerial,char *b,int n)
    {
        if(!WriteFile(hSerial, *b, n, &dwBytesRead, NULL))
        {
            printf("Error occured while writting data.\n");
             //printf("%d\n",GetHandleInformation(hSerial, lpdwFlags));
            //system("pause");
            exit(5);
        }
        printf("Data Sent.\n");
    }
    /*
    void readdata(int n)
    {
        if(!ReadFile(hSerial, rzBuff, n, &dwBytesRead, NULL))
        {
            printf("Error occured while reading data.\n");
            exit(4);
        }
    }
    */
    And in my main.c
    Code:
    #include "main.h"
    #include "serial_functions.h"
    int main()
    {
        printf("\n");
        serialport_init(hSerial);
        pointer_xyz = xyz;
        writedata(hSerial,pointer_xyz,1);
    
    
        return 0;
    }
    The program compiles and runs fine but i am not able to get the desired result in "writedata(hSerial,pointer_xyz,1);"

    When i merge the two functions they work perfectly
    Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,577
    To change a parameter passed to a function inside a function there is two ways.
    1. One return the changed parameter using the return keyword.
    2. Pass the parameter via pointer (and in C++ you can also use via reference)

    I suggest re-reading the section of the book on pointers; or if pointers have not yet been discussed in the book using the "return" keyword.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    55
    Hello Tim,
    Thank you for you reply.

    There did make a mistake in my earlier post.
    I am passing just b not *b in my function.I realized i had made that mistake after reading your post.
    Anyway i got the result using return.
    Code:
    #include "serial_functions.h"
    int main()
    {
        printf("\n");
        myhandle = serialport_init(hSerial);  //myhandle is a handle
        pointer_xyz = xyz;
        writedata(myhandle,pointer_xyz,1);
        return 0;
    }
    In serial_functions.c
    Code:
    #include "serial_functions.h"
    HANDLE serialport_init(HANDLE hSerial)
    {
        hSerial = CreateFile("COM5",GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
        if(hSerial==INVALID_HANDLE_VALUE)
        {
            if(GetLastError()==ERROR_FILE_NOT_FOUND)
            {
                printf("Serial Port does not exist.\n");
                exit(1);
            }
            printf("Some other error occurred.\n");
            exit(1);
        }
        printf("Port opened successfully.\n");
    
    
        if (!GetCommState(hSerial, &dcbSerialParams))
        {
            printf("Error getting state.\n");
            exit(2);
        }
        dcbSerialParams.BaudRate=CBR_19200;
        dcbSerialParams.ByteSize=8;
        dcbSerialParams.StopBits=ONESTOPBIT;
        dcbSerialParams.Parity=NOPARITY;
        if(!SetCommState(hSerial, &dcbSerialParams))
        {
            printf("Error setting serial port state.\n");
        }
        printf("Parameters set.\n");
        timeouts.ReadIntervalTimeout=50;
        timeouts.ReadTotalTimeoutConstant=50;
        timeouts.ReadTotalTimeoutMultiplier=10;
        timeouts.WriteTotalTimeoutConstant=50;
        timeouts.WriteTotalTimeoutMultiplier=10;
        if(!SetCommTimeouts(hSerial, &timeouts))
        {
            printf("Error setting timeouts.\n");
            exit(3);
        }
        printf("Timeouts set.\n");
        return hSerial;
    }
    void writedata(HANDLE hSerial,char *b,int n)
    {
        if(!WriteFile(hSerial, b, n, &dwBytesRead, NULL))
        {
            printf("Error occurred while writing data.\n");
            exit(5);
        }
        printf("Data Sent.\n");
    }
    Could you please tell me how to do it via pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. window handle invalid
    By handle in forum Windows Programming
    Replies: 13
    Last Post: 06-30-2010, 09:14 AM
  2. possible reasons for invalid handle from CreateFile
    By bling in forum Windows Programming
    Replies: 1
    Last Post: 10-01-2008, 08:06 AM
  3. the handle is invalid
    By m37h0d in forum C++ Programming
    Replies: 17
    Last Post: 07-09-2008, 11:29 PM
  4. Invalid Handle Error
    By smarta_982002 in forum Windows Programming
    Replies: 2
    Last Post: 03-24-2008, 10:48 AM
  5. SetFileTime returns invalid handle
    By leonv in forum Windows Programming
    Replies: 5
    Last Post: 01-21-2007, 05:57 PM

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