[C++]傳遞Class Member function pointer至其他Class

困擾已久的傳遞Class的Member function到其他class的問題,在今天仔細參詳了半天高手的code以後,終於把之前的疑惑解決了。

原本都知道要用第三個class來處理這問題,但由於一直搞不懂第三個要怎麼做,所以就一直寫不出來。

目前參考的方法是使用一個Base class來當做傳遞Function pointer的介面,然後一個Template class來讓內層使用的class來使用function pointer,也避開兩個互相參照的class的交互參照之問題。

處理方式如下:
Class Processer (底下稱CP)有工作要麻煩 Class Thread (底下稱CT)處理,但是為了讓Main thread不要卡住,所以CT是另外開一個Thread來處理CP要求的工作。
然而當CT在處理工作時如遇到了例外,並無法立即讓CP知道目前的狀況,所以就需要讓CP開個管道(Function Pointer)來讓CT可以在遇到問題時,回來告知CP。

原本是打算用PostMessage()來達成這樣的需求,但是CT所在的dll可能會移植到別的平台上,所以如果使用PostMessage()來做的話,那麼在移植上,還需要去針對不同平台處理。

但是此時的需求只是要把CT遇到的狀況告知給CP,並由CP來告知使用者而已,所以不會造成CT的程序block,所以這邊就採用Class的 callback方式處理。(如果會發生blocking的話,那還是只能用Windows Message的方式處理會比較好處理,不然Thread也是可以。)

底下範例使用
Class CallBackBase:
class CCallBackBase
{
    public:
        CCallBackBase(){};
        ~CCallBackBase(){};

        virtual bool Execute(void *Param) const = 0;
};




Template Class:
template < class cTarClass >
class CTestCallBack : public CCallBackBase
{
    public:
        typedef bool (cTarClass::*LPFUNC)(void *Param);

    protected:
        cTarClass *m_pTarClass;
        LPFUNC m_pTarFunc;

    public:
        CTestCallBack() {
            m_pTarClass = NULL;
        };
        ~CTestCallBack() {};

        void SetCallBack(cTarClass *cTarClassPtr, LPFUNC pFuncPtr){
            m_pTarClass = cTarClassPtr;
            m_pTarFunc = pFuncPtr;
        };

        virtual bool Execute(void *Param) const        //Execute的參數要與function pointer一致是因為操作者是使用此介面來使用function pointer
        {
            if (m_pTarClass)
            {
                return (m_pTarClass->*m_pTarFunc)(Param);
            }else{
                ::printf("ERROR: The call back funciton is not set!!");
            }
            return false;
        };
};


Class Thread:
class CProcessThread
{
    protected:    
        CCallBackBase *m_pCallBack;
        HANDLE m_hThread;

    public:
        CProcessThread(){
            m_pCallBack = NULL;
            m_hThread = NULL;
        };

        ~CProcessThread(){
            if ( m_hThread != NULL )    {
                DWORD dwExitCode = 0;
                ::TerminateThread(this->m_hThread, dwExitCode);

                HANDLE hThread = this->m_hThread;
                this->m_hThread = NULL;
                ::CloseHandle(hThread);
            }
        };

        void StartProcessThread(void)
        {
            this->m_hThread = CreateThread( NULL, 0, ProcessThread, (LPVOID)this, 0, NULL);
        };

        void SetCallBack(CCallBackBase *pCallBack){
            m_pCallBack = pCallBack;
        };

    protected:
        static DWORD WINAPI ProcessThread(LPVOID lpParam)
        {
            CProcessThread *pcmxProcessThread = (CProcessThread*) lpParam;

            return pcmxProcessThread->ProcessFunc();
        };

        DWORD ProcessFunc(void)
        {
            //Set callback data to upper class
            m_pCallBack->Execute((void*) "This message is pass from ProcessThread::ProcessFunc() in Thread!!\n");

            if ( m_hThread != NULL )    {
                DWORD dwExitCode = 0;
                ::TerminateThread(this->m_hThread, dwExitCode);

                HANDLE hThread = this->m_hThread;
                this->m_hThread = NULL;
                
                ::CloseHandle(hThread);
            }

            return 0;
        };
};



