1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // AdbWinApi.cpp : Implementation of DLL Exports. 18 19 #include "stdafx.h" 20 #include "adb_api.h" 21 #include "adb_winusb_api.h" 22 23 extern "C" { 24 int _forceCRTManifest; 25 int _forceMFCManifest; 26 int _forceAtlDllManifest; 27 }; 28 29 /// References InstantiateWinUsbInterface declared in adb_api.cpp 30 extern PFN_INSTWINUSBINTERFACE InstantiateWinUsbInterface; 31 32 class CAdbWinApiModule : public CAtlDllModuleT< CAdbWinApiModule > { 33 public: 34 CAdbWinApiModule() 35 : CAtlDllModuleT< CAdbWinApiModule >(), 36 adbwinusbapi_handle_(NULL), 37 is_initialized_(false) { 38 } 39 40 ~CAdbWinApiModule() { 41 // Unload AdbWinUsbApi.dll before we exit 42 if (NULL != adbwinusbapi_handle_) { 43 FreeLibrary(adbwinusbapi_handle_); 44 } 45 } 46 47 /** \brief Loads AdbWinUsbApi.dll and caches its InstantiateWinUsbInterface 48 export. 49 50 This method is called from DllMain on DLL_PROCESS_ATTACH event. In this 51 method we will check if WINUSB.DLL required by AdbWinUsbApi.dll is 52 installed, and if it is we will load AdbWinUsbApi.dll and cache address of 53 InstantiateWinUsbInterface routine exported from AdbWinUsbApi.dll 54 */ 55 void AttachToAdbWinUsbApi() { 56 // We only need to run this only once. 57 if (is_initialized_) { 58 return; 59 } 60 61 // Just mark that we have ran initialization. 62 is_initialized_ = true; 63 64 // Before we can load AdbWinUsbApi.dll we must make sure that WINUSB.DLL 65 // has been installed. Build path to the file. 66 wchar_t path_to_winusb_dll[MAX_PATH+1]; 67 if (!GetSystemDirectory(path_to_winusb_dll, MAX_PATH)) { 68 return; 69 } 70 wcscat(path_to_winusb_dll, L"\\WINUSB.DLL"); 71 72 if (0xFFFFFFFF == GetFileAttributes(path_to_winusb_dll)) { 73 // WINUSB.DLL is not installed. We don't (in fact, can't) load 74 // AdbWinUsbApi.dll 75 return; 76 } 77 78 // WINUSB.DLL is installed. Lets load AdbWinUsbApi.dll and cache its 79 // InstantiateWinUsbInterface export. 80 // We require that AdbWinUsbApi.dll is located in the same folder 81 // where AdbWinApi.dll and adb.exe are located, so by Windows 82 // conventions we can pass just module name, and not the full path. 83 adbwinusbapi_handle_ = LoadLibrary(L"AdbWinUsbApi.dll"); 84 if (NULL != adbwinusbapi_handle_) { 85 InstantiateWinUsbInterface = reinterpret_cast<PFN_INSTWINUSBINTERFACE> 86 (GetProcAddress(adbwinusbapi_handle_, "InstantiateWinUsbInterface")); 87 } 88 } 89 90 protected: 91 /// Handle to the loaded AdbWinUsbApi.dll 92 HINSTANCE adbwinusbapi_handle_; 93 94 /// Flags whether or not this module has been initialized. 95 bool is_initialized_; 96 }; 97 98 CAdbWinApiModule _AtlModule; 99 100 // DLL Entry Point 101 extern "C" BOOL WINAPI DllMain(HINSTANCE instance, 102 DWORD reason, 103 LPVOID reserved) { 104 // Lets see if we need to initialize InstantiateWinUsbInterface 105 // variable. We do that only once, on condition that this DLL is 106 // being attached to the process and InstantiateWinUsbInterface 107 // address has not been calculated yet. 108 if (DLL_PROCESS_ATTACH == reason) { 109 _AtlModule.AttachToAdbWinUsbApi(); 110 } 111 return _AtlModule.DllMain(reason, reserved); 112 } 113