开发者

Why does the following code work in Windows XP but not 7?

I have connected a USB printer, and I am trying to open that port using Open method of CFile class. The following code works properly in Windows XP, but it does not work in Windows 7:

#include<stdio.h>
#include<afx.h>
void main()
{
    CFile m_Port;
    CString PortName = "\\\\?\\usb#vid_0b0b&pid_106e#sx0000000-tc0000#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}";
    int i;
    i = m_Port.Open (PortName ,2,0);//PortName is DevicePath
    if ( i > 0 )
        printf("Done");
    else
        printf("Its not working");

    i=GetLastError();
    printf("\n\nError=%d",i);
}

In Windows XP it works properly, but in the case of Windows 7, GetLastError returns a value of 3. How can I fix this code to work on both Windows XP and Windows 7? Let me know if 开发者_运维百科their are any different GUID in windows 7. Are their any other option to open port using device path in windows 7?


This should work with Windows 7, but perhaps you have to run the application elevated. I guess, the path is wrong (and this is what error 3 says). Open the device manager, select the device and "Properties" in the context menu. Then "Details" and there "Hardware IDs". Do you find the path from your code sample here? If not, change your code accordingly.

Also: For this kind of system-near programming I would use ::CreateFile from WIN-API and not CFile class.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