Class Process:
class CProcess
{
    protected:
        CTestCallBack<CProcess> m_CallBack;
        CProcessThread m_ProcessThread;

    public:
        CProcess() {
            m_CallBack.SetCallBack(this, &CProcess::CallBackShower);
            m_ProcessThread.SetCallBack(&m_CallBack);
        };
        ~CProcess() {};

        void StartProcess(void)
        {
            m_ProcessThread.StartProcessThread();
        };

        bool CallBackShower(void *Param)
        {
            char szMsg[512];
            strcpy(szMsg, (char*)Param);

            ::printf("The Pass message from call back is \nMsg = %s", szMsg);

            return true;
        };

};



Main program:
int _tmain(int argc, char* argv[])
{
    CProcess Process;

    Process.StartProcess();

    system("pause");
    return 0;
}


執行結果:



程式碼亂跑的問題再另外找解@@

[WINAPI]取得磁碟空間

可使用GetDiskFreeSpaceEx()來取得目前可使用的磁碟空間、總磁碟空間以及總剩餘磁碟空間。

使用方式如下:


    char szDiskDir[MAX_PATH]  = "C:\\";

    //A pointer to a variable that receives the total number of free bytes on a disk that are available to the user who is associated with the calling thread.
    //This parameter can be NULL.
    //If per-user quotas are being used, this value may be less than the total number of free bytes on a disk.
    __int64 n64AvailableFreeDiskBytes;       

    //A pointer to a variable that receives the total number of bytes on a disk that are available to the user who is associated with the calling thread.
    //This parameter can be NULL.
    //If per-user quotas are being used, this value may be less than the total number of bytes on a disk.
    //To determine the total number of bytes on a disk or volume, use IOCTL_DISK_GET_LENGTH_INFO.        
    __int64 n64TotalDiskBytes;

    //A pointer to a variable that receives the total number of free bytes on a disk.
    //This parameter can be NULL.
    __int64 n64TotalFreeDiskBytes;

    if ( !::GetDiskFreeSpaceEx(szDiskDir, (PULARGE_INTEGER) &n64AvailableFreeDiskBytes, (PULARGE_INTEGER) &n64TotalDiskBytes,
                                                            (PULARGE_INTEGER) &n64TotalFreeDiskBytes) )
    {
        printf("Fail to get disk fress space in %s (Err = %d).\n", szDiskDir, ::GetLastError());
    }else{
        printf("%s has %lld bytes available free disk.\nTotal disk size is %lld bytes.\nTotal free space is %lld bytes.\n", 
                    szDiskDir, n64AvailableFreeDiskBytes, n64TotalDiskBytes, n64TotalFreeDiskBytes);
    }

[CListCtrl]排序Contorl List內之資料

這邊以檔案清單來說明,需要的欄位有:檔案名稱、副檔名、檔案路徑。

首先先建立用來排序用的資料結構

typedef struct {
    char pszFileName[_MAX_FNAME];
    char pszExtName[_MAX_EXT];
    char pszFilePath[MAX_PATH];
} ITEMDATA, *PITEMDATA; 


再來建立給CListCtrl::SortItems()使用的Call back函式,函式內部就放需要用來sort比對的方法
Head File

friend int CALLBACK SortFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);

Cpp File

int CALLBACK SortFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
    int nRetVal = 0;

    PITEMDATA pData1 = (PITEMDATA)lParam1;
    PITEMDATA pData2 = (PITEMDATA)lParam2;

    switch(lParamSort)
    {
    case 0:    // File Name
        nRetVal = strcmp(pData1->pszFileName, pData2->pszFileName);
        break;

    case 1:    // Extension File Name
        nRetVal = strcmp(pData1->pszExtName, pData2->pszExtName);
        break;

    case 2: // TerFile Path
        nRetVal = strcmp(pData1->pszFilePath, pData2->pszFilePath);
        break;

    default:
        break;
    }

    return nRetVal;
}


