📄 withdll.cpp
字号:
//////////////////////////////////////////////////////////////////////////////
//
// Module: cpwth.exe - Test CreateProcessWithDll function.
// File: cpwth.cpp
// Author: Galen C. Hunt
// Copyright 1998-1999, Microsoft Corporation
//
// http://www.research.microsoft.com/sn/detours
//
#include <stdio.h>
#include <windows.h>
#include <detours.h>
#define arrayof(x) (sizeof(x)/sizeof(x[0]))
//////////////////////////////////////////////////////////////////////////////
//
void PrintUsage(void)
{
printf("Usage:\n"
" withdll [options] [command line]\n"
"Options:\n"
" /d:file.dll : Start the process with file.dll.\n"
" /? : This help screen.\n");
}
//////////////////////////////////////////////////////////////////////// main.
//
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR lpszCmdLine, int nCmdShow)
{
INT argc;
WCHAR **argv;
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
BOOLEAN fNeedHelp = FALSE;
BOOLEAN fVerbose = FALSE;
WCHAR wzDllPath[MAX_PATH] = L"";
for (int arg = 1;
arg < argc && (argv[arg][0] == '-' || argv[arg][0] == '/');
arg++) {
WCHAR *argp = argv[arg] + 2;
if (*argp = ':')
argp++;
switch (argv[arg][1]) {
case 'd': // Set DLL Name
case 'D':
wcscpy(wzDllPath, argp);
break;
case 'h': // Help
case 'H':
case '?':
fNeedHelp = TRUE;
break;
default:
fNeedHelp = TRUE;
printf("Bad argument: %ls\n", argv[arg]);
break;
}
}
if (wzDllPath[0] == 0) {
fNeedHelp = TRUE;
}
if (arg >= argc) {
fNeedHelp = TRUE;
}
if (fNeedHelp) {
PrintUsage();
return 1;
}
//////////////////////////////////////////////////////////////////////////
STARTUPINFOW si;
PROCESS_INFORMATION pi;
WCHAR wzCommand[2048];
WCHAR wzExe[1024];
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
si.cb = sizeof(si);
wzCommand[0] = L'\0';
wcscpy(wzExe, argv[arg]);
for (; arg < argc; arg++) {
if (wcschr(argv[arg], ' ') != NULL || wcschr(argv[arg], '\t') != NULL) {
wcscat(wzCommand, L"\"");
wcscat(wzCommand, argv[arg]);
wcscat(wzCommand, L"\"");
}
else {
wcscat(wzCommand, argv[arg]);
}
if (arg + 1 < argc)
wcscat(wzCommand, L" ");
}
printf("Loading: `%ls'\n\n", wzCommand);
DWORD dwCreationFlags = (CREATE_DEFAULT_ERROR_MODE);
SetLastError(0);
WCHAR wzFullExe[1024] = L"\0";
PWCHAR pwzFileExe = NULL;
SearchPathW(NULL, wzExe, L".exe", arrayof(wzFullExe), wzFullExe, &pwzFileExe);
if (wzFullExe[0] == 0) {
printf("Executable: %ls\n", wzFullExe);
}
if (wzFullExe[0] != '\0') {
if (!CreateProcessWithDllW(wzFullExe, wzCommand,
NULL, NULL, TRUE, dwCreationFlags, NULL, NULL,
&si, &pi, wzDllPath, NULL)) {
printf("CreateProcessWithDll failed: %d", GetLastError());
return 2;
}
}
else {
if (!CreateProcessWithDllW(NULL, wzCommand, NULL, NULL, TRUE, dwCreationFlags,
NULL, NULL, &si, &pi, wzDllPath, NULL)) {
printf("CreateProcessWithDll failed: %d", GetLastError());
return 2;
}
}
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD dwResult = 0;
if (!GetExitCodeProcess(pi.hProcess, &dwResult)) {
printf("GetExitCodeProcess failed: %d", GetLastError());
dwResult = 3;
}
return dwResult;
}
//////////////////////////////////////////////////////////////////////////////
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -