"Set of bit flags that specifies how the object can be shared. If
dwShareMode is 0, the object cannot be shared. Subsequent open
operations on the object will fail, until the handle is closed"
I have used dwShareMode as 0 since its a COM port.However subsequent
open operations return ACCESS_DENIED inspite of the fact that the
handle to the com port is closed.
Has anyone had a problem like this before ......its really taking me a
lot of time and i cant figure it out at all.
thanx in advance and regards
vinay
Do you call CloseHandle() on the handle that you get from CreateFile()?
Try something like this:
class CComPort
{
HANDLE m_Handle ;
public:
CComPort( int portId );
virtual ~CComPort();
};
CComPort::CComPort( int PortId ) :
m_Handle( INVALID_HANDLE_VALUE )
{
m_Handle = CreateFile( ..., FILE_SHARE_NONE, OPEN_EXISTING, ... );
if ( m_Handle == 0 || m_Handle == INVALID_HANDLE_VALUE )
throw std::runtime_error("Could not open Com port");
}
CComPort::~CComPort()
{
if ( m_Handle != INVALID_HANDLE_VALUE )
::CloseHandle( m_Handle );
}
Vishal.
closed".
Does that tell you anything?
You said you close the COM port by setting the handle to NULL. This
effectively wipes out the handle and you get a nice resource leak. You
are supposed to call CloseHandle on the CreateFile HANDLE (as the
CreateFile documentation says). I suspect you are not calling
CloseHandle. This isn't Java where setting things to NULL makes them
magically clean themselves up ;)
Setting the handle to NULL doesn't close the handle, and it sounds like you are not
closing the handle.
I believe serial ports always ignore dwShareMode, and it is impossible to open them other
than exclusive anyway.
I don't believe the handle is closed. Note that if you duplicate the handle, pass it down
to a process as inherited, etc., then the COM1 port will not be available until ALL
handles are closed.
Put a breakpoint at your CloseHandle, and make sure that (a) you are calling it and (b) it
succeeds. Most people don't notice or care that CloseHandle is a boolean function. But if
you ARE callilng it, and are not using duplication or inheritance, make sure that the
CloseHandle is really succeeding. For example, write
VERIFY(CloseHandle(h));
and examine, in a watch-window, the value
@ERR,hr
which will give you the last error code for the current thread, and its textual
explanation.
joe
Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
You have to open COM ports in exclusive mode. Do something like this:
hTemp = CreateFile(_T("COM1"),
GENERIC_READ | GENERIC_WRITE,
0, // Must use exclusive mode
NULL, // No security attributes
OPEN_EXISTING, // Must use open existing
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // Use zero here if
only using a single thread
NULL // Comm devices must use null template
);
Examine the MTTTY.EXE sample program from MSDN to see how to use
overlapped IO with separate threads for reading and writing.
Hope this helps,
Kurt
On 23 Jul 2004 07:05:39 -0700, vinaym...@yahoo.com (vinay) wrote:
Kurt Grittner <grittkmg...@mailbag.com> wrote in message news:<kiiag0tbg0vsj4gq5...@4ax.com>...