HRESULT CpiReadRollbackDataList(
	HANDLE hFile,
	CPI_ROLLBACK_DATA** pprdList
	)
{
	HRESULT hr = S_OK;

	int iCount;

	CPI_ROLLBACK_DATA* pItm = NULL;

	// read count
	hr = ReadFileAll(hFile, (PBYTE)&iCount, sizeof(int));
	if (HRESULT_FROM_WIN32(ERROR_HANDLE_EOF) == hr)
		ExitFunction1(hr = S_OK); // EOF reached, nothing left to read
	ExitOnFailure(hr, "Failed to read count");

	for (int i = 0; i < iCount; i++)
	{
		// allocate new element
		pItm = (CPI_ROLLBACK_DATA*)::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CPI_ROLLBACK_DATA));
		if (!pItm)
			ExitFunction1(hr = E_OUTOFMEMORY);

		// read from file
		hr = ReadFileAll(hFile, (PBYTE)pItm->wzKey, MAX_DARWIN_KEY * sizeof(WCHAR));
		if (HRESULT_FROM_WIN32(ERROR_HANDLE_EOF) == hr)
			break; // EOF reached, nothing left to read
		ExitOnFailure(hr, "Failed to read key");

		hr = ReadFileAll(hFile, (PBYTE)&pItm->iStatus, sizeof(int));
		if (HRESULT_FROM_WIN32(ERROR_HANDLE_EOF) == hr)
			pItm->iStatus = 0; // EOF reached, the operation was interupted; set status to zero
		else
			ExitOnFailure(hr, "Failed to read status");

		// add to list
		if (*pprdList)
			pItm->pNext = *pprdList;
		*pprdList = pItm;
		pItm = NULL;
	}

	hr = S_OK;

LExit:
	// clean up
	if (pItm)
		CpiFreeRollbackDataList(pItm);

	return hr;
}
Exemple #2
0
UINT WINAPI CPipeServer::ServerThread(LPVOID pParam)
{
	CoInitialize(NULL);
	CPipeServer* pSys = (CPipeServer*)pParam;

	HANDLE hPipe = NULL;
	HANDLE hEventConnect = NULL;
	HANDLE hEventArray[2];
	OVERLAPPED stOver;

	SECURITY_DESCRIPTOR sd = {};
	SECURITY_ATTRIBUTES sa = {};
	if( pSys->insecureFlag != FALSE &&
	    InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION) != FALSE &&
	    SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE) != FALSE ){
		//安全でないセキュリティ記述子(NULL DACL)をセットする
		sa.nLength = sizeof(sa);
		sa.lpSecurityDescriptor = &sd;
	}
	hEventConnect = CreateEvent(sa.nLength != 0 ? &sa : NULL, FALSE, FALSE, pSys->eventName.c_str());
	hEventArray[0] = pSys->stopEvent;
	hEventArray[1] = CreateEvent(NULL, FALSE, FALSE, NULL);
	
	if( hPipe == NULL ){
		hPipe = CreateNamedPipe(pSys->pipeName.c_str(), PIPE_ACCESS_DUPLEX | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_OVERLAPPED,
		                        PIPE_TYPE_BYTE, 1, 8192, 8192, PIPE_TIMEOUT, sa.nLength != 0 ? &sa : NULL);
		if( hPipe == INVALID_HANDLE_VALUE ){
			hPipe = NULL;
		}

		ZeroMemory(&stOver, sizeof(OVERLAPPED));
		stOver.hEvent = hEventArray[1];
	}

	while( hPipe != NULL ){
		ConnectNamedPipe(hPipe, &stOver);
		SetEvent(hEventConnect);

		DWORD dwRes = WaitForMultipleObjects(2, hEventArray, FALSE, INFINITE);
		if( dwRes == WAIT_OBJECT_0 ){
			//STOP
			break;
		}else if( dwRes == WAIT_OBJECT_0+1 ){
			//コマンド受信
			DWORD dwWrite = 0;
			DWORD head[2];
			for(;;){
				CMD_STREAM stCmd;
				CMD_STREAM stRes;
				if( ReadFileAll(hPipe, (BYTE*)head, sizeof(head)) != sizeof(head) ){
					break;
				}
				stCmd.param = head[0];
				stCmd.dataSize = head[1];
				if( stCmd.dataSize > 0 ){
					stCmd.data.reset(new BYTE[stCmd.dataSize]);
					if( ReadFileAll(hPipe, stCmd.data.get(), stCmd.dataSize) != stCmd.dataSize ){
						break;
					}
				}

				if( pSys->cmdProc != NULL){
					pSys->cmdProc(pSys->cmdParam, &stCmd, &stRes);
					head[0] = stRes.param;
					head[1] = stRes.dataSize;
					if( WriteFile(hPipe, head, sizeof(DWORD)*2, &dwWrite, NULL ) == FALSE ){
						break;
					}
					if( stRes.dataSize > 0 ){
						if( WriteFile(hPipe, stRes.data.get(), stRes.dataSize, &dwWrite, NULL ) == FALSE ){
							break;
						}
					}
				}else{
					head[0] = CMD_NON_SUPPORT;
					head[1] = 0;
					if( WriteFile(hPipe, head, sizeof(DWORD)*2, &dwWrite, NULL ) == FALSE ){
						break;
					}
				}
				if( stRes.param != CMD_NEXT && stRes.param != OLD_CMD_NEXT ){
					//Enum用の繰り返しではない
					break;
				}
			}

			FlushFileBuffers(hPipe);
			DisconnectNamedPipe(hPipe);
		}
	}

	if( hPipe != NULL ){
		FlushFileBuffers(hPipe);
		DisconnectNamedPipe(hPipe);
		CloseHandle(hPipe);
	}

	CloseHandle(hEventArray[1]);
	CloseHandle(hEventConnect);
	CoUninitialize();
	return 0;
}