Unhandled Exception on CreateFile
-
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:
-
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:
What happens if you use
OPEN_EXISTING
instead ofCREATE_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
-
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:
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] -
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:
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:
-
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:
NULL
is notINVALID_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] -
NULL
is notINVALID_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]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:
-
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:
Mark Salsbery wrote:
Not everything uses 0 as invalid values....that was my main point to the OP
Actually it was
NULL
not0
. Are you assuming theNULL
it is always0
? 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] -
Mark Salsbery wrote:
Not everything uses 0 as invalid values....that was my main point to the OP
Actually it was
NULL
not0
. Are you assuming theNULL
it is always0
? 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]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:
-
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:
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]