📄 stafutil.cpp
字号:
void STAFUtilFreeSystemMemory(void *ptr){ GlobalFree(ptr);}unsigned int STAFUtilIsValidSocket(STAFSocket_t theSocket){ return (theSocket != INVALID_SOCKET);}unsigned int STAFUtilGetNonInheritableSocket(STAFSocket_t oldSocket, STAFSocket_t *newSocket, unsigned int *osRC){ HANDLE newHandle; int dupHandleRC = DuplicateHandle( GetCurrentProcess(), (HANDLE)oldSocket, GetCurrentProcess(), &newHandle, 0, FALSE, // The new handle cannot be inherited DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS); if (dupHandleRC) { *newSocket = (SOCKET)newHandle; return kSTAFOk; // To indicate success } else { // DuplicateHandle failed *osRC = static_cast<unsigned int>(GetLastError()); *newSocket = oldSocket; return 1; // To indicate failure }}unsigned int STAFUtilWin32CreateNullSD(PSECURITY_DESCRIPTOR *sd){ // If on WinNT or above, set up security to use on a file mapping to // allow access to everyone. if (STAFUtilWin32GetWinType() & kSTAFWinNTPlus) { // Allocate the Security Descriptor (SD) if (!(*sd = static_cast<PSECURITY_DESCRIPTOR>( new char[SECURITY_DESCRIPTOR_MIN_LENGTH]))) return 1; if (!InitializeSecurityDescriptor(*sd, SECURITY_DESCRIPTOR_REVISION)) return 2; // Set NULL DACL on the Security Descriptor if (!SetSecurityDescriptorDacl(*sd, TRUE, (PACL)NULL, FALSE)) return 3; } return 0;}unsigned int STAFUtilWin32DeleteNullSD(PSECURITY_DESCRIPTOR *sd){ // If on WinNT or above, delete the Security Descriptor if (STAFUtilWin32GetWinType() & kSTAFWinNTPlus) { if (*sd != 0) { delete [] static_cast<char *>(*sd); *sd = 0; } } return 0;}// The Win32 API does not contain a functional equivalent of the standard C// mkstemp() function. The Microsoft C runtime implementation does not even// provide support for the function, although it does provide an// implementation of mktemp(). However, it is strongly advised not to use// the mktemp() function on either Unix or Windows.//// The Win32 API does provide a function, GetTempFileName(), that will// generate a temporary filename, but that is all that it does; it does not// open the file for you. So, we are not using this function.// Instead of using GetTempPath() to obtain the current user's setting for the// location to place temporary files, the location to store temporary files is // being passed in (and should be *gSTAFWriteLocationPtr/tmp), the STAF data// directory).// // The STAFUtilCreateTempFile() method generates our own random filename, // using the system time and request number to seed the rand() function.// The format of the unique filename is: // <tempDir>\STAFTemp??????[.<suffix>]// where <tempDir> contains the name of the directory to store the temp file// ?????? represents 6 randomly generated characters.// Also, if a suffix is specified, e.g. "out", it is added as the file// extension.// We attempt to create the file. If not successful, we repeat the process,// generating another random temporary filename and attempting to create the// file until the file is successfully created or some kind of fatal error// occurs.STAFRC_t STAFUtilCreateTempFile(STAFStringConst_t tempDir, STAFStringConst_t tempFileSuffix, unsigned int requestNumber, STAFString_t *tempFileName, STAFString_t *errorBuffer, unsigned int *osRC){ static char fileNameChars[37] = "0123456789ABCDEFGHIJKLMNOPQRSTUVYXWZ"; const unsigned int BUFSIZE = 4096; // Maximum buffer size char tmpPathBuffer[BUFSIZE] = { 0 }; // Buffer for temp path name char tmpFileBuffer[MAX_PATH] = { 0 }; // Buffer for temp file name char randomBuffer[7] = { 0 }; // Buffer for random values unsigned int characterRange = lstrlen(fileNameChars) - 1; char cCharacter; unsigned int i; HANDLE hFile; // Temporary file's handle // Create a unique temporary file name with format: // <tempDir>\STAFTemp??????[.<suffix>] // where <tempDir> contains the name of the directory to store the // temporary file and ?????? is a randomly assigned unique value. // Also, if a suffix is specified, e.g. out, it is added as the file // extension. STAFString tempNamePrefix = STAFString(tempDir) + STAFString(kUTF8_BSLASH) + "STAFTemp"; STAFString suffix = STAFString(tempFileSuffix); // Note: Using the only the time function is not enough to set the // random seed because on Windows, it would set the same seed for all // temporary files created within the same second. We also use the // request number since it is unique per request on each machine. srand((unsigned)(time(NULL) * requestNumber)); // Repeat the process of generating a random temporary file name and // attempting to create the file, until the file is successfully created // or some kind of fatal error occurs. do { // Add 6 randomly assigned values to the temporary file name for (i = 0; i < 6; i++) { cCharacter = fileNameChars[rand() % characterRange]; randomBuffer[i] = cCharacter; } randomBuffer[i] = 0; STAFString tempNameString = tempNamePrefix + STAFString(randomBuffer); // If a suffix was specified, add it to the temporary file name if (suffix.length() > 0) { tempNameString = tempNameString + STAFString(".") + suffix; } // Attempt to create the file to make sure doesn't already exist strncpy(tmpPathBuffer, tempNameString.toCurrentCodePage()->buffer(), sizeof(tmpPathBuffer)); hFile = CreateFile(tempNameString.toCurrentCodePage()->buffer(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY, 0); if ((hFile == INVALID_HANDLE_VALUE) && (GetLastError() != ERROR_FILE_EXISTS)) { // Fatal error - Don't retry *errorBuffer = STAFString("Creating temp file failed").adoptImpl(); *osRC = GetLastError(); return kSTAFBaseOSError; } } while (hFile == INVALID_HANDLE_VALUE); // Temporary file was successfully created and opened. Close it. CloseHandle(hFile); *tempFileName = STAFString(tmpPathBuffer).adoptImpl(); return kSTAFOk;}/*******************************************************************************//* STAFUtilWin32LookupSystemErrorMessage - This function looks up the error *//* message for the specified Windows system error code. *//* *//* Accepts: (IN) System error code *//* (OUT) Error message *//* *//* Returns: Nothing *//*******************************************************************************/void STAFUtilWin32LookupSystemErrorMessage(unsigned int errorCode, STAFString_t *errorMsg) { LPVOID lpMsgBuffer; if (FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuffer, 0, NULL)) { if (errorMsg) { *errorMsg = (STAFString( reinterpret_cast<char *>(lpMsgBuffer))).adoptImpl(); } } LocalFree(lpMsgBuffer);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -