vchtchetkine | dceaaa5 | 2009-07-22 13:34:53 -0700 | [diff] [blame] | 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"
|
vchtchetkine | f855c4e | 2009-08-05 16:57:18 -0700 | [diff] [blame^] | 20 | #include "adb_api.h"
|
vchtchetkine | dceaaa5 | 2009-07-22 13:34:53 -0700 | [diff] [blame] | 21 |
|
| 22 | extern "C" {
|
| 23 | int _forceCRTManifest;
|
| 24 | int _forceMFCManifest;
|
| 25 | int _forceAtlDllManifest;
|
| 26 | };
|
| 27 |
|
vchtchetkine | f855c4e | 2009-08-05 16:57:18 -0700 | [diff] [blame^] | 28 | /// References InstantiateWinUsbInterface declared in adb_api.cpp
|
| 29 | extern PFN_INSTWINUSBINTERFACE InstantiateWinUsbInterface;
|
| 30 |
|
vchtchetkine | dceaaa5 | 2009-07-22 13:34:53 -0700 | [diff] [blame] | 31 | class CAdbWinApiModule : public CAtlDllModuleT< CAdbWinApiModule > {
|
vchtchetkine | f855c4e | 2009-08-05 16:57:18 -0700 | [diff] [blame^] | 32 | public:
|
| 33 | CAdbWinApiModule()
|
| 34 | : CAtlDllModuleT< CAdbWinApiModule >(),
|
| 35 | adbwinusbapi_handle_(NULL),
|
| 36 | is_initialized_(false) {
|
| 37 | }
|
| 38 |
|
| 39 | ~CAdbWinApiModule() {
|
| 40 | // Unload AdbWinUsbApi.dll before we exit
|
| 41 | if (NULL != adbwinusbapi_handle_) {
|
| 42 | FreeLibrary(adbwinusbapi_handle_);
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | /** \brief Loads AdbWinUsbApi.dll and caches its InstantiateWinUsbInterface
|
| 47 | export.
|
| 48 |
|
| 49 | This method is called from DllMain on DLL_PROCESS_ATTACH event. In this
|
| 50 | method we will check if WINUSB.DLL required by AdbWinUsbApi.dll is
|
| 51 | installed, and if it is we will load AdbWinUsbApi.dll and cache address of
|
| 52 | InstantiateWinUsbInterface routine exported from AdbWinUsbApi.dll
|
| 53 | */
|
| 54 | void AttachToAdbWinUsbApi() {
|
| 55 | // We only need to run this only once.
|
| 56 | if (is_initialized_) {
|
| 57 | return;
|
| 58 | }
|
| 59 |
|
| 60 | // Just mark that we have ran initialization.
|
| 61 | is_initialized_ = true;
|
| 62 |
|
| 63 | // Before we can load AdbWinUsbApi.dll we must make sure that WINUSB.DLL
|
| 64 | // has been installed. Build path to the file.
|
| 65 | wchar_t path_to_winusb_dll[MAX_PATH+1];
|
| 66 | if (!GetSystemDirectory(path_to_winusb_dll, MAX_PATH)) {
|
| 67 | return;
|
| 68 | }
|
| 69 | wcscat(path_to_winusb_dll, L"\\WINUSB.DLL");
|
| 70 |
|
| 71 | if (0xFFFFFFFF == GetFileAttributes(path_to_winusb_dll)) {
|
| 72 | // WINUSB.DLL is not installed. We don't (in fact, can't) load
|
| 73 | // AdbWinUsbApi.dll
|
| 74 | return;
|
| 75 | }
|
| 76 |
|
| 77 | // WINUSB.DLL is installed. Lets load AdbWinUsbApi.dll and cache its
|
| 78 | // InstantiateWinUsbInterface export.
|
| 79 | // We require that AdbWinUsbApi.dll is located in the same folder
|
| 80 | // where AdbWinApi.dll and adb.exe are located, so by Windows
|
| 81 | // conventions we can pass just module name, and not the full path.
|
| 82 | adbwinusbapi_handle_ = LoadLibrary(L"AdbWinUsbApi.dll");
|
| 83 | if (NULL != adbwinusbapi_handle_) {
|
| 84 | InstantiateWinUsbInterface = reinterpret_cast<PFN_INSTWINUSBINTERFACE>
|
| 85 | (GetProcAddress(adbwinusbapi_handle_, "InstantiateWinUsbInterface"));
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | protected:
|
| 90 | /// Handle to the loaded AdbWinUsbApi.dll
|
| 91 | HINSTANCE adbwinusbapi_handle_;
|
| 92 |
|
| 93 | /// Flags whether or not this module has been initialized.
|
| 94 | bool is_initialized_;
|
vchtchetkine | dceaaa5 | 2009-07-22 13:34:53 -0700 | [diff] [blame] | 95 | };
|
| 96 |
|
| 97 | CAdbWinApiModule _AtlModule;
|
| 98 |
|
| 99 | // DLL Entry Point
|
| 100 | extern "C" BOOL WINAPI DllMain(HINSTANCE instance,
|
| 101 | DWORD reason,
|
| 102 | LPVOID reserved) {
|
vchtchetkine | f855c4e | 2009-08-05 16:57:18 -0700 | [diff] [blame^] | 103 | // Lets see if we need to initialize InstantiateWinUsbInterface
|
| 104 | // variable. We do that only once, on condition that this DLL is
|
| 105 | // being attached to the process and InstantiateWinUsbInterface
|
| 106 | // address has not been calculated yet.
|
| 107 | if (DLL_PROCESS_ATTACH == reason) {
|
| 108 | _AtlModule.AttachToAdbWinUsbApi();
|
| 109 | }
|
| 110 | return _AtlModule.DllMain(reason, reserved);
|
vchtchetkine | dceaaa5 | 2009-07-22 13:34:53 -0700 | [diff] [blame] | 111 | }
|