Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Unhandled Exception on CreateFile

Unhandled Exception on CreateFile

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jimjim733
    wrote on last edited by
    #1

    Hi there, I am trying to populate a combo box of available com ports. I call my openSerialport function from my init dialog function but as soon as CreateFile is executed I get an Unhandled Exception error which has the info 0xC0000005 : Access Violation. Can anyone help??????? Code Below InitDialog call

    BOOL CPTZControlDlg::OnInitDialog()
    {
    int i, j, hr;
    char portStr[7];

    CDialog::OnInitDialog();
    
    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m\_hIcon, TRUE);			// Set big icon
    SetIcon(m\_hIcon, FALSE);		// Set small icon
    
    // construct a list of available COM ports and put into combo box
    j=0;
    for (i=0 ; i<PORTNUM ; i++) 
    {
    	hr = m\_serialCtrl->OpenSerialPort(i,0);
    	if (hr == 0) 
    	{
    		g\_exists\[i\] = 1;
    		g\_nComPortInd\[j\] = i;
    		g\_comport\[i\] = j;
    		j++;
    		sprintf(portStr,"COM%d",i+1);
    		m\_comPort.AddString(portStr);
    
    	} 
    	else 
    		g\_exists\[i\] = 0;
    }
    
    return TRUE;  // return TRUE  unless you set the focus to a control
    

    }

    Open Serial port function

    int CSerialControl::OpenSerialPort(int pcCommPortInd,int radd)
    {
    DCB dcb;
    BOOL fSuccess;
    char pcCommPort[14];
    int m_nComPort;

    m_nComPort = pcCommPortInd;

    sprintf(pcCommPort,"COM1",pcCommPortInd+1);

    m_hCom = CreateFile( "COM1",
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    CREATE_ALWAYS,
    FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED,
    NULL
    );

    if (m_hCom == INVALID_HANDLE_VALUE)
    {
    m_hCom = NULL;
    return COMMSERROR;
    }

    if (radd==0)
    {
    CloseSerialPort();
    return COMMSGOOD;
    }

    Cheers Jim :confused:

    D CPalliniC M 3 Replies Last reply
    0
    • J jimjim733

      Hi there, I am trying to populate a combo box of available com ports. I call my openSerialport function from my init dialog function but as soon as CreateFile is executed I get an Unhandled Exception error which has the info 0xC0000005 : Access Violation. Can anyone help??????? Code Below InitDialog call

      BOOL CPTZControlDlg::OnInitDialog()
      {
      int i, j, hr;
      char portStr[7];

      CDialog::OnInitDialog();
      
      // Set the icon for this dialog.  The framework does this automatically
      //  when the application's main window is not a dialog
      SetIcon(m\_hIcon, TRUE);			// Set big icon
      SetIcon(m\_hIcon, FALSE);		// Set small icon
      
      // construct a list of available COM ports and put into combo box
      j=0;
      for (i=0 ; i<PORTNUM ; i++) 
      {
      	hr = m\_serialCtrl->OpenSerialPort(i,0);
      	if (hr == 0) 
      	{
      		g\_exists\[i\] = 1;
      		g\_nComPortInd\[j\] = i;
      		g\_comport\[i\] = j;
      		j++;
      		sprintf(portStr,"COM%d",i+1);
      		m\_comPort.AddString(portStr);
      
      	} 
      	else 
      		g\_exists\[i\] = 0;
      }
      
      return TRUE;  // return TRUE  unless you set the focus to a control
      

      }

      Open Serial port function

      int CSerialControl::OpenSerialPort(int pcCommPortInd,int radd)
      {
      DCB dcb;
      BOOL fSuccess;
      char pcCommPort[14];
      int m_nComPort;

      m_nComPort = pcCommPortInd;

      sprintf(pcCommPort,"COM1",pcCommPortInd+1);

      m_hCom = CreateFile( "COM1",
      GENERIC_READ | GENERIC_WRITE,
      0,
      NULL,
      CREATE_ALWAYS,
      FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED,
      NULL
      );

      if (m_hCom == INVALID_HANDLE_VALUE)
      {
      m_hCom = NULL;
      return COMMSERROR;
      }

      if (radd==0)
      {
      CloseSerialPort();
      return COMMSGOOD;
      }

      Cheers Jim :confused:

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      What happens if you use OPEN_EXISTING instead of CREATE_ALWAYS?

      "Love people and use things, not love things and use people." - Unknown

      "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

      1 Reply Last reply
      0
      • J jimjim733

        Hi there, I am trying to populate a combo box of available com ports. I call my openSerialport function from my init dialog function but as soon as CreateFile is executed I get an Unhandled Exception error which has the info 0xC0000005 : Access Violation. Can anyone help??????? Code Below InitDialog call

        BOOL CPTZControlDlg::OnInitDialog()
        {
        int i, j, hr;
        char portStr[7];

        CDialog::OnInitDialog();
        
        // Set the icon for this dialog.  The framework does this automatically
        //  when the application's main window is not a dialog
        SetIcon(m\_hIcon, TRUE);			// Set big icon
        SetIcon(m\_hIcon, FALSE);		// Set small icon
        
        // construct a list of available COM ports and put into combo box
        j=0;
        for (i=0 ; i<PORTNUM ; i++) 
        {
        	hr = m\_serialCtrl->OpenSerialPort(i,0);
        	if (hr == 0) 
        	{
        		g\_exists\[i\] = 1;
        		g\_nComPortInd\[j\] = i;
        		g\_comport\[i\] = j;
        		j++;
        		sprintf(portStr,"COM%d",i+1);
        		m\_comPort.AddString(portStr);
        
        	} 
        	else 
        		g\_exists\[i\] = 0;
        }
        
        return TRUE;  // return TRUE  unless you set the focus to a control
        

        }

        Open Serial port function

        int CSerialControl::OpenSerialPort(int pcCommPortInd,int radd)
        {
        DCB dcb;
        BOOL fSuccess;
        char pcCommPort[14];
        int m_nComPort;

        m_nComPort = pcCommPortInd;

        sprintf(pcCommPort,"COM1",pcCommPortInd+1);

        m_hCom = CreateFile( "COM1",
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        CREATE_ALWAYS,
        FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED,
        NULL
        );

        if (m_hCom == INVALID_HANDLE_VALUE)
        {
        m_hCom = NULL;
        return COMMSERROR;
        }

        if (radd==0)
        {
        CloseSerialPort();
        return COMMSGOOD;
        }

        Cheers Jim :confused:

        CPalliniC Online
        CPalliniC Online
        CPallini
        wrote on last edited by
        #3

        jimjim733 wrote:

        sprintf(pcCommPort,"COM1",pcCommPortInd+1);

        What is the purpose of the above line?

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • J jimjim733

          Hi there, I am trying to populate a combo box of available com ports. I call my openSerialport function from my init dialog function but as soon as CreateFile is executed I get an Unhandled Exception error which has the info 0xC0000005 : Access Violation. Can anyone help??????? Code Below InitDialog call

          BOOL CPTZControlDlg::OnInitDialog()
          {
          int i, j, hr;
          char portStr[7];

          CDialog::OnInitDialog();
          
          // Set the icon for this dialog.  The framework does this automatically
          //  when the application's main window is not a dialog
          SetIcon(m\_hIcon, TRUE);			// Set big icon
          SetIcon(m\_hIcon, FALSE);		// Set small icon
          
          // construct a list of available COM ports and put into combo box
          j=0;
          for (i=0 ; i<PORTNUM ; i++) 
          {
          	hr = m\_serialCtrl->OpenSerialPort(i,0);
          	if (hr == 0) 
          	{
          		g\_exists\[i\] = 1;
          		g\_nComPortInd\[j\] = i;
          		g\_comport\[i\] = j;
          		j++;
          		sprintf(portStr,"COM%d",i+1);
          		m\_comPort.AddString(portStr);
          
          	} 
          	else 
          		g\_exists\[i\] = 0;
          }
          
          return TRUE;  // return TRUE  unless you set the focus to a control
          

          }

          Open Serial port function

          int CSerialControl::OpenSerialPort(int pcCommPortInd,int radd)
          {
          DCB dcb;
          BOOL fSuccess;
          char pcCommPort[14];
          int m_nComPort;

          m_nComPort = pcCommPortInd;

          sprintf(pcCommPort,"COM1",pcCommPortInd+1);

          m_hCom = CreateFile( "COM1",
          GENERIC_READ | GENERIC_WRITE,
          0,
          NULL,
          CREATE_ALWAYS,
          FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED,
          NULL
          );

          if (m_hCom == INVALID_HANDLE_VALUE)
          {
          m_hCom = NULL;
          return COMMSERROR;
          }

          if (radd==0)
          {
          CloseSerialPort();
          return COMMSGOOD;
          }

          Cheers Jim :confused:

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          In addition to the "For communications resources, the dwCreationDisposition parameter must be OPEN_EXISTING" problem...

          jimjim733 wrote:

          if (m_hCom == INVALID_HANDLE_VALUE) { m_hCom = NULL; return COMMSERROR; }

          NULL is not an invalid handle value. INVALID_HANDLE_VALUE is an invalid handle value. Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          CPalliniC 1 Reply Last reply
          0
          • M Mark Salsbery

            In addition to the "For communications resources, the dwCreationDisposition parameter must be OPEN_EXISTING" problem...

            jimjim733 wrote:

            if (m_hCom == INVALID_HANDLE_VALUE) { m_hCom = NULL; return COMMSERROR; }

            NULL is not an invalid handle value. INVALID_HANDLE_VALUE is an invalid handle value. Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            CPalliniC Online
            CPalliniC Online
            CPallini
            wrote on last edited by
            #5

            NULL is not INVALID_HANDLE_VALUE, but (I guess) it is still is an invalid handle value. ;P That's said, I agree with you: he should return INVALID_HANDLE_VALUE. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            M 1 Reply Last reply
            0
            • CPalliniC CPallini

              NULL is not INVALID_HANDLE_VALUE, but (I guess) it is still is an invalid handle value. ;P That's said, I agree with you: he should return INVALID_HANDLE_VALUE. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              CPallini wrote:

              NULL is not INVALID_HANDLE_VALUE, but (I guess) it is still is an invalid handle value.

              In reality, yes. But that assumption is BAD programming when the API docs clearly state the return value is either a valid handle or INVALID_HANDLE_VALUE. :) Not everything uses 0 as invalid values....that was my main point to the OP :) ;P

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              CPalliniC 1 Reply Last reply
              0
              • M Mark Salsbery

                CPallini wrote:

                NULL is not INVALID_HANDLE_VALUE, but (I guess) it is still is an invalid handle value.

                In reality, yes. But that assumption is BAD programming when the API docs clearly state the return value is either a valid handle or INVALID_HANDLE_VALUE. :) Not everything uses 0 as invalid values....that was my main point to the OP :) ;P

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                CPalliniC Online
                CPalliniC Online
                CPallini
                wrote on last edited by
                #7

                Mark Salsbery wrote:

                Not everything uses 0 as invalid values....that was my main point to the OP

                Actually it was NULL not 0. Are you assuming the NULL it is always 0? Don't do that: it is a BAD programming!!! :laugh: ;P :-D ;P :laugh:

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                In testa che avete, signor di Ceprano?

                M 1 Reply Last reply
                0
                • CPalliniC CPallini

                  Mark Salsbery wrote:

                  Not everything uses 0 as invalid values....that was my main point to the OP

                  Actually it was NULL not 0. Are you assuming the NULL it is always 0? Don't do that: it is a BAD programming!!! :laugh: ;P :-D ;P :laugh:

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  CPallini wrote:

                  Are you assuming the NULL it is always 0?

                  :laugh: I knew you were going to bring that up! On any other board I wouldn't assume that. In VC++ it is 0 until the language or the CRT specs change ;P That's kinda goofy :) I like to redefine NULL to 3 (or (void*)3). :((

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  CPalliniC 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    CPallini wrote:

                    Are you assuming the NULL it is always 0?

                    :laugh: I knew you were going to bring that up! On any other board I wouldn't assume that. In VC++ it is 0 until the language or the CRT specs change ;P That's kinda goofy :) I like to redefine NULL to 3 (or (void*)3). :((

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    CPalliniC Online
                    CPalliniC Online
                    CPallini
                    wrote on last edited by
                    #9

                    Mark Salsbery wrote:

                    I knew you were going to bring that up!

                    MS's trap!!! :laugh:

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    In testa che avete, signor di Ceprano?

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups