在做一個串口通訊,遇到了這個問題,com1到com9正常開啟,com10以上就不行了,用getlasterror查看,錯誤位找不到指定檔案,後查看MSDN,解釋如下:
Windows NT: You can use paths longer than MAX_PATH characters by calling the wide (W) version of CreateFile and prepending "\\?\" to the path. The "\\?\" tells the function to turn off path parsing. This lets you use paths that are nearly 32,000 Unicode
characters long. However, each component in the path cannot be more than MAX_PATH characters long. You must use fully-qualified paths with this technique. This also works with UNC names. The "\\?\" is ignored as part of the path. For example, "\\?\C:\myworld\private"
is seen as "C:\myworld\private", and "\\?\UNC\tom_1\hotstuff\coolapps" is seen as "\\tom_1\hotstuff\coolapps".
當路徑長度大於MAX_PATH 後,應加上"\\?\",個人猜想,可能開啟串口的話只能支援com1到com9,大於4個字元就不行了,故com10以上應添加"\\?\",即
CreateFile("\\\\?\\COM10", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
這樣就能成功開啟。以上方法所有com1~9也是可以的,所以建議就用這種方法吧。
後在網上看到說加"\\.\",試了一下也可以,和"\\?\"一樣,我現在就在想“?”到底是什麼意思,是不是換成其他符號也可以呢,後來試了,只有換成“."可以,關於這個問題還請高手解答。