0
我不明白爲什麼這個不要工作:的CreateFile/ReadFile的WriteFile的與多線程 - 無效的句柄
void __cdecl main_pipe_client(void * Args)
{
py_func python_func;
BOOL bRet;
DWORD size = 0;
hpipe = CreateFile(TEXT("D:/stc_5010"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
if (hpipe == INVALID_HANDLE_VALUE)
{
// its OK there is no error
}
while (1)
{
char wiadomosc[50];
bRet = ReadFile(hpipe, wiadomosc, 50, &size, NULL);
//READING ONLINE
if (bRet == FALSE)
{
python_func.py_AppendChat(1, 1, "Read Failed : Bytes Read : %u", size);
break;
}
if (size > 0){
// There is OK
}
Sleep(1500);
size = 0;
}
}
hpipe - 在程序HANDLE hpipe的開頭聲明;
void init(){
Sleep(500);
py_func python_func;
char buf[256];
DWORD wpisano = 0;
sprintf_s(buf, "INIT_CHARACTER");
if (!WriteFile(hpipe, buf, 256, &wpisano, NULL)){
char buf5[256];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf5, 256, NULL);
sprintf_s(buf5, "%s %d", buf5, GetLastError());
MessageBoxA(NULL, (LPCSTR)buf5, (LPCSTR) "ERROR", NULL); // return error 6
}
}
但是,如果我在循環中,在main_pipe_client函數寫入文件,這很好。數據寫入文件。 但是,如果我以後
HANDLE hThread = (HANDLE)_beginthread(main_pipe_client, 0, NULL);
然後WriteFile的返回無效手柄...
爲什麼在MessageBoxA()中調用這些轉換?他們看起來沒用。 – Medinoc
'//確定沒有錯誤'。不在這個星球上。 –