bool CTcpSocket::Open() { if (m_nFd != -1) { AILOG_ERROR() << "socket has been opened."; return false; } m_nFd = socket(AF_INET, SOCK_STREAM, 0); if (m_nFd <= 0) { AILOG_ERROR() << "create socket error:" << LastErrorMessage(); return false; } int nReuseAddr = 1; if (setsockopt(m_nFd, SOL_SOCKET, SO_REUSEADDR, (char *)&nReuseAddr, sizeof(nReuseAddr)) != 0) { AILOG_ERROR() << "setsockopt error:" << LastErrorMessage(); return false; } return true; }
void createDialog(void) { STARTUPINFOA startinfo; WNDCLASSEXA WndClass; g_hInstance = GetModuleHandle(NULL); GetStartupInfoA(&startinfo); WndClass.cbSize = sizeof WndClass; WndClass.style = 0; WndClass.lpfnWndProc = WndProc; WndClass.cbClsExtra = 0; WndClass.cbWndExtra = 0; WndClass.hInstance = g_hInstance; WndClass.hIcon = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON)); WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); WndClass.lpszMenuName = MAKEINTRESOURCEA(IDM_MAINMENU); WndClass.lpszClassName = "DrMingw"; WndClass.hIconSm = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_MAINICON)); if (!RegisterClassExA(&WndClass)) { ErrorMessageBox("RegisterClassEx: %s", LastErrorMessage()); exit(EXIT_FAILURE); } g_hWnd = CreateWindowExA( WS_EX_CLIENTEDGE, WndClass.lpszClassName, "Dr. Mingw", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInstance, NULL ); if (g_hWnd == NULL) { ErrorMessageBox("CreateWindowEx: %s", LastErrorMessage()); exit(EXIT_FAILURE); } ShowWindow(g_hWnd, (startinfo.dwFlags & STARTF_USESHOWWINDOW) ? startinfo.wShowWindow : SW_SHOWDEFAULT); UpdateWindow(g_hWnd); }
//--------------------------------------------------------------------------- AnsiString ResStr(UINT resID) { char buff[1024] = ""; __TRY int rs = LoadString( NULL, resID, buff, 1024); if (!rs) LastErrorMessage("Load Resource String error"); __CATCH return AnsiString(buff); }
bool File::Exists( const string& filename ) { const char *cName; cName = filename.c_str(); #ifdef USE_PHYSICSFS if ( !PHYSFS_exists( cName ) ){ LogMsg(ERR, "%s: %s.", LastErrorMessage().c_str(), cName); return false; } #else struct stat fileStatus; int stat_ret = stat(cName, &fileStatus ); if ( stat_ret != 0 ) { LogMsg(ERR,"%s: %s.", LastErrorMessage().c_str(), cName); break; return false; } #endif return true; }
//--------------------------------------------------------------------------- bool __fastcall DosCmdExec(LPCSTR lpCmdLine) { PROCESS_INFORMATION ProcInf; STARTUPINFO StInf = { sizeof(STARTUPINFO), 0,0,"Hello", 0,0,100,100,0,0,0, STARTF_USESHOWWINDOW, SW_HIDE, 0,0,0,0,0 }; BOOL ErrCode = CreateProcess( NULL, (char*)lpCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &StInf, &ProcInf ); if (!ErrCode) { LastErrorMessage("CreateProcess"); return false; } HANDLE hP = ProcInf.hProcess; DWORD ExCode; do { ErrCode = GetExitCodeProcess( hP, &ExCode); } while (ExCode == STILL_ACTIVE); if (!ErrCode) { LastErrorMessage("GetExitCodeProcess"); return false; } return true; }
bool CTcpSocket::Listen(int n) { struct sockaddr_in nAddress; if (!GetAddress(m_strHost, m_nPort, nAddress)) return false; if (bind(m_nFd, (struct sockaddr*)&nAddress, sizeof(nAddress)) != 0) { AILOG_ERROR() << "Address=" << inet_ntoa(nAddress.sin_addr) << ":" << ntohs(nAddress.sin_port); AILOG_ERROR() << AddressToString() << " bind socket error:" << LastErrorMessage(); return false; } return listen(m_nFd, n) != -1; }
static void debugThread(void *arg) { DWORD dwProcessId = (DWORD)(UINT_PTR)arg; // attach debuggee if (!DebugActiveProcess(dwProcessId)) { ErrorMessageBox("DebugActiveProcess: %s", LastErrorMessage()); return; } setDumpCallback(appendText); SetSymOptions(debug_options.debug_flag); DebugMainLoop(&debug_options); }
//--------------------------------------------------------------------------- void __fastcall TWaitDataThread::Execute() { __TRY HANDLE hMapFile = NULL; LPVOID lpMapAddress = NULL; while (!hMapFile) { hMapFile = OpenFileMapping(FILE_MAP_READ, // read permission FALSE, // Do not inherit the name FILE_MAP_OBJ_TEXT); // of the mapping object. } /* if (!hMapFile) { LastErrorMessage("Could not open file mapping object."); throw Exception("hMapFile == NULL"); } */ lpMapAddress = MapViewOfFile(hMapFile, // handle to mapping object FILE_MAP_READ, // read permission 0, // max. object size 0, // size of hFile 0); // map entire file if (!lpMapAddress) { //ErrorHandler("Could not map view of file."); LastErrorMessage("Could not map view of file."); throw Exception("lpMapAddress == NULL"); } FirstByteOfData = (TInOutData*)lpMapAddress; long i = 0; while (!Terminated) { if (FirstByteOfData->lNum != i) { ::EnterCriticalSection(&m_AccessCriticalSection); i = FirstByteOfData->lNum; _size = FirstByteOfData->lSize; memcpy( _data, FirstByteOfData->ch, _size); ::LeaveCriticalSection(&m_AccessCriticalSection); Synchronize(UpdateCaption); } } __CATCH }
/**Seek to new position in the file. * \param pos Position in bytes form the beginning of the file. * \return true if successful, false otherwise.*/ bool File::Seek( long pos ){ int retval; if ( fp == NULL ) return false; #ifdef USE_PHYSICSFS retval = PHYSFS_seek( fp, static_cast<PHYSFS_uint64>( pos )); #else const char *cName; cName = validName.c_str(); retval = fseek(fp, pos, SEEK_SET); #endif if ( retval == 0 ){ LogMsg(ERR,"%s: Error using file seek [%ld]. %s", validName.c_str(), pos, LastErrorMessage().c_str() ); return false; } return true; }
/**Writes buffer to file. * \param buffer The buffer to write * \param bufsize The size of the buffer in bytes * \return true if successful, false otherwise. */ bool File::Write( char *buffer, const long bufsize ){ if ( fp == NULL ) return false; #ifdef USE_PHYSICSFS PHYSFS_sint64 bytesWritten = PHYSFS_write(this->fp, buffer, sizeof(char), bufsize); #else long bytesWritten = fwrite(buffer,1,bufsize,fp); #endif if ( bytesWritten != bufsize ){ LogMsg(ERR,"Unable to write to file '%s'. (%d/%d bytes written) %s", this->validName.c_str(), bytesWritten, bufsize, LastErrorMessage().c_str() ); return false; } return true; }
/**Reads the whole file into a buffer. Buffer will be automatically allocated * for you, but you must explicitly free it by using "delete [] buffer" * \return Pointer to buffer, NULL otherwise.*/ char *File::Read( void ){ if ( fp == NULL ) return NULL; // Seek to beginning Seek( 0 ); char *fBuffer = new char[static_cast<Uint32>(contentSize)]; long bytesRead = static_cast<long>( #ifdef USE_PHYSICSFS PHYSFS_read( this->fp, fBuffer, 1, contentSize ) #else fread(fBuffer,1,contentSize,fp) #endif ); if( bytesRead == contentSize){ return fBuffer; } else { delete [] fBuffer; LogMsg(ERR,"%s: Unable to read file into memory. %s", validName.c_str(), LastErrorMessage().c_str() ); return NULL; } }