python与C++通过命名管道通讯

import win32pipe, win32file, pywintypes
def recv():
    print("pipe recv")
    quit = False

    while not quit:
        try:
            handle = win32pipe.CreateNamedPipe(
                # r'\\.\pipe\Foo',
                r'\\.\\Pipe\\mypipe',
                win32pipe.PIPE_ACCESS_DUPLEX,
                win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT,
                1, 65536, 65536,
                0,
                None)
            
          
            win32pipe.ConnectNamedPipe(handle, None)
            
            while True:
                
                with torch.no_grad():
                  
                    #resp = win32file.ReadFile(handle, 320*240*4)[1]
                   
                    win32file.WriteFile(handle, some_data)       
        except pywintypes.error as e:
            if e.args[0] == 2:
                print("no pipe, trying again in a sec")
                sleep(1)
            elif e.args[0] == 109:
                print("broken pipe, bye bye")
                quit = True


if __name__ == '__main__':
    recv()

c++端

#include "stdafx.h"
int main()
{
	DWORD wlen = 0;

	BOOL bRet = WaitNamedPipe(TEXT("\\\\.\\Pipe\\kat_walk"), NMPWAIT_WAIT_FOREVER);
	if (!bRet)
	{
		printf("connect the namedPipe failed!\n");
		return 0;
	}
//pipe通道——————————————————————————————————————————————————————————————————————————
	HANDLE hPipe = CreateFile(			//管道属于一种特殊的文件
		TEXT("\\\\.\\Pipe\\kat_walk"),	//创建的文件名
		GENERIC_READ | GENERIC_WRITE,	//文件模式
		0,								//是否共享
		NULL,							//指向一个SECURITY_ATTRIBUTES结构的指针
		OPEN_EXISTING,					//创建参数
		FILE_ATTRIBUTE_NORMAL,			//文件属性(隐藏,只读)NORMAL为默认属性
		NULL);							//模板创建文件的句柄
if (INVALID_HANDLE_VALUE == hPipe)
	{
		printf("open the exit pipe failed!\n");
		exit(0);
	}
while (true) {
		char * dataT = new char[1];
		DWORD rlen = 0;
		std::cout<

 

你可能感兴趣的:(c++基础的深入浅出)