C Style SEH handling Example
在此记录这段代码, 仅仅是为了以后使用时象使用文档模板一样方便而已.
C++中的try/catch/final, 无法catch C0000005这样的异常,虽然出现内存访问一定表示
程序设计的Bug,但这样的Bug有时候可能作为一个潜在的Bug随产品被Release出去时,可能
会带来某些麻烦,所以,此时用C提供的异常框架还是一个不错的选择。
1:2: typedef enum _EWinInetApiCode {3: winet_InertnetOpen = 0xFFFFFFF1,4: winet_InternetCrackUrl = 0xFFFFFFF2,5: winet_InternetConnect = 0xFFFFFFF3,6: winet_HttpOpenRequest = 0xFFFFFFF4,7: winet_HttpSendRequestEx = 0xFFFFFFF5,8: winet_InternetWriteFile = 0xFFFFFFF6,9: winet_HttpEndRequest = 0xFFFFFFF7,10: winet_InternetReadFile = 0xFFFFFFF811: }EWinInetApicode;12:13: int ExceptionHandler(DWORD dwExcpCode, struct _EXCEPTION_POINTERS *ep)14: {15:16: //DWORD dwExcpCode = ep->ExceptionRecord->ExceptionCode;17:18: switch(ep->ExceptionRecord->ExceptionInformation[0]) {19: case winet_InertnetOpen:20: printf("InertnetOpen Exception: 0x%08X", dwExcpCode);21: break;22: case winet_InternetCrackUrl:23: printf("InternetCrackUrl Exception: 0x%08X", dwExcpCode);24: break;25: case winet_InternetConnect:26: printf("InternetConnect Exception: 0x%08X", dwExcpCode);27: break;28: case winet_HttpOpenRequest:29: printf("HttpOpenRequest Exception: 0x%08X", dwExcpCode);30: break;31: case winet_HttpSendRequestEx:32: printf("HttpSendRequestEx Exception: 0x%08X", dwExcpCode);33: break;34: case winet_InternetWriteFile:35: printf("InternetWriteFile Exception: 0x%08X", dwExcpCode);36: break;37: case winet_HttpEndRequest:38: printf("HttpEndRequest Exception: 0x%08X", dwExcpCode);39: break;40: case winet_InternetReadFile:41: printf("InternetReadFile Exception: 0x%08X", dwExcpCode);42: break;43: default:44: printf("Other Exception: 0x%08X", dwExcpCode);45: break;46: }47:48: return EXCEPTION_EXECUTE_HANDLER;49: }50:51: __try {52: __try {53: hSession = InternetOpen(g_tcsAgent,54: INTERNET_OPEN_TYPE_PRECONFIG ,55: NULL,56: NULL,57: 0 // INTERNET_FLAG_ASYNC58: );59: if(hSession == NULL) {60: ulExcpArgu[0] = winet_InertnetOpen;61: RaiseException(GetLastError(), 0, 1, ulExcpArgu);62: }63:64:65: InitUrlComponents(&urlComponents);66: if(!InternetCrackUrl( g_tcsUrl, 0, ICU_DECODE, &urlComponents )) {67: ulExcpArgu[0] = winet_InternetCrackUrl;68: RaiseException(GetLastError(), 0, 1, ulExcpArgu);69: }70:71: hConnect = InternetConnect(72: hSession, // HINTERNET hInternet,73: urlComponents.lpszHostName, // LPCTSTR lpszServerName,74: urlComponents.nPort, // INTERNET_PORT nServerPort,75: NULL, // LPCTSTR lpszUserName,76: NULL, // LPCTSTR lpszPassword,77: INTERNET_SERVICE_HTTP, // DWORD dwService,78: 0, // DWORD dwFlags,79: 0 // DWORD dwContext80: );81: if(hConnect == NULL) {82: ulExcpArgu[0] = winet_InternetConnect;83: RaiseException(GetLastError(), 0, 1, ulExcpArgu);84: }85:86: hRequest = HttpOpenRequest(hConnect,87: _T("POST"),88: urlComponents.lpszUrlPath,89: NULL, NULL, NULL,90: INTERNET_FLAG_TRANSFER_ASCII| INTERNET_FLAG_NO_CACHE_WRITE| INTERNET_FLAG_RELOAD,91: 092: );93: if(hRequest == NULL) {94: ulExcpArgu[0] = winet_HttpOpenRequest;95: RaiseException(GetLastError(), 0, 1, ulExcpArgu);96: }97:98: INTERNET_BUFFERS inetBuf;99: DWORD dwBytesWritten;100:101: memset(&inetBuf, 0, sizeof(INTERNET_BUFFERS));102: inetBuf.dwStructSize = sizeof( INTERNET_BUFFERS ); // Must be set or error will occur103: inetBuf.Next = NULL;104: inetBuf.lpcszHeader = g_tcsHeader; // 请求头105: inetBuf.dwHeadersLength = _tcslen(inetBuf.lpcszHeader);106: inetBuf.dwBufferTotal = ulBufLen; // This is the only member used other than dwStructSize107:108: if(!HttpSendRequestEx( hRequest, &inetBuf, NULL, 0, 0)){109: ulExcpArgu[0] = winet_HttpSendRequestEx;110: RaiseException(GetLastError(), 0, 1, ulExcpArgu);111: }112:113:114: if(!InternetWriteFile( hRequest, lpDataBuf, ulBufLen, &dwBytesWritten)){115: ulExcpArgu[0] = winet_InternetWriteFile;116: RaiseException(GetLastError(), 0, 1, ulExcpArgu);117: }118:119: if(!HttpEndRequest(hRequest, NULL, 0, 0)){120: ulExcpArgu[0] = winet_HttpEndRequest;121: RaiseException(GetLastError(), 0, 1, ulExcpArgu);122: }123:124: char pcBuffer[512];125: DWORD dwBytesRead;126:127: do {128: dwBytesRead=0;129: if(InternetReadFile(hRequest, pcBuffer, 512-1, &dwBytesRead)) {130: pcBuffer[dwBytesRead] = 0;131: printf(pcBuffer);132: }133: }while(dwBytesRead>0);134:135: }__except(ExceptionHandler(GetExceptionCode(),GetExceptionInformation())) {136:137: }138: }__finally {139:140: if (hRequest)141: InternetCloseHandle(hRequest);142: if (hConnect)143: InternetCloseHandle(hConnect);144:145: if (hSession)146: InternetCloseHandle(hSession);147:148: UninitUrlComponents(&urlComponents);149: }
敦品厚德 格物致知
浙公网安备 33010602011771号