发送端代码:
HWND hwnd = ::FindWindow(NULL,TEXT("接收端窗口名"));
if(hwnd == NULL)
{
MessageBox(0,TEXT("没找到窗口"),TEXT("ERROR"),MB_OK);
}
else
{
// 发送消息
//SendMessage(hwnd,WM_USER+0x1,NULL, (LPARAM)100);
PostMessage(hwnd,WM_USER+0x1, NULL, (LPARAM)100);
}
接收端代码:
switch(uMsg)
{
case WM_CLOSE:
{
EndDialog(hDlg,0);
break;
}
case WM_USER+0x1:
{
DWORD x = wParam;
DWORD y = lParam;
MessageBox(0,0,0,0);
break;
}
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDC_BUTTON_RECV:
{
return TRUE;
}
}
break ;
}
//
共享内存
发送端代码:
HANDLE hMapObject;
HANDLE hMapView;
//创建FileMapping对象
hMapObject = CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,0x1000,TEXT("shared"));
if(!hMapObject)
{
MessageBox(NULL,TEXT("共享内存失败"),TEXT("Error"),MB_OK);
return FALSE;
}
//将FileMapping对象映射到自己的进程
hMapView = MapViewOfFile(hMapObject,FILE_MAP_WRITE,0,0,0);
if(!hMapView)
{
MessageBox(NULL,TEXT("内存映射失败"),TEXT("Error"),MB_OK);
return FALSE;
}
//向共享内存写入数据
strcpy((char*)hMapView,"Test Shared Memery");
接收端代码:
HANDLE hMapObject;
HANDLE hMapView;
//创建FileMapping对象
hMapObject = CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,0x1000,TEXT("shared"));
if(!hMapObject)
{
MessageBox(NULL,TEXT("共享内存失败"),TEXT("Error"),MB_OK);
return FALSE;
}
//将FileMapping对象映射到自己的进程
hMapView = MapViewOfFile(hMapObject,FILE_MAP_WRITE,0,0,0);
if(!hMapView)
{
MessageBox(NULL,TEXT("内存映射失败"),TEXT("Error"),MB_OK);
return FALSE;
}
//从共享内存读取数据
TCHAR szBuffer[0x1000] = {0};
memcpy(szBuffer,hMapView,10);
匿名管道:
父进程:
HANDLE hRead;
HANDLE hWrite;
SECURITY_ATTRIBUTES sa;
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
if(!CreatePipe(&hRead,&hWrite,&sa,0))
{
MessageBox(0,TEXT("创建匿名管道失败!"),TEXT("Error"),MB_OK);
}
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si,sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = hRead;
si.hStdOutput = hWrite;
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
if(!CreateProcess("E:\\Project\\zzzzzzz\\Debug\\zzzzzzz.exe",NULL,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi))
{
CloseHandle(hRead);
CloseHandle(hWrite);
hRead = NULL;
hWrite = NULL;
MessageBox(0,TEXT("创建子进程失败!"),TEXT("Error"),MB_OK);
}
else
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
//写数据
TCHAR szBuffer[] = "http:\\www.dtdebug.com";
DWORD dwWrite;
if(!WriteFile(hWrite,szBuffer,strlen(szBuffer)+1,&dwWrite,NULL))
{
MessageBox(0,TEXT("写数据失败!"),TEXT("Error"),MB_OK);
}
//读数据
TCHAR szBuffer[100];
DWORD dwRead;
if(!ReadFile(hRead,szBuffer,100,&dwRead,NULL))
{
MessageBox(NULL,TEXT("读取数据失败!"),TEXT("Error"),MB_OK);
}
else
{
MessageBox(NULL,szBuffer,TEXT("[读取数据]"),MB_OK);
}
子进程:
//初始化
HANDLE hRead = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hWrite = GetStdHandle(STD_OUTPUT_HANDLE);
//读数据
TCHAR szBuffer[100];
DWORD dwRead;
if(!ReadFile(hRead,szBuffer,100,&dwRead,NULL))
{
MessageBox(NULL,TEXT("读取数据失败!"),TEXT("Error"),MB_OK);
}
else
{
MessageBox(NULL,szBuffer,TEXT("[读取数据]"),MB_OK);
}
//写数据
TCHAR szBuffer[100] = "匿名管道";
DWORD dwWrite;
if(!WriteFile(hWrite,szBuffer,strlen(szBuffer)+1,&dwWrite,NULL))
{
MessageBox(NULL,TEXT("写入数据失败!"),TEXT("Error"),MB_OK);
}