进程之间的通信可以通过发送消息,共享内存和设置全局Event等方式进行,这儿介绍一下通过设置多个Event来进行通信,以满足通信中不同信息的传递。(非常抱歉采用Event来叙述,因为网站的某个信息拦截系统对中文的Event进行了拦截,无法上传。)在下面的例子中,是A进程等待B进程运行结果。
A进程:
cpp代码
-
-
- HANDLE m_hEvent[3];
-
-
-
- m_hEvent[0] = CreateEvent(NULL, TRUE, FALSE, "syn_event_failed");
- m_hEvent[1] = CreateEvent(NULL, TRUE, FALSE, "syn_event_success");
- m_hEvent[2] = CreateEvent(NULL, TRUE, FALSE, "syn_event_others");
- ResetEvent(m_hEvent[0]);
- ResetEvent(m_hEvent[1]);
- ResetEvent(m_hEvent[2]);
-
-
-
- char path[255];
- strcpy(path,"c:abc.exe");
- ShellE xecute(NULL, NULL, path, NULL, NULL, SW_SHOWNORMAL);
-
-
-
-
- short returnFlag = 10;
- DWORD Event = 0;
- DWORD count = 0;
- while (1)
- {
- count++;
-
- Event = WaitForMultipleObjects(3, m_hEvent, FALSE, 50);
-
- switch (Event)
- {
- case 0:
- {
- returnFlag = 0;
- }
- break;
- case 1:
- {
- returnFlag = 1;
- }
- break;
- case 2:
- {
- returnFlag = 2;
- }
- break;
-
- }
- if (returnFlag != 10)
- {
- CloseHandle(m_hEvent[0]);
- CloseHandle(m_hEvent[1]);
- CloseHandle(m_hEvent[2]);
-
- return returnFlag;
- }
- if (count >= 1000)
- {
-
- return 0;
- }
- }
B进程:
cpp代码
-
-
- HANDLE m_hEvent;
-
-
-
- short ret = Func1();
-
-
- m_hEvent = NULL;
- switch(ret)
- {
- case 0:
- m_hEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, "syn_event_failed");
- break;
- case 1:
- m_hEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, "syn_event_success");
- break;
- case 2:
- m_hEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, "syn_event_others");
- break;
- }
-
-
- if(m_hEvent != NULL)
- {
-
- SetEvent(m_hEvent);
- Sleep(100);
- }
-
-
- return;
-
-
采用事件方式进行进程之间的通信,比较简单,但是通信的信息量比较小。如果需要大的数据通信或者同步,可以采用共享内存的方式。我会另写文字叙述。
阅读(7395) | 评论(0) | 转发(0) |