I have implemented a DLL for hooking functions in a target process using Microsoft Detours. However, when I run withdll.exe
, which comes with Detours, I get the following error:
B:>withdll.exe /d:[...]\IniHook.dll HookTarget.exe withdll.exe: Error: [...]\IniHook.dll does not export ordinal #1. See help entry DetourCreateProcessWithDllEx in Detours.chm.
However, I can't find any Detours.chm
in order to look up what to do.
What could the problem be, and how to fix it?
My hook code is
#include "pch.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <detours.h>
#include <iostream>
#pragma comment(lib, "detours.lib")
typedef DWORD(WINAPI* GetPrivateProfileStringW_t)(LPCWSTR app, LPCWSTR key, LPCWSTR def, LPWSTR result, DWORD size, LPCWSTR file_name);
GetPrivateProfileStringW_t TrueGetPrivateProfileStringW = nullptr;
DWORD WINAPI HookedGetPrivateProfileStringW(LPCWSTR app, LPCWSTR key, LPCWSTR def, LPWSTR result, DWORD size, LPCWSTR file_name)
{
std::wcout << L"GetPrivateProfileStringW called." << L"\n";
return TrueGetPrivateProfileStringW(app, key, def, result, size, file_name);
}
BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID _)
{
switch (reason)
{
case DLL_THREAD_ATTACH:
case DLL_PROCESS_ATTACH:
DetourTransactionBegin();
TrueGetPrivateProfileStringW = (GetPrivateProfileStringW_t)GetProcAddress(GetModuleHandleA("kernel32.dll"), "GetPrivateProfileStringW");
DetourAttach(&(PVOID&)TrueGetPrivateProfileStringW, HookedGetPrivateProfileStringW);
DetourTransactionCommit();
break;
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
DetourTransactionBegin();
DetourDetach(&(PVOID&)TrueGetPrivateProfileStringW, HookedGetPrivateProfileStringW);
DetourTransactionCommit();
break;
}
return TRUE;
}
I have implemented a DLL for hooking functions in a target process using Microsoft Detours. However, when I run withdll.exe
, which comes with Detours, I get the following error:
B:>withdll.exe /d:[...]\IniHook.dll HookTarget.exe withdll.exe: Error: [...]\IniHook.dll does not export ordinal #1. See help entry DetourCreateProcessWithDllEx in Detours.chm.
However, I can't find any Detours.chm
in order to look up what to do.
What could the problem be, and how to fix it?
My hook code is
#include "pch.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <detours.h>
#include <iostream>
#pragma comment(lib, "detours.lib")
typedef DWORD(WINAPI* GetPrivateProfileStringW_t)(LPCWSTR app, LPCWSTR key, LPCWSTR def, LPWSTR result, DWORD size, LPCWSTR file_name);
GetPrivateProfileStringW_t TrueGetPrivateProfileStringW = nullptr;
DWORD WINAPI HookedGetPrivateProfileStringW(LPCWSTR app, LPCWSTR key, LPCWSTR def, LPWSTR result, DWORD size, LPCWSTR file_name)
{
std::wcout << L"GetPrivateProfileStringW called." << L"\n";
return TrueGetPrivateProfileStringW(app, key, def, result, size, file_name);
}
BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID _)
{
switch (reason)
{
case DLL_THREAD_ATTACH:
case DLL_PROCESS_ATTACH:
DetourTransactionBegin();
TrueGetPrivateProfileStringW = (GetPrivateProfileStringW_t)GetProcAddress(GetModuleHandleA("kernel32.dll"), "GetPrivateProfileStringW");
DetourAttach(&(PVOID&)TrueGetPrivateProfileStringW, HookedGetPrivateProfileStringW);
DetourTransactionCommit();
break;
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
DetourTransactionBegin();
DetourDetach(&(PVOID&)TrueGetPrivateProfileStringW, HookedGetPrivateProfileStringW);
DetourTransactionCommit();
break;
}
return TRUE;
}
Share
Improve this question
edited Jan 18 at 18:19
Thomas Weller
asked Jan 18 at 15:53
Thomas WellerThomas Weller
59.7k23 gold badges137 silver badges253 bronze badges
6
|
Show 1 more comment
1 Answer
Reset to default 1The code does not export DllMain
, so withdll.exe
can't find it.
Change the code to:
__declspec(dllexport)
BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID _)
{
switch (reason)
[...]
DetourUpdateThread(GetCurrentThread())
senseless code.TrueGetPrivateProfileStringW
- with__imp_GetPrivateProfileStringW
code was more native – RbMm Commented Jan 18 at 18:11EXTERN_C extern PVOID __imp_GetPrivateProfileStringW;
and callGetPrivateProfileStringW
in native way. Also will be not need useGetProcAddress
in this case for intit pointer. You can simply import it ( in some case possible better not unconditionally import some api, but not in concrete case ). – RbMm Commented Jan 18 at 18:28TrueGetPrivateProfileStringW
also correct, but here you need not native names, typedefs, etc. also about #1 ordinal is documented. Dll simply must export it. It never will be called. But it need for load dll – RbMm Commented Jan 18 at 18:31