您的位置:首页 > 其它

windows 无窗口进程间消息通…

2014-12-24 21:01 218 查看
主进程源码:

#include

#include

#include

#include

TCHAR g_ShareMemCMD[] = "testipc/buf";

const int g_ShareMemSize = 512;

int main( void )

{

    
//创建子进程

STARTUPINFO
si={sizeof(si)};             

 PROCESS_INFORMATION pi;

ZeroMemory( &pi, sizeof(pi) );

ZeroMemory( &si, sizeof(si) );

si.cb = sizeof(si);

TCHAR  chPath[]=TEXT("E:\\my
project\\testpro\\testMessage\\Debug\\childProcess.exe");       
//我的child.exe程序位于该目录下,作为第一

//个参数传给CreateProcess

if(CreateProcess(chPath, NULL, NULL, NULL, FALSE,
CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi))

{

CloseHandle( pi.hProcess
);             
//hProcess跟hThread为pi的成员,分别为进程跟线程的句柄

CloseHandle( pi.hThread );

}

else

{

printf("%s","Child process created faild!\n");

}

void * m_hMapCMD = NULL;

SECURITY_ATTRIBUTES SecAttr;

SECURITY_DESCRIPTOR
SecDesc;         

SecAttr.nLength = sizeof(SecAttr); 

SecAttr.bInheritHandle = FALSE; 

SecAttr.lpSecurityDescriptor = &SecDesc;
//指向访问权限列表

InitializeSecurityDescriptor(&SecDesc,
SECURITY_DESCRIPTOR_REVISION); 

SetSecurityDescriptorDacl(&SecDesc, TRUE, 0, FALSE);
//设置访问权限列表为空(不受限制)       

m_hMapCMD = CreateFileMapping(

INVALID_HANDLE_VALUE,   
// use paging file

&SecAttr,                  

PAGE_EXECUTE_READWRITE,         
// read/write access

0,                      
// maximum object size (high-order DWORD)

g_ShareMemSize,               
// maximum object size (low-order DWORD)

g_ShareMemCMD);

if (NULL == m_hMapCMD)

{

   
printf("could not create file mapping object(%d).\n",
GetLastError());

}

void * m_pCMDBuf =  NULL;

m_pCMDBuf = (char*) MapViewOfFile(m_hMapCMD, FILE_MAP_ALL_ACCESS,
0, 0, g_ShareMemSize);

   
 

if (NULL == m_pCMDBuf)

{

   
printf("could not map view of file .\n");

}

int * p = (int*)m_pCMDBuf;

*p = 123454321;

printf("shareMemery id is : %d\n", *p);

printf(" 新进程的主线程ID号:%d /n", pi.dwThreadId);

Sleep(1000);

int j = 5;

while(j--)

{

 if(0 != PostThreadMessage(pi.dwThreadId,
WM_USER,0,0))

 {

  printf("postthreadmessage
success !\n");

  break;

 }

 else

 {

  printf("postthreadmessage
fialed !\n");

  Sleep(1000);

 }

}

 SYSTEMTIME sys;

while(true){

GetLocalTime( &sys );

printf("%s","The parent is talking at ");

printf( "M/d/d d:d:d 星期\n", sys.wYear, 

   
sys.wMonth,  sys.wDay, sys.wHour, sys.wMinute,
sys.wSecond,sys.wDayOfWeek);

Sleep(2000);

}

return 0;

}

 

子进程源码:

#include

#include

TCHAR g_ShareMemCMD[] = "testipc/buf";

const int g_ShareMemSize = 512;

int main( void )

{

printf("child process!");

DWORD threadId = GetCurrentThreadId();

printf("%d\n", threadId);

void * m_hMapCMD = NULL;

m_hMapCMD = OpenFileMapping(

               
FILE_MAP_ALL_ACCESS,   //
read/write access

               
FALSE,                
// do not inherit the name

               
g_ShareMemCMD);

if (NULL == m_hMapCMD)

{

   
printf("could not create file mapping object(%d).\n",
GetLastError());

}

void * m_pCMDBuf =  NULL;

m_pCMDBuf = (char*) MapViewOfFile(m_hMapCMD, FILE_MAP_ALL_ACCESS,
0, 0, g_ShareMemSize);

   
 

if (NULL == m_pCMDBuf)

{

   
printf("could not map view of file .\n");

}

int * p1 = (int*)m_pCMDBuf;

printf("shareMemery id is : %d\n", *p1);

 MSG msg;

 int i = 5;

 while(i--)

 {

  if (0 == PeekMessage(&msg,
NULL, WM_USER, WM_USER, PM_NOREMOVE))

  {

   printf("peekmessage
failed!\n");

   Sleep(1000);

  }

  else

  {

   printf("peekmessage
success!\n");

   break;

  }

 }

 

SYSTEMTIME sys;

while(true){

GetLocalTime( &sys );

printf("%s","The child is talking at ");

printf( "M/d/d d:d:d 星期\n", sys.wYear, 

   
sys.wMonth,  sys.wDay, sys.wHour, sys.wMinute,
sys.wSecond,sys.wDayOfWeek);

Sleep(2000);

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: