改了一些代码,发现运行结果就完全不一样了。
https://docs.microsoft.com/zh-cn/windows/desktop/ipc/named-pipe-server-using-overlapped-i-o
PIPEINST Pipe[INSTANCES];
这一句原来为全局变量。
我改为main函数里的局部变量。(还要改DisconnectAndReconnect就不细说了)
这时候就发现在下面case READING_STATE:下面的wErr = GetLastError();
就发生了改变,原来返回997,改为局部变量之后就变成了87。
-
为此又分别做了其他的尝试
PIPEINST* Pipe = (PIPEINST*)GlobalAlloc(GPTR, sizeof(PIPEINST)*INSTANCES); // success
PIPEINST* Pipe = new PIPEINST[INSTANCES](); // failed
PIPEINST* Pipe = (PIPEINST*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PIPEINST)*INSTANCES); // success
PIPEINST* Pipe = (PIPEINST*)LocalAlloc(LPTR, sizeof(PIPEINST)*INSTANCES); // success
PIPEINST* Pipe = (PIPEINST*)malloc(sizeof(PIPEINST)*INSTANCES); //failed
匪夷所以,是内存分配的问题?
GetlastError的问题?
还是其他的问题?