|
发表于 2007-5-30 10:52:33| 字数 2,264| - 中国–陕西–西安 电信
|
显示全部楼层
没贴代码是因为代码实在很简单. 从 detours 的例子改的.- [font="Courier New"]#include <windows.h>
- #include "detours.h"
- /*
- cl /LD /nologo /Zi /MT /Gm- /W4 /WX /I../../include /O1 accessfile.cpp /link /incremental:no /release /subsystem:windows /entry:_DllMainCRTStartup@12 /export:CreateFileW_ ../../lib/detours.lib ../../lib/detours.lib user32.lib
- */
- static HANDLE (WINAPI *OriCreateFileW)
- (LPCWSTR lpFileName,
- DWORD dwDesiredAccess,
- DWORD dwShareMode,
- LPSECURITY_ATTRIBUTES lpSecurityAttributes,
- DWORD dwCreationDisposition,
- DWORD dwFlagsAndAttributes,
- HANDLE hTemplateFile) = CreateFileW;
- WCHAR lpwDocName[MAX_PATH] = {0};
- HANDLE WINAPI CreateFileW_
- (LPCWSTR lpFileName,
- DWORD dwDesiredAccess,
- DWORD dwShareMode,
- LPSECURITY_ATTRIBUTES lpSecurityAttributes,
- DWORD dwCreationDisposition,
- DWORD dwFlagsAndAttributes,
- HANDLE hTemplateFile)
- {
- if (wcslen(lpwDocName) && wcsstr(lpFileName, lpwDocName))
- {
- OutputDebugStringW(L"W: Got it!");
- HWND hWnd = FindWindowW(0, L"AccessFileClient");
- if (hWnd)
- PostMessage(hWnd, WM_USER + 12345, 0, 0);
- }
- return OriCreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
- }
- BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
- {
- LONG error;
- (void)hinst;
- (void)reserved;
- if (dwReason == DLL_PROCESS_ATTACH) {
- OutputDebugString("accessfile.dll: Starting.\n");
-
- GetPrivateProfileStringW(L"config", L"docname", 0, lpwDocName, MAX_PATH, L"c:\\accessfile.ini");
- if (! wcslen(lpwDocName))
- MessageBoxW(NULL, L"没有读取到文档名称. 文档名称应该保存在 c:\\accessfile.ini. 如下格式: \n\n[config]\ndocname=myfile.ext", L"AccessFile", 0);
- DetourRestoreAfterWith();
- DetourTransactionBegin();
- DetourUpdateThread(GetCurrentThread());
- DetourAttach(&(PVOID&)OriCreateFileW, CreateFileW_);
- error = DetourTransactionCommit();
- if (error == NO_ERROR) {
- OutputDebugString("accessfile.dll: Detoured.\n");
- }
- else {
- OutputDebugString("accessfile.dll: Error when detouring()\n");
- }
- }
- else if (dwReason == DLL_PROCESS_DETACH) {
- DetourTransactionBegin();
- DetourUpdateThread(GetCurrentThread());
- DetourDetach(&(PVOID&)OriCreateFileW, CreateFileW_);
- error = DetourTransactionCommit();
- OutputDebugString("accessfile.dll: Ending.\n");
- }
- return TRUE;
- }
- //
- ///////////////////////////////////////////////////////////////// End of File.[/font]
复制代码 |
|