windows函数调用出错时,可以通过方法去查询出错根源在哪里。
- 1 #include <Windows.h>
- 2 #include <iostream>
- 3
- 4 using namespace std;
- 5
- 6 int main(){
- 7 HANDLE hf = CreateFile(TEXT("c:\\test"),0,0,NULL,OPEN_EXISTING,0,NULL);
- 8 cout<<(int)hf<<endl;
- 9 if((int)hf != 1){
- 10 int res = GetLastError();
- 11 cout<<res<<endl;
- 12 }
- 13 system("pause");
- 14 return 0;
- 15 }
错误号是2,错误信息可以通过vs2010的工具->错误查找 进行查找。
也可以在调试debug时,输入$err,hr,然后在watch窗口下,查看出错原因。
用户也可以自定义错误。
- void f(){
- SetLastError(3);
- return;
- }
错误号码可以使用微软的,也可以自定义。
0-31位,需要在29位设置为1,这样就是用户自己定义的错误。
比如自己定义的错误:不和微软产生冲突。