代码不长,通读代码,会有收获! 例如,将函数调用返回值用一个结构变量来保存。
头文件:
#ifndef _OS_H #define _OS_H #include "typ.h" #ifdef _DLL #define DECLSPEC_DLLEXPORT __declspec(dllexport) #else #define DECLSPEC_DLLEXPORT #endif #ifdef __cplusplus extern "C" { #endif #define ProcessState_OK 0 #define ProcessState_FAILURE 0x80000001 #define ProcessState_TIMEOUT 0x80000002 #define ProcessState_INVALID_PARAM 0x80000003 ///////////////////////////////////////////////////////////////// // thread functions ///////////////////////////////////////////////////////////////// typedef enum { MY_PRIORITY_LOWEST, MY_PRIORITY_LOW, MY_PRIORITY_NORMAL, MY_PRIORITY_HIGH, MY_PRIORITY_HIGHEST } MY_PRIORITY; // Creates a thread DECLSPEC_DLLEXPORT MY_HANDLE MY_ThreadCreate( char const *pThreadName, // name of the thread,This parameter may be NULL. MY_U32 (*pFunction)(void *), // A pointer to the function to be executed by the thread. void *pParam, // A pointer to a variable to be passed to the thread MY_U32 StackSize, // The initial size of the stack MY_PRIORITY Priority, // The priority value for the thread MY_BOOL IsSuspended // if IsSuspended is TRUE, the thread is created in a suspended state ); // Close the thread handle DECLSPEC_DLLEXPORT void MY_ThreadDelete( MY_HANDLE ThreadHandle ); // Blocks the calling thread until a thread terminates DECLSPEC_DLLEXPORT MY_BOOL MY_ThreadJoin( MY_HANDLE ThreadHandle, // A handle to the handle to be terminated. MY_U32 TimeoutInMS // The time-out interval, in milliseconds ); ///////////////////////////////////////////////////////////////// // Semaphore functions ///////////////////////////////////////////////////////////////// // Creates semaphore object DECLSPEC_DLLEXPORT MY_HANDLE MY_SemCreate( MY_U16 Count ); // Deletes semaphore object DECLSPEC_DLLEXPORT void MY_SemDelete( MY_HANDLE SemHandle ); // Increases the count of the specified semaphore object DECLSPEC_DLLEXPORT void MY_SemSignal( MY_HANDLE SemHandle ); // Waits until the semaphore object is in the signaled state. DECLSPEC_DLLEXPORT MY_BOOL MY_SemWait( MY_HANDLE SemHandle, MY_U32 TimeoutInMS ); ///////////////////////////////////////////////////////////////// // Mutex functions ///////////////////////////////////////////////////////////////// // Creates a mutex object DECLSPEC_DLLEXPORT MY_HANDLE MY_MutexCreate(); // Delete a mutex object DECLSPEC_DLLEXPORT void MY_MutexDelete( MY_HANDLE MutexHandle ); // Lock a mutex object DECLSPEC_DLLEXPORT void MY_MutexLock( MY_HANDLE MutexHandle ); // Release a mutex object DECLSPEC_DLLEXPORT void MY_MutexRelease( MY_HANDLE MutexHandle ); ///////////////////////////////////////////////////////////////// // Process functions ///////////////////////////////////////////////////////////////// // Create a process DECLSPEC_DLLEXPORT MY_HANDLE MY_CreateProcess(const char *CmdLine,const char *CurrentDirectory,MY_BOOL bShow); // Waitfor a process to exit DECLSPEC_DLLEXPORT MY_RESULT MY_WaitForProcessExit(MY_HANDLE handle,MY_U32 timeout); // Terminate a process DECLSPEC_DLLEXPORT void MY_TerminateProcess(MY_HANDLE handle); // Close Process DECLSPEC_DLLEXPORT void MY_CloseProcess(MY_HANDLE handle); ///////////////////////////////////////////////////////////////// // time functions ///////////////////////////////////////////////////////////////// // Suspends the execution of the current thread for at least the specified interval DECLSPEC_DLLEXPORT void MY_Sleep( MY_U32 TimeInMS ); // Retrieves the number of milliseconds that have elapsed since the system was started DECLSPEC_DLLEXPORT MY_U32 MY_TickCountTime(); DECLSPEC_DLLEXPORT MY_U32 MY_GetCurrTime(); DECLSPEC_DLLEXPORT void MY_SetCurrTime(MY_U32 t); ///////////////////////////////////////////////////////////////// // 键盘和鼠标functions ///////////////////////////////////////////////////////////////// #define LBUTTON_PRESS 0x01 // Left button #define MBUTTON_PRESS 0x02 // right button #define RBUTTON_PRESS 0x04 // middle button typedef void (*MY_KEYCALLBACK)(void *Context, MY_U32 KeyMsgCode,MY_U32 KeyValue ); typedef void (*MY_MOUSECALLBACK)(void *Context, MY_U32 MouseMsgCode,MY_S32 x, MY_S32 y); DECLSPEC_DLLEXPORT void MY_KeySetCallback( MY_KEYCALLBACK Callback,void *Context ); DECLSPEC_DLLEXPORT void MY_MouseSetCallback( MY_MOUSECALLBACK Callback,void *Context ); ///////////////////////////////////////////////////////////////// // system functions ///////////////////////////////////////////////////////////////// // Shutdown the system DECLSPEC_DLLEXPORT void MY_SystemShutdown( MY_U32 shutdownType ); ///////////////////////////////////////////////////////////////// // debug functions ///////////////////////////////////////////////////////////////// #define OUTPUT_DEBUG 3 #define OUTPUT_INFO 2 #define OUTPUT_ERROR 1 #define OUTPUT_ALWAYS 0 // Output debug string DECLSPEC_DLLEXPORT void MY_OutputDebugString( MY_S32 level, const char *pFormat, ... ); #ifdef __cplusplus } #endif #endif
源代码:
#include "os.h" #include <windows.h> #include <process.h> #include <time.h> #include "stdio.h" #pragma warning(disable:4996) #ifdef __cplusplus extern "C" { #endif #define SEM_MAX_COUNT 1500 typedef MY_U32 (*MY_THREADPROC)(void*); typedef struct MY_ThreadTag { void* pParam; MY_THREADPROC Function; HANDLE Handle; } MY_Thread; typedef struct MY_SemTag { HANDLE Handle; } MY_Sem; typedef struct MY_MutexTag { CRITICAL_SECTION Handle; } MY_Mutex; typedef struct MY_ProcessTag { PROCESS_INFORMATION pi; } MY_Process; static MY_U32 _output_level=0xffffffff; BOOL APIENTRY DllMain(HINSTANCE hMoudle, DWORD ul_reason_for_call, LPVOID lpReserved) { return TRUE; } ///////////////////////////////////////////////////////////////// // thread functions ///////////////////////////////////////////////////////////////// const int ThreadPriority[] = { THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL, THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST }; static unsigned __stdcall MY_ThreadFunction( void* pParam ) { unsigned rt=0; MY_Thread* pThread = (MY_Thread *)pParam; CoInitialize(NULL); rt = pThread->Function( pThread->pParam ); CoUninitialize(); return rt; } MY_HANDLE MY_ThreadCreate( char const *pThreadName, MY_U32 (*pFunction)(void *), void *pParam, MY_U32 StackSize, MY_PRIORITY Priority, MY_BOOL IsSuspended ) { unsigned ThreadID; MY_Thread *pThread = (MY_Thread *)malloc( sizeof( MY_Thread ) ); if( !pThread ) return NULL; pThread->Function = pFunction; pThread->pParam = pParam; pThread->Handle= (HANDLE)_beginthreadex( NULL, 0, &MY_ThreadFunction, pThread, CREATE_SUSPENDED, &ThreadID ); if( pThread->Handle == NULL ) { free( pThread ); return NULL; } SetThreadPriority( pThread->Handle, ThreadPriority[Priority] ); if( !IsSuspended ) ResumeThread( pThread->Handle ); return (MY_HANDLE)pThread; } void MY_ThreadDelete( MY_HANDLE ThreadHandle ) { if(ThreadHandle) { MY_Thread *pThread = (MY_Thread *)ThreadHandle; if(pThread->Handle) CloseHandle( pThread->Handle ); free( pThread ); } } MY_BOOL MY_ThreadJoin( MY_HANDLE ThreadHandle, MY_U32 TimeoutInMS ) { if(ThreadHandle) { MY_Thread *pThread = (MY_Thread *)ThreadHandle; DWORD ret=WaitForSingleObject(pThread->Handle, TimeoutInMS ); if( ret == WAIT_OBJECT_0 ) return MY_TRUE; } return MY_FALSE; } ///////////////////////////////////////////////////////////////// // Semaphore functions ///////////////////////////////////////////////////////////////// MY_HANDLE MY_SemCreate( MY_U16 Count ) { MY_Sem *pSem= (MY_Sem *)malloc( sizeof( MY_Sem ) ); if( !pSem ) return NULL; pSem->Handle = CreateSemaphore( NULL, Count, SEM_MAX_COUNT , NULL ); if( pSem->Handle == NULL ) { free( pSem ); return NULL; } return (MY_HANDLE)pSem; } void MY_SemDelete( MY_HANDLE SemHandle ) { MY_Sem *pSem = (MY_Sem *)SemHandle; if(pSem) { CloseHandle( pSem->Handle ); free( pSem ); } } void MY_SemSignal( MY_HANDLE SemHandle ) { MY_Sem *pSem = (MY_Sem *)SemHandle; if(pSem) ReleaseSemaphore( pSem->Handle , 1 , NULL ); } MY_BOOL MY_SemWait( MY_HANDLE SemHandle, MY_U32 TimeoutInMS ) { MY_Sem *pSem = (MY_Sem *)SemHandle; if(pSem) { if( WaitForSingleObject( pSem->Handle, TimeoutInMS ) == WAIT_TIMEOUT ) return MY_FALSE; } return MY_TRUE; } ///////////////////////////////////////////////////////////////// // Mutex functions ///////////////////////////////////////////////////////////////// MY_HANDLE MY_MutexCreate() { MY_Mutex* pMutex = (MY_Mutex*)malloc( sizeof( MY_Mutex ) ); if( !pMutex ) return NULL; InitializeCriticalSection( &( pMutex->Handle ) ); return pMutex; } void MY_MutexDelete( MY_HANDLE MutexHandle ) { MY_Mutex* pMutex = (MY_Mutex*)MutexHandle; if(pMutex) { DeleteCriticalSection( &(pMutex->Handle) ); free( pMutex ); } } void MY_MutexLock( MY_HANDLE MutexHandle ) { MY_Mutex* pMutex = (MY_Mutex*)MutexHandle; if(pMutex) EnterCriticalSection( &( pMutex->Handle ) ); } void MY_MutexRelease( MY_HANDLE MutexHandle ) { MY_Mutex* pMutex = (MY_Mutex*)MutexHandle; if(pMutex) LeaveCriticalSection( &( pMutex->Handle ) ); } ///////////////////////////////////////////////////////////////// // time functions ///////////////////////////////////////////////////////////////// MY_U32 MY_TickCountTime() { return GetTickCount(); } MY_U32 MY_GetCurrTime() { struct tm ptm; SYSTEMTIME ltime; GetLocalTime(<ime); ptm.tm_year = ltime.wYear - 1900; ptm.tm_mon = ltime.wMonth - 1; ptm.tm_mday = ltime.wDay; ptm.tm_hour = ltime.wHour; ptm.tm_min = ltime.wMinute; ptm.tm_sec = ltime.wSecond; ptm.tm_isdst = -1; return (MY_U32)mktime(&ptm); } void MY_SetCurrTime(MY_U32 t) { struct tm * stm = gmtime((time_t *)&t); SYSTEMTIME systime; systime.wYear = stm->tm_year+1900; systime.wMonth = stm->tm_mon+1; systime.wDay = stm->tm_mday; systime.wHour = stm->tm_hour; systime.wMinute = stm->tm_min; systime.wSecond = stm->tm_sec; systime.wMilliseconds = 0; if(systime.wYear <= 1980 || systime.wYear >= 2099) return; SetLocalTime(&systime); } void MY_Sleep( MY_U32 TimeInMS ) { Sleep( TimeInMS ); } ///////////////////////////////////////////////////////////////// // Process functions ///////////////////////////////////////////////////////////////// MY_HANDLE MY_CreateProcess(const char *CmdLine,const char *CurrentDirectory,MY_BOOL bShow) { STARTUPINFO si; PROCESS_INFORMATION pi; MY_Process * Result=NULL; char cmd[MAX_PATH]={0}; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); si.dwFlags = STARTF_USESHOWWINDOW; if(bShow) si.wShowWindow = SW_SHOW; else si.wShowWindow = SW_HIDE; if(strlen(CmdLine)+1<MAX_PATH) strcpy(cmd,CmdLine); ZeroMemory( π, sizeof(pi) ); if( !CreateProcess( 0, cmd, // Command line. NULL, // Process handle not inheritable. NULL, // Thread handle not inheritable. FALSE, // Set handle inheritance to FALSE. 0, // No creation flags. NULL, // Use parent's environment block. CurrentDirectory, // &si, // Pointer to STARTUPINFO structure. π ) // Pointer to PROCESMY_INFORMATION structure. ) { return NULL; } Result = (MY_Process *)malloc(sizeof(MY_Process)); if(Result) Result->pi = pi; return Result; } MY_RESULT MY_WaitForProcessExit(MY_HANDLE handle,MY_U32 timeout) { MY_Process * pProcess = (MY_Process *)handle; if(!pProcess) return ProcessState_INVALID_PARAM; switch(WaitForSingleObject( pProcess->pi.hProcess, timeout)) { case WAIT_OBJECT_0: return ProcessState_OK; case WAIT_TIMEOUT: return ProcessState_TIMEOUT; default: return ProcessState_OK; } } void MY_TerminateProcess(MY_HANDLE handle) { MY_Process * pProcess = (MY_Process *)handle; if(pProcess) { TerminateProcess(pProcess->pi.hProcess,0); free(pProcess); } } void MY_CloseProcess(MY_HANDLE handle) { MY_Process * pProcess = (MY_Process *)handle; if(pProcess) { CloseHandle( pProcess->pi.hProcess ); CloseHandle( pProcess->pi.hThread ); free(pProcess); } } void MY_OutputDebugString( MY_S32 level, const char *pFormat, ... ) { MY_U32 l = 1<<level; if(l&_output_level) { va_list list; va_start(list, pFormat ); char buffer[1024]; vsprintf(buffer,pFormat,list); //OutputDebugString(buffer); printf("%s",buffer); va_end(list); } } #ifdef __cplusplus } #endif