Skip to content

Commit 0277ce3

Browse files
authored
Merge pull request #30 from unknownv2/update-build-structure
Update detours library code to latest version
2 parents 36e67dc + 9fc2977 commit 0277ce3

File tree

12 files changed

+323
-86
lines changed

12 files changed

+323
-86
lines changed

README.md

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,26 @@
77

88
For [CoreHook](https://github.com/unknownv2/CoreHook), the [Microsoft Detours](https://github.com/Microsoft/Detours) package serves as a good binary hooking module since it supports x86, x86_64, ARM, and ARM64, while [EasyHook](https://github.com/EasyHook/EasyHook) only supports x86 and x86_64. Since .NET Core supports the two ARM architectures, we can implement the necessary changes to support those architectures for CoreHook.
99

10-
## Supported Platforms
10+
# Supported Platforms
1111

1212
`X86, X64, and ARM`. If you have a *Windows on ARM* device to test `ARM64` with, pull requests and contributions are all welcome!
1313

14-
## Building
14+
# Binary Releases
15+
You can download the pre-built Windows binaries [here](https://github.com/unknownv2/CoreHook.Hooking/releases).
16+
17+
For `x86, x64`, extract the zip corresponding to your target architecture, then place the `corehook32.dll` and/or `corehook64.dll` in the build output directory of your program.
18+
19+
For `ARM, ARM64`, extract the zip corresponding to your target architecture, then place the `corehook32.dll` and/or `corehook64.dll` in the output directory of your published program, created either from using the [Publishing Script](https://github.com/unknownv2/CoreHook#publishing-script) or the `dotnet publish` command.
1520

16-
Building the DLL requires Visual Studio and that can be accomplished by using `cmake` or the tools that come with `Visual Studio`. This can be the `Visual Studio IDE` or `msbuild` within the `Developer Command Prompt`.
21+
# Building
1722

18-
### CMake
23+
Building the DLL requires Visual Studio and that can be accomplished by using `cmake` or the tools that come with `Visual Studio`. This can be the `Visual Studio IDE` or `msbuild` within the `Developer Command Prompt`.
1924

20-
You can also build the library using CMake. You can run the `build/win-vs-2017.cmd` file to build for the `x86` and `x64` architectures. This also gives you the option to generate and build with an older version of `Visual Studio` such as `VS 2015` or `VS 2013`.
25+
## CMake
2126

22-
You can build by running these commands from the root of the repository:
23-
```
24-
cd build
25-
win-vs-2017.cmd
26-
```
27+
You can build the library using CMake by running [`build.cmd`](build.cmd), which builds the library for the `x86` and `x64` architectures. This also gives you the option to generate and build the library with an older version of `Visual Studio` such as `VS 2015` or `VS 2013`.
2728

28-
### Visual Studio
29+
## Visual Studio
2930

3031
You can find the Visual Studio solution inside [the msvc folder](/msvc). You can choose a configuration (**Debug|Release**) and a platform (**X86|X64|ARM|ARM64**) and build.
3132

@@ -43,14 +44,8 @@ nuget restore msvc/corehook.sln
4344
msbuild msvc/corehook.sln /p:Configuration=Release /p:Platform=x64
4445
```
4546

46-
### Binary Releases
47-
You can also download the pre-built Windows binaries [here](https://github.com/unknownv2/CoreHook.Hooking/releases).
48-
49-
For `x86, x64`, extract the zip corresponding to your target architecture, then place the `corehook32.dll` and/or `corehook64.dll` in the build output directory of your program.
50-
51-
For `ARM, ARM64`, extract the zip corresponding to your target architecture, then place the `corehook32.dll` and/or `corehook64.dll` in the output directory of your published program, created either from using the [Publishing Script](https://github.com/unknownv2/CoreHook#publishing-script) or the `dotnet publish` command.
5247

53-
## Usage
48+
# Usage
5449

5550
* For X86, the output directory is `bin/x86` and the output file is `corehook32.dll`.
5651
* For X64, the output directory is `bin/x64` and the output file is `corehook64.dll`.
@@ -60,7 +55,7 @@ msbuild msvc/corehook.sln /p:Configuration=Release /p:Platform=x64
6055
Copy the desired file for your target architecture to the output directory of the program that uses [CoreHook](https://github.com/unknownv2/CoreHook/).
6156

6257

63-
## Credits
58+
# Credits
6459

6560
The hooking module is mostly based on the [EasyHook](https://github.com/EasyHook/EasyHook/blob/master/LICENSE) native module and the [Microsoft Detours](https://github.com/Microsoft/Detours/blob/master/LICENSE.md) library and this library wouldn't be possible without them. They are both MIT-licensed.
6661

build/win-vs-2017.cmd renamed to build.cmd

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cd ../
21
mkdir build32-vs2017
32
mkdir build64-vs2017
43
cd build32-vs2017
@@ -10,5 +9,4 @@ cd ../
109
cmake --build build32-vs2017 --config Debug
1110
cmake --build build32-vs2017 --config Release
1211
cmake --build build64-vs2017 --config Debug
13-
cmake --build build64-vs2017 --config Release
14-
cd build
12+
cmake --build build64-vs2017 --config Release

samples/Makefile

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ all:
3939
@$(MAKE) /NOLOGO /$(MAKEFLAGS)
4040
cd "$(MAKEDIR)\echo"
4141
@$(MAKE) /NOLOGO /$(MAKEFLAGS)
42+
!IF "$(DETOURS_TARGET_PROCESSOR)" != "ARM64"
4243
cd "$(MAKEDIR)\einst"
4344
@$(MAKE) /NOLOGO /$(MAKEFLAGS)
45+
!ENDIF
4446
!IF "$(DETOURS_TARGET_PROCESSOR)" == "X86"
4547
cd "$(MAKEDIR)\excep"
4648
@$(MAKE) /NOLOGO /$(MAKEFLAGS)
@@ -49,9 +51,11 @@ all:
4951
@$(MAKE) /NOLOGO /$(MAKEFLAGS)
5052
cd "$(MAKEDIR)\commem"
5153
@$(MAKE) /NOLOGO /$(MAKEFLAGS)
54+
!IF "$(DETOURS_TARGET_PROCESSOR)" != "ARM64"
5255
cd "$(MAKEDIR)\findfunc"
5356
@$(MAKE) /NOLOGO /$(MAKEFLAGS)
54-
!IF "$(DETOURS_TARGET_PROCESSOR)" != "ARM"
57+
!ENDIF
58+
!IF "$(DETOURS_TARGET_PROCESSOR)" != "ARM" && "$(DETOURS_TARGET_PROCESSOR)" != "ARM64"
5559
cd "$(MAKEDIR)\member"
5660
@$(MAKE) /NOLOGO /$(MAKEFLAGS)
5761
!ENDIF
@@ -77,7 +81,7 @@ all:
7781
@$(MAKE) /NOLOGO /$(MAKEFLAGS)
7882
cd "$(MAKEDIR)\tracelnk"
7983
@$(MAKE) /NOLOGO /$(MAKEFLAGS)
80-
!IF "$(DETOURS_TARGET_PROCESSOR)" != "ARM"
84+
!IF "$(DETOURS_TARGET_PROCESSOR)" != "ARM" && "$(DETOURS_TARGET_PROCESSOR)" != "ARM64"
8185
cd "$(MAKEDIR)\tryman"
8286
@$(MAKE) /NOLOGO /$(MAKEFLAGS)
8387
!ENDIF
@@ -218,34 +222,42 @@ test:
218222
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
219223
cd "$(MAKEDIR)\simple"
220224
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
225+
!IF "$(DETOURS_TARGET_PROCESSOR)" != "ARM64"
221226
cd "$(MAKEDIR)\slept"
222227
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
223228
cd "$(MAKEDIR)\setdll"
224229
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
225230
cd "$(MAKEDIR)\withdll"
226231
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
232+
!ENDIF
227233
!IF "$(DETOURS_TARGET_PROCESSOR)" == "X86"
228234
cd "$(MAKEDIR)\cping"
229235
# @$(MAKE) /NOLOGO /$(MAKEFLAGS) test
230236
!ENDIF
231237
cd "$(MAKEDIR)\disas"
232238
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
239+
!IF "$(DETOURS_TARGET_PROCESSOR)" != "ARM64"
233240
cd "$(MAKEDIR)\dtest"
234241
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
242+
!ENDIF
235243
cd "$(MAKEDIR)\dumpe"
236244
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
237245
cd "$(MAKEDIR)\dumpi"
238246
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
239247
cd "$(MAKEDIR)\echo"
240248
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
249+
!IF "$(DETOURS_TARGET_PROCESSOR)" != "ARM64"
241250
cd "$(MAKEDIR)\einst"
242251
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
252+
!ENDIF
243253
!IF "$(DETOURS_TARGET_PROCESSOR)" == "X86"
244254
cd "$(MAKEDIR)\excep"
245255
# @$(MAKE) /NOLOGO /$(MAKEFLAGS) test
246256
!ENDIF
257+
!IF "$(DETOURS_TARGET_PROCESSOR)" != "ARM64"
247258
cd "$(MAKEDIR)\comeasy"
248259
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
260+
249261
cd "$(MAKEDIR)\commem"
250262
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
251263
cd "$(MAKEDIR)\findfunc"
@@ -254,10 +266,12 @@ test:
254266
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
255267
cd "$(MAKEDIR)\region"
256268
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
269+
!ENDIF
257270
!IF "$(DETOURS_TARGET_PROCESSOR)" == "X64" || "$(DETOURS_TARGET_PROCESSOR)" == "IA64"
258271
cd "$(MAKEDIR)\talloc"
259272
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
260273
!ENDIF
274+
!IF "$(DETOURS_TARGET_PROCESSOR)" != "ARM64"
261275
cd "$(MAKEDIR)\traceapi"
262276
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
263277
cd "$(MAKEDIR)\tracebld"
@@ -268,12 +282,15 @@ test:
268282
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
269283
cd "$(MAKEDIR)\traceser"
270284
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
285+
!ENDIF
271286
# cd "$(MAKEDIR)\tracessl"
272287
# @$(MAKE) /NOLOGO /$(MAKEFLAGS) test
273288
# cd "$(MAKEDIR)\tracetcp"
274289
# @$(MAKE) /NOLOGO /$(MAKEFLAGS) test
290+
!IF "$(DETOURS_TARGET_PROCESSOR)" != "ARM64"
275291
cd "$(MAKEDIR)\tracelnk"
276292
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
293+
!ENDIF
277294
cd "$(MAKEDIR)\impmunge"
278295
@$(MAKE) /NOLOGO /$(MAKEFLAGS) test
279296
cd "$(MAKEDIR)"

samples/cping/cping.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ STDAPI PingMessage(PCSTR msg, ...)
115115

116116
va_start(args, msg);
117117
hr = StringCchVPrintfA(s_szMessageBuf, ARRAYSIZE(s_szMessageBuf), msg, args);
118+
va_end(args);
118119
if (FAILED(hr)) {
119120
return hr;
120121
}
@@ -140,6 +141,7 @@ BOOLEAN CheckResult(HRESULT hr, PCSTR pszMsg, ...)
140141

141142
va_start(args, pszMsg);
142143
ihr = StringCchVPrintfA(s_szMessageBuf, ARRAYSIZE(s_szMessageBuf), pszMsg, args);
144+
va_end(args);
143145
if (FAILED(ihr)) {
144146
return FALSE;
145147
}
@@ -1410,10 +1412,9 @@ HRESULT CSampleRecord::Measure(IPing *pIPing, LONG cbToClient, LONG cbToServer)
14101412
hr = Catch_IPing_PingToClient(pIPing, &pszString);
14111413
llEnd = GetTimeStamp();
14121414

1413-
LONG cb = (LONG)strlen(pszString) + 1;
1414-
ASSERT(cb == cbToClient);
1415-
14161415
if (pszString) {
1416+
LONG cb = (LONG)strlen(pszString) + 1;
1417+
ASSERT(cb == cbToClient);
14171418
CoTaskMemFree(pszString);
14181419
pszString = NULL;
14191420
}

samples/einst/Makefile

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@
99

1010
!include ..\common.mak
1111

12+
# ARM64 does not like base addresses below 4GB.
13+
# Append two extra zeros for it.
14+
#
15+
!if "$(DETOURS_TARGET_PROCESSOR)" == "ARM64"
16+
EDLL1X_BASE=0x710000000
17+
EDLL2X_BASE=0x720000000
18+
EDLL3X_BASE=0x730000000
19+
!else
20+
EDLL1X_BASE=0x7100000
21+
EDLL2X_BASE=0x7200000
22+
EDLL3X_BASE=0x7300000
23+
!endif
24+
1225
LIBS=$(LIBS) kernel32.lib user32.lib
1326

1427
all: dirs \
@@ -60,7 +73,7 @@ $(BIND)\edll1x$(DETOURS_BITS).dll : $(OBJD)\edll1x.obj $(DEPS)
6073
$(OBJD)\edll1x.obj /LD \
6174
/link $(LINKFLAGS) $(LIBS) \
6275
/subsystem:windows \
63-
/base:0x7100000
76+
/base:$(EDLL1X_BASE)
6477

6578
$(OBJD)\edll1x$(DETOURS_BITS).bsc : $(OBJD)\edll1x.obj
6679
bscmake /v /n /o $@ $(OBJD)\edll1x.sbr
@@ -72,7 +85,7 @@ $(BIND)\edll2x$(DETOURS_BITS).dll : $(OBJD)\edll2x.obj $(DEPS)
7285
$(OBJD)\edll2x.obj /LD \
7386
/link $(LINKFLAGS) $(LIBS) \
7487
/subsystem:console \
75-
/base:0x7200000
88+
/base:$(EDLL2X_BASE)
7689

7790
$(OBJD)\edll2x$(DETOURS_BITS).bsc : $(OBJD)\edll2x.obj
7891
bscmake /v /n /o $@ $(OBJD)\edll2x.sbr
@@ -84,7 +97,7 @@ $(BIND)\edll3x$(DETOURS_BITS).dll : $(OBJD)\edll3x.obj $(DEPS)
8497
$(OBJD)\edll3x.obj /LD \
8598
/link $(LINKFLAGS) $(LIBS) \
8699
/subsystem:console \
87-
/base:0x7300000
100+
/base:$(EDLL3X_BASE)
88101

89102
$(OBJD)\edll3x$(DETOURS_BITS).bsc : $(OBJD)\edll3x.obj
90103
bscmake /v /n /o $@ $(OBJD)\edll3x.sbr

samples/findfunc/Makefile

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99

1010
!include ..\common.mak
1111

12+
# ARM64 does not like base addresses below 4GB.
13+
# Append two extra zeros for it.
14+
#
15+
!if "$(DETOURS_TARGET_PROCESSOR)" == "ARM64"
16+
TARGET_BASE=0x190000000
17+
EXTEND_BASE=0x1a0000000
18+
!else
19+
TARGET_BASE=0x1900000
20+
EXTEND_BASE=0x1a00000
21+
!endif
22+
1223
LIBS=$(LIBS) kernel32.lib
1324

1425
##############################################################################
@@ -44,7 +55,7 @@ $(BIND)\target$(DETOURS_BITS).dll $(BIND)\target$(DETOURS_BITS).lib: \
4455
$(OBJD)\target.obj $(OBJD)\target.res \
4556
/link $(LINKFLAGS) /subsystem:console \
4657
/export:Target \
47-
/base:0x1900000 \
58+
/base:$(TARGET_BASE) \
4859
$(LIBS)
4960

5061
$(OBJD)\target$(DETOURS_BITS).bsc : $(OBJD)\target.obj
@@ -60,7 +71,7 @@ $(BIND)\extend$(DETOURS_BITS).dll $(BIND)\extend$(DETOURS_BITS).lib: \
6071
$(OBJD)\extend.obj $(OBJD)\extend.res \
6172
/link $(LINKFLAGS) /subsystem:console \
6273
/export:DetourFinishHelperProcess,@1,NONAME \
63-
/base:0x1a00000 \
74+
/base:$(EXTEND_BASE) \
6475
$(LIBS)
6576

6677
$(OBJD)\extend$(DETOURS_BITS).bsc : $(OBJD)\extend.obj

samples/slept/slept.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ DWORD WINAPI TestTicksEx(DWORD Add)
6767

6868
Add = pdw[Add] - Add;
6969

70-
delete pdw;
70+
delete [] pdw;
7171
}
7272
else {
7373
Add = dwSlept + Add;

samples/traceapi/_win32.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12741,18 +12741,18 @@ BOOL __stdcall Mine_CreateProcessA(LPCSTR lpApplicationName,
1274112741

1274212742
BOOL rv = 0;
1274312743
__try {
12744-
rv = DetourCreateProcessWithDllA(lpApplicationName,
12745-
lpCommandLine,
12746-
lpProcessAttributes,
12747-
lpThreadAttributes,
12748-
bInheritHandles,
12749-
dwCreationFlags,
12750-
lpEnvironment,
12751-
lpCurrentDirectory,
12752-
lpStartupInfo,
12753-
lpProcessInformation,
12754-
s_szDllPath,
12755-
Real_CreateProcessA);
12744+
rv = DetourCreateProcessWithDllExA(lpApplicationName,
12745+
lpCommandLine,
12746+
lpProcessAttributes,
12747+
lpThreadAttributes,
12748+
bInheritHandles,
12749+
dwCreationFlags,
12750+
lpEnvironment,
12751+
lpCurrentDirectory,
12752+
lpStartupInfo,
12753+
lpProcessInformation,
12754+
s_szDllPath,
12755+
Real_CreateProcessA);
1275612756
} __finally {
1275712757
_PrintExit("CreateProcessA(,,,,,,,,,) -> %x (proc:%d/%p, thrd:%d/%p\n", rv,
1275812758
lpProcessInformation->dwProcessId,
@@ -12794,18 +12794,18 @@ BOOL __stdcall Mine_CreateProcessW(LPCWSTR lpApplicationName,
1279412794

1279512795
BOOL rv = 0;
1279612796
__try {
12797-
rv = DetourCreateProcessWithDllW(lpApplicationName,
12798-
lpCommandLine,
12799-
lpProcessAttributes,
12800-
lpThreadAttributes,
12801-
bInheritHandles,
12802-
dwCreationFlags,
12803-
lpEnvironment,
12804-
lpCurrentDirectory,
12805-
lpStartupInfo,
12806-
lpProcessInformation,
12807-
s_szDllPath,
12808-
Real_CreateProcessW);
12797+
rv = DetourCreateProcessWithDllExW(lpApplicationName,
12798+
lpCommandLine,
12799+
lpProcessAttributes,
12800+
lpThreadAttributes,
12801+
bInheritHandles,
12802+
dwCreationFlags,
12803+
lpEnvironment,
12804+
lpCurrentDirectory,
12805+
lpStartupInfo,
12806+
lpProcessInformation,
12807+
s_szDllPath,
12808+
Real_CreateProcessW);
1280912809
} __finally {
1281012810
_PrintExit("CreateProcessW(,,,,,,,,,) -> %x (proc:%d/%p, thrd:%d/%p\n", rv,
1281112811
lpProcessInformation->dwProcessId,

0 commit comments

Comments
 (0)