使用WriteFile() GetLastError()的返回值是5,如何提升权限?谢谢

a3622041 a3622041 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我自己写了1个OCX 然后再写了JS调用这个OCX来打开串口,但是发送数据需要使用WriteFile函数。结果数据发不出来,调试发现GetLastError返回值是5,拒绝访问,都说是权限不够。如何解决,再VC VB中使用这两个却没有问题。 来高人!!
ComHandle=CreateFile(Port,
GENERIC_READ||GENERIC_WRITE,
0,NULL,OPEN_EXISTING,NULL,NULL);
if(ComHandle==INVALID_HANDLE_VALUE){MessageBox("1"); return 0;}
char tmpP[11]="9600,n,8,1";//设置通讯参数
DCB dcb;
if(GetCommState(ComHandle,&dcb)==0){MessageBox("2");return -1;}
if(BuildCommDCB(tmpP,&dcb)==0){MessageBox("3");return -1;}
if(SetCommState(ComHandle,&dcb)==0){MessageBox("4");return -1;}
COMMTIMEOUTS TimeOuts;//设置超时时间
GetCommTimeouts(ComHandle,&TimeOuts);
TimeOuts.ReadIntervalTimeout=2000;
TimeOuts.ReadTotalTimeoutConstant=400;
TimeOuts.ReadTotalTimeoutMultiplier=400;
TimeOuts.WriteTotalTimeoutMultiplier=40;
TimeOuts.WriteTotalTimeoutConstant=400;
if(SetCommTimeouts(ComHandle,&TimeOuts)==0){MessageBox("6");return -2;}
if(SetupComm(ComHandle,2048,1024)==0){MessageBox("7");return -3;}//设置缓冲区
//PurgeComm(ComHandle,PURGE_RXABORT||PURGE_RXCLEAR);
//PurgeComm(ComHandle,PURGE_TXABORT||PURGE_TXCLEAR);
if(ComHandle<0){MessageBox("111");return 111;}
BYTE m_SendBuffer[1];
DWORD m_Number;
m_SendBuffer[0]=0x06;
if(WriteFile(ComHandle, m_SendBuffer, 1 , &m_Number,NULL)==0)
{
DWORD ErrorCode;
ErrorCode=GetLastError();
char temp[5];
sprintf(temp,"%d",ErrorCode);
MessageBox(temp);
MessageBox("here!");
return -5;
}
CloseHandle(ComHandle);
// Write();
MessageBox(Port);
return 0;
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
a3622041
ATL 3,248 成员 48,530 内容