BOOL pCompareHardwareID( LPCTSTR szHardwareID, LPCTSTR mszFindingHardwareIDs) { LPCTSTR lpszCur = mszFindingHardwareIDs; while (TRUE) { if (_T('\0') == *lpszCur) { XTLTRACE1(TRACE_LEVEL_INFORMATION, _T("Hardware ID %s not found.\n"), mszFindingHardwareIDs); return FALSE; } XTLTRACE1(TRACE_LEVEL_INFORMATION, _T("Comparing Hardware ID %s against %s\n"), szHardwareID, lpszCur); if (0 == lstrcmpi(szHardwareID, lpszCur)) { XTLTRACE1(TRACE_LEVEL_INFORMATION, _T("Found %s\n"), lpszCur); return TRUE; } // next hardware ID _ASSERTE(!::IsBadStringPtr(lpszCur, -1)); while (_T('\0') != *lpszCur) { ++lpszCur; } _ASSERTE(!::IsBadStringPtr(lpszCur, -1)); ++lpszCur; } }
BOOL pCompareINF( LPCTSTR szInfFile, LPCTSTR mszHardwareIDs) { XTLTRACE1(TRACE_LEVEL_INFORMATION, _T("Inspecting INF %s for %s...\n"), szInfFile, mszHardwareIDs); HINF hInf = ::SetupOpenInfFile( szInfFile, NULL, INF_STYLE_WIN4, 0); if (INVALID_HANDLE_VALUE == hInf) { XTLTRACE1(TRACE_LEVEL_ERROR, _T("SetupOpenInfFile failed, error=0x%X\n"), GetLastError()); return FALSE; } BOOL fMatch = pProcessManufacturerSection(hInf, mszHardwareIDs); ::SetupCloseInfFile(hInf); return fMatch; }
HRESULT NdasFsCtlShutdownEx( __in HANDLE ControlDeviceHandle) { HRESULT hr; DWORD bytesReturned; BOOL success = ::DeviceIoControl( ControlDeviceHandle, FSCTL_NDAS_FS_UNLOAD, NULL, 0, NULL, 0, &bytesReturned, NULL); if (!success) { hr = HRESULT_FROM_WIN32(GetLastError()); XTLTRACE1(TRACE_LEVEL_ERROR, "NdasFsCtlShutdown failed, handle=%p, hr=0x%X\n", ControlDeviceHandle, hr); return hr; } XTLTRACE1(TRACE_LEVEL_INFORMATION, "NdasFs is now shut down and ready to unload, handle=%p\n", ControlDeviceHandle); return S_OK; }
BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved) { switch (dwReason) { case DLL_PROCESS_ATTACH: XTLTRACE1(TRACE_LEVEL_INFORMATION, _T("NDMSICA.DLL Process Attach\n")); break; case DLL_THREAD_ATTACH: XTLTRACE1(TRACE_LEVEL_INFORMATION, _T("NDMSICA.DLL Thread Attach\n")); break; case DLL_THREAD_DETACH: XTLTRACE1(TRACE_LEVEL_INFORMATION, _T("NDMSICA.DLL Thread Detach\n")); break; case DLL_PROCESS_DETACH: XTLTRACE1(TRACE_LEVEL_INFORMATION, _T("NDMSICA.DLL Process Detach\n")); break; } return TRUE; }
BOOL CNdasHIXChangeNotify::Notify( CONST NDAS_DEVICE_ID& deviceId) { NDAS_HIX::DEVICE_CHANGE::NOTIFY notify = {0}; NDAS_HIX::DEVICE_CHANGE::PNOTIFY pNotify = ¬ify; NDAS_HIX::PHEADER pHeader = &pNotify->Header; _ASSERTE(sizeof(pNotify->DeviceId) == sizeof(deviceId.Node)); ::CopyMemory( pNotify->DeviceId, deviceId.Node, sizeof(pNotify->DeviceId)); DWORD cbLength = sizeof(NDAS_HIX::DEVICE_CHANGE::NOTIFY); pBuildNHIXRequestHeader(pHeader, &m_hostGuid, NHIX_TYPE_DEVICE_CHANGE, static_cast<USHORT>(cbLength)); pNHIXHeaderToNetwork(pHeader); BOOL fSuccess = m_bcaster.Broadcast( NDAS_HIX_LISTEN_PORT, cbLength, (CONST BYTE*) pNotify); if (!fSuccess) { XTLTRACE1(TRACE_LEVEL_ERROR, "Broadcast failed, error=0x%X", GetLastError()); return FALSE; } return TRUE; }
BOOL pProcessModelSection( HINF hInfFile, LPCTSTR szModelSection, LPCTSTR szPlatform, LPCTSTR mszHardwareIDs) { XTLTRACE1(TRACE_LEVEL_INFORMATION, _T("[%s.%s]\n"), szModelSection, szPlatform ? szPlatform : _T("none")); TCHAR Section[255] = {0}; HRESULT hr = ::StringCchCopy(Section, RTL_NUMBER_OF(Section), szModelSection); _ASSERTE(SUCCEEDED(hr)); // If szPlatform is given, append to the section if (szPlatform && szPlatform[0] != 0) { hr = ::StringCchCat(Section, RTL_NUMBER_OF(Section), _T(".")); _ASSERTE(SUCCEEDED(hr)); hr = ::StringCchCat(Section, RTL_NUMBER_OF(Section), szPlatform); _ASSERTE(SUCCEEDED(hr)); } INFCONTEXT infContext = {0}; BOOL fSuccess = ::SetupFindFirstLine( hInfFile, Section, NULL, &infContext); if (!fSuccess) { return FALSE; } do { TCHAR szHardwareID[MAX_PATH] = {0}; fSuccess = ::SetupGetStringField( &infContext, 2, szHardwareID, MAX_PATH, NULL); if (!fSuccess) { return FALSE; } fSuccess = pCompareHardwareID(szHardwareID, mszHardwareIDs); if (fSuccess) { return TRUE; } fSuccess = ::SetupFindNextLine(&infContext, &infContext); } while (fSuccess); return FALSE; }
BOOL CNdasHIXChangeNotify::Initialize() { BOOL fSuccess = m_bcaster.Initialize(); if (!fSuccess) { XTLTRACE1(TRACE_LEVEL_ERROR, "CLpxDatagramMultiClient init failed, error=0x%X", GetLastError()); return FALSE; } return TRUE; }
BOOL CNamedPipeTransport:: Accept( LPOVERLAPPED lpOverlapped) { _ASSERTE( (NULL == lpOverlapped) || (!IsBadReadPtr(lpOverlapped, sizeof(OVERLAPPED)) && NULL != lpOverlapped->hEvent && INVALID_HANDLE_VALUE != lpOverlapped->hEvent)); BOOL fConnected = ::ConnectNamedPipe(m_hPipe, lpOverlapped); if (fConnected) { XTLTRACE1(TRACE_LEVEL_VERBOSE, "Accepting a new connection.\n"); return TRUE; } if (lpOverlapped) { // if overlapped operation ConnectNamedPipe should return FALSE; DWORD dwError = ::GetLastError(); switch (dwError) { case ERROR_PIPE_CONNECTED: ::SetEvent(lpOverlapped->hEvent); // omitting break is intentional case ERROR_IO_PENDING: XTLTRACE1(TRACE_LEVEL_INFORMATION, "Accepting a new connection.\n"); return TRUE; default: // An error occurs during the connect operation XTLTRACE1(TRACE_LEVEL_ERROR, "ConnectNamedPipe failed, error=0x%X\n", GetLastError()); return FALSE; } } return FALSE; }
HRESULT NdasFsCtlOpenControlDevice( __in LPCWSTR ControlDeviceName, __in DWORD DesiredAccess, __deref_out HANDLE* ControlDeviceHandle) { HRESULT hr; XTLASSERT(NULL != ControlDeviceHandle); *ControlDeviceHandle = INVALID_HANDLE_VALUE; // // Returning handle must not be AutoFileHandle // HANDLE handle = ::CreateFile( ControlDeviceName, DesiredAccess, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_DEVICE, NULL); if (INVALID_HANDLE_VALUE == handle) { hr = HRESULT_FROM_WIN32(GetLastError()); XTLTRACE1(TRACE_LEVEL_ERROR, "CreateFile(%ls) failed, hr=0x%X\n", ControlDeviceName, hr); return hr; } *ControlDeviceHandle = handle; return S_OK; }
BOOL pProcessManufacturerSection( HINF hInfFile, LPCTSTR mszHardwareIDs) { INFCONTEXT infContext = {0}; TCHAR szModel[MAX_PATH] = {0}; BOOL fSuccess = ::SetupFindFirstLine( hInfFile, _T("Manufacturer"), NULL, &infContext); if (!fSuccess) { XTLTRACE1(TRACE_LEVEL_WARNING, _T("No Manufacturer section found.\n")); return FALSE; } do { // Model Section Fields fSuccess = ::SetupGetStringField( &infContext, 1, szModel, MAX_PATH, NULL); if (!fSuccess) { XTLTRACE1(TRACE_LEVEL_ERROR, _T("No Model section found.\n")); return FALSE; } XTLTRACE1(TRACE_LEVEL_INFORMATION, _T("Model: %s\n"), szModel); DWORD fieldCount = ::SetupGetFieldCount(&infContext); XTLTRACE1(TRACE_LEVEL_INFORMATION, _T("Decorations: %d\n"), fieldCount); if (fieldCount < 2) { XTLTRACE1(TRACE_LEVEL_INFORMATION, _T("TargetOS: (none)\n")); fSuccess = pProcessModelSection( hInfFile, szModel, NULL, mszHardwareIDs); if (fSuccess) { return TRUE; } } else { // String Field is 1-based index for (DWORD i = 2; i <= fieldCount; ++i) { // Decoration: e.g. NTamd64 TCHAR szDecoration[50] = {0}; fSuccess = ::SetupGetStringField( &infContext, i, szDecoration, RTL_NUMBER_OF(szDecoration), NULL); // Ignore if there is no decoration if (!fSuccess) { continue; } XTLTRACE1(TRACE_LEVEL_INFORMATION, _T("TargetOS: %s\n"), szDecoration); fSuccess = pProcessModelSection( hInfFile, szModel, szDecoration, mszHardwareIDs); if (fSuccess) { return TRUE; } } } fSuccess = ::SetupFindNextLine(&infContext, &infContext); } while (fSuccess); return FALSE; }
BOOL CNamedPipeTransport::Send( LPCVOID lpBuffer, DWORD cbToSend, LPDWORD lpcbSent, LPOVERLAPPED lpOverlapped, DWORD dwFlags) { // AUTOFUNCTRACE(); _ASSERTE(!IsBadReadPtr(lpBuffer, cbToSend)); _ASSERTE(!IsBadWritePtr(lpcbSent, sizeof(DWORD))); _ASSERTE( (NULL == lpOverlapped) || (!IsBadReadPtr(lpOverlapped, sizeof(OVERLAPPED)) && NULL != lpOverlapped->hEvent && INVALID_HANDLE_VALUE != lpOverlapped->hEvent)); UNREFERENCED_PARAMETER(dwFlags); // handling sending zero-byte packet if (cbToSend == 0) { *lpcbSent = 0; if (lpOverlapped) { BOOL fSuccess = ::SetEvent(lpOverlapped->hEvent); _ASSERT(fSuccess); } XTLTRACE1(TRACE_LEVEL_INFORMATION, "Sending 0 byte ignored\n"); return TRUE; } XTLTRACE1(TRACE_LEVEL_VERBOSE, "Sending %d bytes\n", cbToSend); BOOL fSuccess(FALSE); DWORD cbCurSent(0), cbCurToSend(cbToSend); const BYTE* lpCurBuffer = reinterpret_cast<const BYTE*>(lpBuffer); *lpcbSent = 0; while (cbCurToSend > 0) { fSuccess = ::WriteFile(m_hPipe, lpBuffer, cbCurToSend, &cbCurSent, lpOverlapped); if (!fSuccess && NULL != lpOverlapped && ::GetLastError() != ERROR_IO_PENDING) { break; } if (lpOverlapped) { // wait until timeout (to prevent indefinite waiting...) DWORD dwWaitResult = ::WaitForSingleObject(lpOverlapped->hEvent, TRANSMIT_TIMEOUT); if (dwWaitResult != WAIT_OBJECT_0) { switch (dwWaitResult) { case WAIT_TIMEOUT: ::SetLastError(WAIT_TIMEOUT); break; case WAIT_ABANDONED: ::SetLastError(WAIT_ABANDONED); break; default: break; } return FALSE; } fSuccess = ::GetOverlappedResult(m_hPipe, lpOverlapped, &cbCurSent, TRUE); if (!fSuccess && ::GetLastError() != ERROR_IO_PENDING) { break; } } cbCurToSend -= cbCurSent; *lpcbSent += cbCurSent; lpCurBuffer += cbCurSent; } XTLTRACE1(TRACE_LEVEL_VERBOSE, "Sent %d bytes\n", *lpcbSent); return fSuccess; }
BOOL CNamedPipeTransport::Receive( LPVOID lpBuffer, DWORD cbToReceive, LPDWORD lpcbReceived, LPOVERLAPPED lpOverlapped, LPDWORD lpdwFlags) { // AUTOFUNCTRACE(); _ASSERTE(!IsBadWritePtr(lpBuffer, cbToReceive)); _ASSERTE(!IsBadWritePtr(lpcbReceived, sizeof(DWORD))); _ASSERTE(NULL == lpOverlapped || !IsBadWritePtr(lpOverlapped, sizeof(OVERLAPPED)) && NULL != lpOverlapped->hEvent && INVALID_HANDLE_VALUE != lpOverlapped->hEvent); UNREFERENCED_PARAMETER(lpdwFlags); // handling sending zero-byte packet if (cbToReceive == 0) { *lpcbReceived = 0; if (lpOverlapped) { BOOL fSuccess = ::SetEvent(lpOverlapped->hEvent); _ASSERT(fSuccess); } return TRUE; } // we have to receive by the request size XTLTRACE1(TRACE_LEVEL_VERBOSE, "Reading %d bytes\n", cbToReceive); BOOL fSuccess(FALSE); DWORD cbCurReceived(0), cbCurToReceive(cbToReceive); LPBYTE lpCurBuffer = reinterpret_cast<LPBYTE>(lpBuffer); *lpcbReceived = 0; while (cbCurToReceive > 0) { fSuccess = ::ReadFile(m_hPipe, lpBuffer, cbCurToReceive, &cbCurReceived, lpOverlapped); if (!fSuccess && NULL != lpOverlapped && ::GetLastError() != ERROR_IO_PENDING) { break; } if (lpOverlapped) { // wait until timeout (to prevent indefinite waiting...) DWORD dwWaitResult = ::WaitForSingleObject(lpOverlapped->hEvent, TRANSMIT_TIMEOUT); if (dwWaitResult != WAIT_OBJECT_0) { switch (dwWaitResult) { case WAIT_TIMEOUT: ::SetLastError(WAIT_TIMEOUT); break; case WAIT_ABANDONED: ::SetLastError(WAIT_ABANDONED); break; default: break; } return FALSE; } fSuccess = ::GetOverlappedResult(m_hPipe, lpOverlapped, &cbCurReceived, TRUE); if (!fSuccess && ::GetLastError() != ERROR_IO_PENDING) { break; } } cbCurToReceive -= cbCurReceived; *lpcbReceived += cbCurReceived; lpCurBuffer += cbCurReceived; } XTLTRACE1(TRACE_LEVEL_VERBOSE, "Read %d bytes\n", *lpcbReceived); return fSuccess; }