OSRelate.cpp
上传用户:hbzxgg2
上传日期:2009-03-28
资源大小:291k
文件大小:2k
- #include "StdAfx.h"
- #include "OSRelate.h"
- TCHAR* hwFormatMessage ( DWORD dwErrorCode )
- {
- static TCHAR szError[256] = {0};
- FormatMessage (
- FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- dwErrorCode,
- MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
- szError,
- LENGTH(szError),
- NULL);
- return szError;
- }
- //
- // 等待线程退出
- //
- BOOL WaitForThreadEnd ( HANDLE *phThread, DWORD dwWaitTime /*=5000*/ )
- {
- BOOL bRet = TRUE;
- ASSERT ( phThread );
- if ( !(*phThread) ) return TRUE;
- if ( ::WaitForSingleObject ( *phThread, dwWaitTime ) == WAIT_TIMEOUT )
- {
- bRet = FALSE;
- ::TerminateThread ( *phThread, 0 );
- }
- ::CloseHandle ( *phThread );
- (*phThread) = NULL;
- return bRet;
- }
- /********************************************************************************
- * Function Type : Global
- * Parameter : lpProgram - 存放当前执行程序所在的路径字符串的缓冲
- * dwSize - 缓冲大小
- * Return Value : 字符个数
- * Description : 得到当前执行程序所在的路径
- *********************************************************************************/
- int GetProgramDir(LPTSTR lpProgram,DWORD dwSize)
- {
- ASSERT ( lpProgram );
- ZeroMemory(lpProgram,dwSize);
- GetModuleFileName(AfxGetInstanceHandle(),(LPTSTR)lpProgram,dwSize);
- LPTSTR p = (LPTSTR)strrchr_s((LPCTSTR)lpProgram, _T('\'));
- if(!p) return 0;
- p[1] = _T(' ');
- int nLength = (int)(p - lpProgram)/sizeof(TCHAR);
- TCHAR szAppDir[MAX_PATH] = {0};
- strncpy_s ( szAppDir, AfxGetApp()->m_pszProfileName, MAX_PATH );
- p = (LPTSTR)strrchr_s((LPCTSTR)szAppDir, _T('\'));
- if(!p) return nLength;
- p[1] = _T(' ');
- int nLength1 = (int)(p - szAppDir)/sizeof(TCHAR);
- if ( stricmp_s ( szAppDir, lpProgram ) != 0 )
- {
- CString csMsg;
- csMsg.Format ( _T("Get program directory maybe error occurred.rn%srn%s"),
- lpProgram, szAppDir );
- strncpy_s ( lpProgram, szAppDir, dwSize );
- AfxMessageBox ( csMsg );
- return nLength1;
- }
- return nLength;
- }
- CString GetProgramDir()
- {
- TCHAR szProgram[MAX_PATH] = {0};
- GetProgramDir ( szProgram, sizeof(szProgram) );
- return szProgram;
- }