]> git.cworth.org Git - apitrace/blob - inject/inject.h
inject: Add define to use environment var instead of shared memory.
[apitrace] / inject / inject.h
1 /**************************************************************************
2  *
3  * Copyright 2011-2012 Jose Fonseca
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 /*
28  * Code for the DLL that will be injected in the target process.
29  *
30  * The injected DLL will manipulate the import tables to hook the
31  * modules/functions of interest.
32  *
33  * See also:
34  * - http://www.codeproject.com/KB/system/api_spying_hack.aspx
35  * - http://www.codeproject.com/KB/threads/APIHooking.aspx
36  * - http://msdn.microsoft.com/en-us/magazine/cc301808.aspx
37  */
38
39
40 #include <windows.h>
41
42
43 static inline const char *
44 getSeparator(const char *szFilename) {
45     const char *p, *q;
46     p = NULL;
47     q = szFilename;
48     char c;
49     do  {
50         c = *q++;
51         if (c == '\\' || c == '/' || c == ':') {
52             p = q;
53         }
54     } while (c);
55     return p;
56 }
57
58
59 static inline const char *
60 getBaseName(const char *szFilename) {
61     const char *pSeparator = getSeparator(szFilename);
62     if (!pSeparator) {
63         return szFilename;
64     }
65     return pSeparator;
66 }
67
68
69 static inline void
70 getDirName(char *szFilename) {
71     char *pSeparator = const_cast<char *>(getSeparator(szFilename));
72     if (pSeparator) {
73         *pSeparator = '\0';
74     }
75 }
76
77
78 static inline void
79 getModuleName(char *szModuleName, size_t n, const char *szFilename) {
80     char *p = szModuleName;
81     const char *q = getBaseName(szFilename);
82     char c;
83     while (--n) {
84         c = *q++;
85         if (c == '.' || c == '\0') {
86             break;
87         }
88         *p++ = c;
89     };
90     *p++ = '\0';
91 }
92
93
94 #define USE_SHARED_MEM 1
95
96
97 #if USE_SHARED_MEM
98
99 #define SHARED_MEM_SIZE 4096
100
101 static LPVOID pSharedMem = NULL;
102 static HANDLE hFileMapping = NULL;
103
104
105 static LPSTR
106 OpenSharedMemory(void) {
107     if (pSharedMem) {
108         return (LPSTR)pSharedMem;
109     }
110
111     hFileMapping = CreateFileMapping(
112         INVALID_HANDLE_VALUE,   // system paging file
113         NULL,                   // lpAttributes
114         PAGE_READWRITE,         // read/write access
115         0,                      // dwMaximumSizeHigh
116         SHARED_MEM_SIZE,              // dwMaximumSizeLow
117         TEXT("injectfilemap")); // name of map object
118     if (hFileMapping == NULL) {
119         fprintf(stderr, "Failed to create file mapping\n");
120         return NULL;
121     }
122
123     BOOL bAlreadyExists = (GetLastError() == ERROR_ALREADY_EXISTS);
124
125     pSharedMem = MapViewOfFile(
126         hFileMapping,
127         FILE_MAP_WRITE, // read/write access
128         0,              // dwFileOffsetHigh
129         0,              // dwFileOffsetLow
130         0);             // dwNumberOfBytesToMap (entire file)
131     if (pSharedMem == NULL) {
132         fprintf(stderr, "Failed to map view \n");
133         return NULL;
134     }
135
136     if (!bAlreadyExists) {
137         memset(pSharedMem, 0, SHARED_MEM_SIZE);
138     }
139
140     return (LPSTR)pSharedMem;
141 }
142
143
144 static inline VOID
145 CloseSharedMem(void) {
146     if (!pSharedMem) {
147         return;
148     }
149
150     UnmapViewOfFile(pSharedMem);
151     pSharedMem = NULL;
152
153     CloseHandle(hFileMapping);
154     hFileMapping = NULL;
155 }
156
157
158 static inline VOID
159 SetSharedMem(LPCSTR lpszSrc) {
160     LPSTR lpszDst = OpenSharedMemory();
161     if (!lpszDst) {
162         return;
163     }
164
165     size_t n = 1;
166     while (*lpszSrc && n < SHARED_MEM_SIZE) {
167         *lpszDst++ = *lpszSrc++;
168         n++;
169     }
170     *lpszDst = '\0';
171 }
172
173
174 static inline VOID
175 GetSharedMem(LPSTR lpszDst, size_t n) {
176     LPCSTR lpszSrc = OpenSharedMemory();
177     if (!lpszSrc) {
178         return;
179     }
180
181     while (*lpszSrc && --n) {
182         *lpszDst++ = *lpszSrc++;
183     }
184     *lpszDst = '\0';
185 }
186
187
188 #endif /* USE_SHARED_MEM */