开发者

ERROR_DEV_NOT_EXIST when ::CreateFile in C++ MFC?

I am writing to open a port using this function:

HANDLE hFile = ::CreateFile(pszComName, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0,0);

// Check if we could open the device
if (hFile == INVALID_HANDLE_VALUE)
{
    DWORD hh=  ::GetLastError();

              error.Format(_T("test - [%d]"),hh); 

      AfxMessageBox(error,MB_ICONSTOP);
}

I cannot open the port and system error code I receive is 55: ERROR_DEV_NOT_EXIST 55 (0x37) from th开发者_开发知识库is list

what can i do to open the port? thanks

EDIT: I Enumerate Ports like this:

 for (UINT i=1; i<256; i++)
    {

      CString sPort;
      sPort.Format(_T("COM%d"), i);

      HANDLE hPort = ::CreateFile(sPort, GENERIC_READ | GENERIC_WRITE, 0, 0,OPEN_EXISTING,0, 0);
      if (hPort == INVALID_HANDLE_VALUE)
      {
        DWORD dwError = GetLastError();
      }
      else
      {
        AfxMessageBox(_T("1 open"));
        CloseHandle(hPort);
      }
    }

I also checked these formats:

sPort1.Format(_T("URT%d"), i);

sPort3.Format(_T("\.\COM%d"), i);

sPort4.Format(_T("\\.\COM%d"), i);

and sPort5.Format(_T("\COM%d"), i);

but I couldnt find any.


COM ports names in Windows CE/Mobile are in the format of COMX: - the difference is the colon - (for example COM1:).

Your code should look like this: CreateFile(L"COM1:",...)

You can also check the port name through the registry. If you have an ActiveSync connection, use a remote registry editor and go to [HKLM\Drivers\Active] - one of the subkeys will hold the information of the port you want (assuming it is loading properly).


The obvious thing to check is whether you have the name correct. For a COM port, it'll normally be something like \\.\com1, but in C or C++ you need to escape all the back-slashes, so it'll look like "\\\\.\\com1" if you're using a string literal.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