利用邮槽建立主机与虚拟机进程间的通信,只是很简单的一次实验,没啥技术含量。 主机与虚拟机用host-only方式连接。 虚拟机名称:www-work //Client.cpp #include <stdio.h> #include <tchar.h> #include <windows.h> int _tmain(int argc, _TCHAR* argv[]) { HANDLE hMail; char buf[100] = "http:www.baidu.com"; DWORD dwWritten; hMail = CreateFile(L"////www-work//mailslot//MyMail", GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(hMail == INVALID_HANDLE_VALUE) { printf("CreateFile faild. Error: %d/n", GetLastError()); return -1; } if( !WriteFile(hMail, buf, 100, &dwWritten, NULL)) { printf("write faild. ERROR:%d",GetLastError()); return -1; } printf("send msg successed!!!/n"); CloseHandle(hMail); return 0; } Server.cpp int _tmain(int argc, _TCHAR* argv[]) { HANDLE hMail; char buf[100]; DWORD dwRead; hMail = CreateMailslot( L"////.//mailslot//MyMail", 0, MAILSLOT_WAIT_FOREVER, NULL); if(hMail == INVALID_HANDLE_VALUE) { printf("create mailslot faild. error:%d/n", GetLastError()); return -1; } if( !ReadFile(hMail, buf, 100, &dwRead, NULL)) { printf("read faild! error:%d/n",GetLastError()); return -1; } printf("read %d bytes:%s", dwRead, buf); CloseHandle(hMail); getchar(); return 0; }