본문 바로가기

Windows/MFC

IPC 사용법

[code]// 선언 부분
#define WM_MY_MSG WM_USER+7 // 사용자 정의 메시지

// 보내는 쪽
void CIPCView::OnUserMsg()
{
    CWnd* pWnd = CWnd::FindWindow(NULL, "받기프로그램");
    if(!pWnd)
    {
        AfxMessageBox("프로그램을 못찾음");
        return;
    }
    //pWnd->SendMessage(WM_MY_MSG, (WPARAM)GetCurrentProcessId());
    
    // 사용자 정의 메시지로 보낸다.
    pWnd->PostMessage(WM_MY_MSG, (WPARAM)GetCurrentProcessId());
    AfxMessageBox("메시지 전송 완료");
}

void CIPCView::OnCopyData()
{
    CString strData;
    GetDlgItemText(IDC_DATA, strData); // 에디트 컨트롤의 문자열 얻음

    COPYDATASTRUCT cds;
    cds.dwData = 1004; // 사용 목적에 따른 식별값
    cds.cbData = strData.GetLength()+1; // 전달될 정보 lpData의 크기
    cds.lpData = (LPSTR)(LPCSTR)strData; // 전달될 정보

    CWnd* pWnd = CWnd::FindWindow(NULL, "받기프로그램");
    if(!pWnd)    
    {
        AfxMessageBox("프로그램을 못찾음");
        return;
    }
    // Window Message 로 보낸다.
    pWnd->SendMessage(WM_COPYDATA, (WPARAM)m_hWnd, (LPARAM)&cds);
}[/code]

[code]
// 받는 쪽
//============================================================
// 프로세스 통신 방법
LONG CSubDlg::OnMyUserProc(WPARAM wParam, LPARAM lParam)
{
    AfxMessageBox("메시지 받음");
    CString str;
    LONG nID = (LONG)wParam; // 전달받은 상대방 프로그램의 프로세스ID
    str.Format("Process ID = %X", nID);
    SetDlgItemText(IDC_USER_MSG, str);
    return 0L;
}
//=================================================================
// WM_COPYDATA 처리 과정
BOOL CSubDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
    AfxMessageBox("메시지 받음");
    
    switch(pCopyDataStruct->dwData) // 사용 용도를 판단
    {
    case 1004:
        SetDlgItemText(IDC_DATA, (LPCSTR)pCopyDataStruct->lpData);
        break;
    }
    return CDialog::OnCopyData(pWnd, pCopyDataStruct);
}

HBRUSH CSubDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    
    if(    nCtlColor == CTLCOLOR_STATIC)
    {
        pDC->SetTextColor(RGB(0, 0, 255));
    }
    // TODO: Return a different brush if the default is not desired
    return hbr;
}
[/code]

'Windows > MFC' 카테고리의 다른 글

COLORREF 에서 r, g, b 를 정수형으로  (0) 2013.10.02
툴팁(Tool tip) 사용법  (0) 2013.10.02
FPU를 이용한 형변환  (0) 2013.10.02
탭 컨트롤 사용 (Tab Control)  (0) 2013.10.02
콤보 박스 설정 (Combo box)  (0) 2013.10.02