blob: e81c2c7ea1538f68e31c2ee2a85976c7b8b874c1 [file] [log] [blame]
vchtchetkinedceaaa52009-07-22 13:34:53 -07001/*
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"
vchtchetkinef855c4e2009-08-05 16:57:18 -070020#include "adb_api.h"
vchtchetkinedceaaa52009-07-22 13:34:53 -070021
22extern "C" {
23int _forceCRTManifest;
24int _forceMFCManifest;
25int _forceAtlDllManifest;
26};
27
vchtchetkinef855c4e2009-08-05 16:57:18 -070028/// References InstantiateWinUsbInterface declared in adb_api.cpp
29extern PFN_INSTWINUSBINTERFACE InstantiateWinUsbInterface;
30
vchtchetkinedceaaa52009-07-22 13:34:53 -070031class CAdbWinApiModule : public CAtlDllModuleT< CAdbWinApiModule > {
vchtchetkinef855c4e2009-08-05 16:57:18 -070032 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_;
vchtchetkinedceaaa52009-07-22 13:34:53 -070095};
96
97CAdbWinApiModule _AtlModule;
98
99// DLL Entry Point
100extern "C" BOOL WINAPI DllMain(HINSTANCE instance,
101 DWORD reason,
102 LPVOID reserved) {
vchtchetkinef855c4e2009-08-05 16:57:18 -0700103 // 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);
vchtchetkinedceaaa52009-07-22 13:34:53 -0700111}