//----------------------------------------------------------------------------- // // Initialize/Deinitialize Functions // //----------------------------------------------------------------------------- DWORD Initialize() { TCHAR dir[MAX_PATH]; TCHAR *ptr; GetModuleFileName(gModule, dir, MAX_PATH); if((ptr = _tcsrchr(dir, '\\')) != NULL) { *ptr = '\0'; } wsprintf(gDriverPath, _T("%s\\%s"), dir, gDriverFileName); if(IsFileExist(gDriverPath) == FALSE) { return OLS_DLL_DRIVER_NOT_FOUND; } if(IsOnNetworkDrive(gDriverPath) == TRUE) { return OLS_DLL_DRIVER_NOT_LOADED_ON_NETWORK; } if(gIsNT) { if(OpenDriver()) { return OLS_DLL_NO_ERROR; } ManageDriver(OLS_DRIVER_ID, gDriverPath, OLS_DRIVER_REMOVE); if(!ManageDriver(OLS_DRIVER_ID, gDriverPath, OLS_DRIVER_INSTALL)) { ManageDriver(OLS_DRIVER_ID, gDriverPath, OLS_DRIVER_REMOVE); return OLS_DLL_DRIVER_NOT_LOADED; } if(OpenDriver()) { return OLS_DLL_NO_ERROR; } return OLS_DLL_DRIVER_NOT_LOADED; } else { gHandle = CreateFile( _T("\\\\.\\") OLS_DRIVER_FILE_NAME_WIN_9X, 0, 0, NULL, 0, FILE_FLAG_DELETE_ON_CLOSE, NULL); if(gHandle == INVALID_HANDLE_VALUE) { return OLS_DLL_DRIVER_NOT_LOADED; } return OLS_DLL_NO_ERROR; } }
VOID WINAPI DeinitializeOls() { if(gInitDll == TRUE) { if(gIsNT && GetRefCount() == 1) { CloseHandle(gHandle); gHandle = INVALID_HANDLE_VALUE; ManageDriver(OLS_DRIVER_ID, gDriverPath, OLS_DRIVER_REMOVE); } if(gHandle != INVALID_HANDLE_VALUE) { CloseHandle(gHandle); gHandle = INVALID_HANDLE_VALUE; } gInitDll = FALSE; } }
int _cdecl main(int argc, LPCTSTR argv[]) { HANDLE hDevice; // handle to a device, file, or directory DWORD dwError = ERROR_SUCCESS; LPVOID lpFileName = _T("\\\\.\\TraceKmp") ; TCHAR driverLocation[MAX_PATH]; DWORD dwOutBuffer[2048]; DWORD dwOutBufferCount ; int ch; UNREFERENCED_PARAMETER(argc); UNREFERENCED_PARAMETER(argv); if ((hDevice = CreateFile( lpFileName, // pointer to name of the file 0, // access (read-write) mode 0, // share mode NULL, // pointer to security attributes OPEN_EXISTING, // how to create FILE_ATTRIBUTE_NORMAL, // file attributes NULL // handle to file with attributes to // copy )) == INVALID_HANDLE_VALUE) { dwError = GetLastError(); if ( dwError != ERROR_FILE_NOT_FOUND ) { _tprintf(_T("CreateFile failed ! error: %d\n"), dwError); return 1; } // // Setup full path to driver name // if (!SetupDriverName(driverLocation)) { return 2; } // // Install driver // if (!ManageDriver(DRIVER_NAME, driverLocation, DRIVER_FUNC_INSTALL )) { _tprintf(_T("Unable to install driver. \n")); // // Error - remove driver. // ManageDriver(DRIVER_NAME, driverLocation, DRIVER_FUNC_REMOVE ); return 3; } if ((hDevice = CreateFile( lpFileName, // pointer to name of the file 0, // access (read-write) mode 0, // share mode NULL, // pointer to security attributes OPEN_EXISTING, // how to create FILE_ATTRIBUTE_NORMAL, // file attributes NULL // handle to file with attributes to // copy )) == INVALID_HANDLE_VALUE) { _tprintf(_T("Error: CreateFile failed\n")); return 4; } } _tprintf(_T("\nPress 'q' to exit, any other key to send ioctl...\n")); fflush(stdin); ch = _getche(); while(tolower(ch) != 'q' ) { _tprintf(_T("Making TRACEKMP_TRACE_EVENT ioctl to log events\n")); if (DeviceIoControl( hDevice, // handle to a device, file, or directory IOCTL_TRACEKMP_TRACE_EVENT, // control code of operation to perform NULL, // pointer to buffer to supply input data 0, // size, in bytes, of input buffer dwOutBuffer, // pointer to buffer to receive output data 2048, // size, in bytes, of output buffer &dwOutBufferCount, // pointer to variable to receive byte count NULL // pointer to structure for asynchronous operation ) == 0) { _tprintf(_T("DeviceIOControl Failed %d\n"),GetLastError()); return 5; } ch = _getche(); } if (CloseHandle(hDevice) == 0) { _tprintf(_T("CloseHandle Failed %d\n"),GetLastError()); return 6; } // // stop the driver // ManageDriver(DRIVER_NAME, driverLocation, DRIVER_FUNC_REMOVE ); _tprintf(_T("Driver '%s' is removed\n"), DRIVER_NAME); return 0; }