再來是建立接收到滑鼠事件後需要執行的函式,在函式內就把先前建立好的Call back函式,SortFunc塞給CListCtrl::SortItems()。
Head File

afx_msg void OnItemClick2Sort(NMHDR* pNMHDR, LRESULT* pResult);

Cpp File

void CFileSearchDlg::OnItemClick2Sort(NMHDR* pNMHDR, LRESULT* pResult)
{
    NMLISTVIEW *pLV = (NMLISTVIEW *) pNMHDR;
    
    m_lstFileItem.SortItems(SortFunc, pLV->iSubItem);

    *pResult = 0;
}



然後在message map鍵入

ON_NOTIFY(LVN_COLUMNCLICK, IDC_LST_FILE, OnItemClick2Sort)

來接收滑鼠點選List的欄位標題時的訊息。

以上動作完成後,就可以利用點擊欄位標題來進行排序了。

[WINDOWS]延遲載入的特定服務的方式

根據MS技術支援的文件內顯示:
1.先至服務的登陸子機碼,HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ < 服務名稱 >
2.在此子機碼內新增 DependOnService 值(可擴充字串值)
3. 鍵入與此服務之前啟動的服務名稱
4.之後在電腦啟動時,它會使用這個項目,來確認啟動服務或服務列在這個值,再嘗試啟動相依的服務。

測試平台:Win7 x64
測試結果:有效

Ref:延遲載入的特定服務的方式

[MFC]設定RichEdit內顯示文字顏色

設定文字插入位置
//移動游標m_RichEdit.SendMessage(EM_LINEINDEX, m_RichEdit.GetLineCount(), 0);            //移到最底行m_RichEdit.SendMessage(EM_SETSEL, m_RichEdit.GetWindowTextLength(), m_RichEdit.GetWindowTextLength());    //移到最後一個字元後面

改變顯示顏色
    CHARFORMAT chFormat;
    ::ZeroMemory(&chFormat, sizeof(CHARFORMAT));        
    m_RichEdit.GetSelectionCharFormat(chFormat);     //取得目前文字格式  
    chFormat.dwMask |= CFM_COLOR;                    //設定修改顏色屬性
    chFormat.dwEffects &= ~CFE_AUTOCOLOR;        //移除 CFE_AUTOCOLOR
    chFormat.crTextColor = RGB(255, 0, 0);

    m_RichEdit.SetSelectionCharFormat(chFormat);
    m_RichEdit.ReplaceSel(str);        //因為沒有選擇文字,所以會將str塞到最後面

Memo用,可直接使用,需再新增其他功能時,要再修改與測試。

[C] String Copy

while (*s++ = *t++);

1. *s = *t
2. *s+=1 and *t+=1
3. *t == NULL ||  -> Exit
     *t != NULL     -> do 1

[MFC]Timer

.h
afx_msg void OnTimer(UINT_PTR nIDEvent)

Message map
ON_WM_TIMER()

void CDialog:: OnTimer(UINT nIDEvent)
{
CView:: OnTimer(nIDEvent);
switch (nIDEvent) ...

}

//OnInitialDialog
SetTimer(ANIMATE_MFT, 20, NULL);

//When Used
OnTimer(ANIMATE_TIMER);
OnTimer(ANIMATE_MFT);


//Destructor
KillTimer(ANIMATE_TIMER);

[MFC]Thread用到UpdateData

可參考http://realchecko.blogspot.com/2007/06/updatedata-in-thread.html
這篇寫得所蠻詳細的。

[MFC]Rich Edit 2.0 Control

在使用Rich Edit 2.0 Control時,要在App::InitInstance()裡,產生Dialog物件之前呼叫AfxInitRichEdit2()
這樣才能使得編譯出來的程式可以執行,不然會在.DoModal()的時候就會出現handle錯誤而結束。

Build docker image from multiple build contexts

Build docker image from multiple build contexts ...