Ascii和Unicode的互轉
- //-------------------------------------------------------------------------------------
- //Description:
- // This function maps a character string to a wide-character (Unicode) string
- //
- //Parameters:
- // lpcszStr: [in] Pointer to the character string to be converted
- // lpwszStr: [out] Pointer to a buffer that receives the translated string.
- // dwSize: [in] Size of the buffer
- //
- //Return Values:
- // TRUE: Succeed
- // FALSE: Failed
- //
- //Example:
- // MByteToWChar(szA,szW,sizeof(szW)/sizeof(szW[0]));
- //---------------------------------------------------------------------------------------
- BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize)
- {
- // Get the required size of the buffer that receives the Unicode
- // string.
- DWORD dwMinSize;
- dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, NULL, 0);
- if(dwSize < dwMinSize)
- {
- return FALSE;
- }
- // Convert headers from ASCII to Unicode.
- MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize);
- return TRUE;
- }
- //-------------------------------------------------------------------------------------
- //Description:
- // This function maps a wide-character string to a new character string
- //
- //Parameters:
- // lpcwszStr: [in] Pointer to the character string to be converted
- // lpszStr: [out] Pointer to a buffer that receives the translated string.
- // dwSize: [in] Size of the buffer
- //
- //Return Values:
- // TRUE: Succeed
- // FALSE: Failed
- //
- //Example:
- // MByteToWChar(szW,szA,sizeof(szA)/sizeof(szA[0]));
- //---------------------------------------------------------------------------------------
- BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)
- {
- DWORD dwMinSize;
- dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);
- if(dwSize < dwMinSize)
- {
- return FALSE;
- }
- WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE);
- return TRUE;
- }
//讀取程式設定
SetRegistryKey(TEXT("本地程式"));//PsMonitor
OpCom::dwWaitTime = GetProfileInt(TEXT("Setting"), TEXT("參數"), 預設值);
OpCom::sExePath = GetProfileString(TEXT("Setting"), TEXT("預設值"));
//隻運作一個執行個體
HANDLE hMutex;
hMutex=CreateMutex(NULL,TRUE,TEXT("MutexName")); //MutexName使用你自己的名字
if(hMutex)
{
if(ERROR_ALREADY_EXISTS == GetLastError())
{
return FALSE;
}
}
//啟動隐藏主視窗和工作列
ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW); //隐藏工作列
WINDOWPLACEMENT wp;
wp.length=sizeof(WINDOWPLACEMENT);
//GetWindowPlacement(&wp);
wp.flags=WPF_RESTORETOMAXIMIZED;
wp.showCmd=SW_HIDE;
SetWindowPlacement(&wp);
//設定自動運作
#define EP_REG_AUTORUN TEXT("SOFTWARE//Microsoft//Windows//CurrentVersion//Run")
#define EP_REG_AUTORUN_NAME TEXT("運作項名字")
TCHAR sPath[MAX_PATH];
GetModuleFileName(NULL, sPath, sizeof(sPath));
OpCom::SetRegValue(HKEY_LOCAL_MACHINE, EP_REG_AUTORUN, EP_REG_AUTORUN_NAME, sPath);
//查詢系統資料庫string
BOOL OpCom::QueryRegValue(HKEY hKeyParent,
LPCTSTR sRegKey,
LPCTSTR sKeyName,
LPTSTR sValue,
ULONG iMaxLength,
LPCTSTR sDefaultValue)
{
CRegKey regKey;
if(ERROR_SUCCESS != regKey.Open(hKeyParent,sRegKey))
{
if(lstrcpyn(sValue, sDefaultValue, static_cast<int>(iMaxLength)))
return TRUE;
return FALSE;
}
ULONG iMaxLength2 = iMaxLength;
if(ERROR_SUCCESS != regKey.QueryStringValue(sKeyName,sValue,&iMaxLength2))
{
regKey.Close();
if (sDefaultValue)
{
if(lstrcpyn(sValue, sDefaultValue, static_cast<int>(iMaxLength)))
return TRUE;
}
else
{
sValue[0] = TEXT('/0');
}
return FALSE;
}
regKey.Close();
return TRUE;
}
//查詢系統資料庫DWORD
BOOL OpCom::QueryRegDwordValue(HKEY hKeyParent,
LPCTSTR sRegKey,
LPCTSTR sKeyName,
DWORD &dwValue,
DWORD dwDefaultValue)
{
CRegKey regKey;
if(ERROR_SUCCESS != regKey.Open(hKeyParent,sRegKey))
{
dwValue = dwDefaultValue;
return FALSE;
}
if(ERROR_SUCCESS != regKey.QueryDWORDValue(sKeyName,dwValue))
{
dwValue = dwDefaultValue;
regKey.Close();
return FALSE;
}
regKey.Close();
return TRUE;
}
//修改系統資料庫string
BOOL OpCom::SetRegValue(HKEY hKeyParent,
LPCTSTR sRegKey,
LPCTSTR sKeyName,
LPCTSTR sValue)
{
CRegKey regKey;
if(ERROR_SUCCESS != regKey.Open(hKeyParent,sRegKey))
{
if(ERROR_SUCCESS != regKey.Create(hKeyParent,sRegKey))
return FALSE;
}
if(ERROR_SUCCESS != regKey.SetStringValue(sKeyName,sValue))
{
regKey.Close();
return FALSE;
}
regKey.Close();
return TRUE;
}
//修改系統資料庫DWORD
BOOL OpCom::SetRegDwordValue(HKEY hKeyParent,
LPCTSTR sRegKey,
LPCTSTR sKeyName,
DWORD dwValue)
{
CRegKey regKey;
if(ERROR_SUCCESS != regKey.Open(hKeyParent,sRegKey))
{
if(ERROR_SUCCESS != regKey.Create(hKeyParent,sRegKey))
return FALSE;
}
if(ERROR_SUCCESS != regKey.SetDWORDValue(sKeyName,dwValue))
{
regKey.Close();
return FALSE;
}
regKey.Close();
return TRUE;
}
//檔案是否存在
BOOL OpCom::FileExist(LPCTSTR sFilePath)
{
//PathFileExists( sFilePath )
WIN32_FIND_DATA w32fd;
HANDLE hFile=FindFirstFile(sFilePath,&w32fd);
if(hFile!=INVALID_HANDLE_VALUE)
{
FindClose(hFile);
return TRUE;
}
return FALSE;
}
//建立一個非模态對話框
//添加一個對話框資源,并添加對話框類class CProcDlg:CDialog;
//頭檔案
CProcDlg *m_pProcDlg;
//CPP檔案
//初始化 m_pProcDlg(NULL)
//顯示對話框
if(!m_pProcDlg)
{
m_pProcDlg = new CProcDlg(this);
m_pProcDlg->Create(CProcDlg::IDD, this);
}
m_pProcDlg->ShowWindow(SW_SHOW);
//關閉對話框
if(m_pProcDlg)
{
//m_pProcDlg->CloseWindow();
m_pProcDlg->DestroyWindow();
delete m_pProcDlg;
m_pProcDlg=NULL;
}
//當主視窗隐藏時,顯示子對話框的方法
CProcDlg::CProcDlg(CPsMonitorDlg* pParent )
: CDialog(CProcDlg::IDD, (CWnd *)NULL) //注意:這行的pParent被修改為NULL了
//設定視窗置頂
::SetWindowPos(this->m_hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
//設定視窗螢幕居中
CenterWindow(GetDesktopWindow());
//時間差類 CTimeSpan
CTimeSpan m_tsYysj
m_tsYysj.Format("%D天 %H:%M:%S");
//增加1秒
CTimeSpan ts(1); // 1 seconds
m_tsYysj += ts;