一个机器上的两个进程间通信,可以使用很多种方式。但看《windows核心编程》说,其实归根结底都是使用了file-mapping kernel object。把那一章看了看,长了不少知识。但是我最感兴趣的地方还是:假设有两个线程:线程A和线程B。当线程A在运行的时候,线程B给A通信,使得A可以改变自己程序的运行轨迹。其实,这也算是为调试程序埋的伏笔吧。因为我现在的工作中就遇到一个这样的问题,程序A可以运行,但运行效果不是想要的,我想使用程序B给A发一些消息,改变A的运行轨迹。在VS2008的msdn文档中找了找,找到一个简单的例子,修改了一下。代码包括两部分,第一部分是程序A的代码,代码如下:
 #include <windows.h>
#include <windows.h>
 #include <stdio.h>
#include <stdio.h>
 #include <conio.h>
#include <conio.h>

 #define BUF_SIZE 256
#define BUF_SIZE 256
 TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
TCHAR szName[]=TEXT("Global\\MyFileMappingObject");

 int main()
int main()


 {
{
 HANDLE hMapFile;
    HANDLE hMapFile;
 char * pBuf;
    char * pBuf;

 hMapFile = CreateFileMapping(
    hMapFile = CreateFileMapping(
 INVALID_HANDLE_VALUE,    // use paging file
        INVALID_HANDLE_VALUE,    // use paging file
 NULL,                    // default security
        NULL,                    // default security 
 PAGE_READWRITE,          // read/write access
        PAGE_READWRITE,          // read/write access
 0,                       // max. object size
        0,                       // max. object size 
 BUF_SIZE,                // buffer size
        BUF_SIZE,                // buffer size  
 szName);                 // name of mapping object
        szName);                 // name of mapping object

 if (hMapFile == NULL)
    if (hMapFile == NULL) 

 
     {
{ 
 printf("Could not create file mapping object (%d).\n",
        printf("Could not create file mapping object (%d).\n", 
 GetLastError());
            GetLastError());
 return 1;
        return 1;
 }
    }
 pBuf = (char *) MapViewOfFile(hMapFile,   // handle to map object
    pBuf = (char *) MapViewOfFile(hMapFile,   // handle to map object
 FILE_MAP_ALL_ACCESS, // read/write permission
        FILE_MAP_ALL_ACCESS, // read/write permission
 0,
        0,                   
 0,
        0,                   
 BUF_SIZE);
        BUF_SIZE);           

 if (pBuf == NULL)
    if (pBuf == NULL) 

 
     {
{ 
 printf("Could not map view of file (%d).\n",
        printf("Could not map view of file (%d).\n", 
 GetLastError());
            GetLastError()); 
 return 2;
        return 2;
 }
    }

 while (1)
    while (1)

 
     {
{
 if (*pBuf == 'a')
        if (*pBuf == 'a')

 
         {
{
 printf("hello world!\n");
            printf("hello world!\n");
 }
        }
 else
        else

 
         {
{
 printf("no hello\n");
            printf("no hello\n");
 }
        }
 Sleep(2000);
        Sleep(2000);
 }
    }

 UnmapViewOfFile(pBuf);
    UnmapViewOfFile(pBuf);

 CloseHandle(hMapFile);
    CloseHandle(hMapFile);

 return 0;
    return 0;
 }
}第二部分是程序B的代码,代码如下:
 #include <windows.h>
#include <windows.h>
 #include <stdio.h>
#include <stdio.h>
 #include <conio.h>
#include <conio.h>

 #define BUF_SIZE 256
#define BUF_SIZE 256
 TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
TCHAR szName[]=TEXT("Global\\MyFileMappingObject");

 int main()
int main()


 {
{
 HANDLE hMapFile;
    HANDLE hMapFile;
 char * pBuf;
    char * pBuf;

 hMapFile = OpenFileMapping(
    hMapFile = OpenFileMapping(
 FILE_MAP_ALL_ACCESS,   // read/write access
        FILE_MAP_ALL_ACCESS,   // read/write access
 FALSE,                 // do not inherit the name
        FALSE,                 // do not inherit the name
 szName);               // name of mapping object
        szName);               // name of mapping object 

 if (hMapFile == NULL)
    if (hMapFile == NULL) 

 
     {
{ 
 printf("Could not open file mapping object (%d).\n",
        printf("Could not open file mapping object (%d).\n", 
 GetLastError());
            GetLastError());
 return 1;
        return 1;
 }
    } 

 pBuf = (char *) MapViewOfFile(hMapFile, // handle to map object
    pBuf = (char *) MapViewOfFile(hMapFile, // handle to map object
 FILE_MAP_ALL_ACCESS,  // read/write permission
        FILE_MAP_ALL_ACCESS,  // read/write permission
 0,
        0,                    
 0,
        0,                    
 BUF_SIZE);
        BUF_SIZE);                   

 if (pBuf == NULL)
    if (pBuf == NULL) 

 
     {
{ 
 printf("Could not map view of file (%d).\n",
        printf("Could not map view of file (%d).\n", 
 GetLastError());
            GetLastError()); 
 return 2;
        return 2;
 }
    }

 printf("press a key!\n");
    printf("press a key!\n");
 _getch();
    _getch();
 *pBuf = 'a';
    *pBuf = 'a';
 
    
 printf("press another key!\n");
    printf("press another key!\n");
 _getch();
    _getch();
 *pBuf = 'b';
    *pBuf = 'b';

 UnmapViewOfFile(pBuf);
    UnmapViewOfFile(pBuf);

 CloseHandle(hMapFile);
    CloseHandle(hMapFile);

 return 0;
    return 0;
 }
}        平时的时候,程序A正常运行,每隔两秒打印一次“no hello”,启动程序B后,如果按下回车,则程序A会不断打印“hello world”,程序B中再次按回车,程序A又回到原来的运行轨迹,不断打印“no hello”。这样,通过file-mapping kernel object的方式,实现了进程间的通信。
        至于文件映射内核对象的原理之类的话,我就不多说了,使用google搜索,一搜一大把。大家也可以看书,《windows核心编程》讲的很好。
        其实,上面这个例子,我感觉挺傻的。不过现在没想到多么好的办法,可以使进程B通过通信的方式影响进程A的运行轨迹。 先把这种笨方法记录下来,等找到好的办法,再写上来,哈哈。
        网上还有好多不错的文章,贴出几个来:
        http://blog.codingnow.com/2005/10/interprocess_communications.html
        http://learn.akae.cn/media/ch30s04.html