热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

window核心编程22.3DIPS.exe的踩坑逻辑梳理

先上源代码DIPSLib.DLL编译篇这个是公共头文件CmnHdr.h#pragmaonce#define_WIN32_WINNT_WIN32_WINNT_LONGHOR

先上源代码

DIPSLib.DLL编译篇

这个是公共头文件CmnHdr.h

#pragma once
//#define _WIN32_WINNT _WIN32_WINNT_LONGHORN
//#define WINVER _WIN32_WINNT_LONGHORN#ifndef UNICODE#define UNICODE
#endif // UNICODE#ifdef UNICODE#ifndef _UNICODE#define _UNICODE#endif // !_UNICODE
#endif // UNICODE#pragma warning(push,3)
#include
#pragma warning(pop)
#pragma warning(push,4)
#include
#include#ifndef FILE_SKIP_COMPLETION_PORT_ON_SUCESS
#pragma message("You are not using the latest Platform SDK header /library")
#pragma message("files. This may prevent the project form building correctly.")
#endif // !FILE_SKIP_COMPLETION_PORT_ON_SUCESS#define chSTR2(x) #x
#define chSTR(x) chSTR2(x)
#define chMSG(desc) message(__FILE__ "(" chSTR(__LINE__) "):" #desc)
inline void chMB(PCSTR szMsg)
{char szTitle[MAX_PATH];GetModuleFileNameA(NULL, szTitle, _countof(szTitle));MessageBoxA(GetActiveWindow(), szMsg, szTitle, MB_OK);
}
inline void chFAIL(PSTR szMsg)
{chMB(szMsg);DebugBreak();
}
inline void chASSERTFAIL(LPCSTR file, int line, PCSTR expr)
{char sz[2 * MAX_PATH];wsprintfA(sz, "File %s , Linr %d : %s", file, line, expr);chFAIL(sz);
}#ifdef _DEBUG
#define chASSERT(x) if(!(x)) chASSERTFAIL(__FILE__,__LINE__,#x)
#else
#define chASSERT(x)
#endif // _DEBUG#ifdef _DEBUG
#define chVERIFY(x) chASSERT(x)
#else
#define chVERIFY(x) (x)
#endif // _DEBUGinline void chSETDLGICONS(HWND hWnd, int idi)
{SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon((HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),MAKEINTRESOURCE(idi)));SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon((HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),MAKEINTRESOURCE(idi)));
}#define chHANDLE_DLGMSG(hWnd,message,fn) \case(message): return (SetDlgMsgResult(hWnd,message,HANDLE_##message((hWnd),(wParam),(lParam),(fn))))

创建个dialog资源用mfc  改名字Wintellect DIPS 改id  IDD_DIPS

下面附上DIPSLib代码  .H文件

#pragma once
#if !defined(DIPSLIBAPI)
#define DIPSLIBAPI __declspec(dllimport)
#endifDIPSLIBAPI BOOL __stdcall SetDIPSHook(DWORD dwThreadId);

.cpp文件

#include"CmnHdr.h"
#include
#include
#define DIPSLIBAPI __declspec(dllexport)
#include"DIPSLib.h"
#include"resource.h"#ifdef _DEBUG
void ForceDebugBreak()
{__try {DebugBreak();}__except (UnhandledExceptionFilter(GetExceptionInformation())){}
}
#else
#define ForceDebugBreak()
#endif // _DEBUG//Instruct the compiler to put the g_hHook data variable in its own
//data section called shared. we then instruct the linker that we want to
//share the data in this section with all instances of this application
#pragma data_seg("Shared")
HHOOK g_hHook = NULL;
DWORD g_dwThreadIdDIPS = 0;
#pragma data_seg()//Instruct the linker to make the shared section
//readable writeable and shared
#pragma comment(linker,"/section:Shared,rws")//Nonshared variables
HINSTANCE g_hInstDll = NULL;
LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK Dlg_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID fImpLoad)
{switch (fdwReason){case DLL_PROCESS_ATTACH:g_hInstDll = hInstance;break;case DLL_THREAD_ATTACH:break;case DLL_PROCESS_DETACH:break;case DLL_THREAD_DETACH:break;default:break;}return TRUE;
}
BOOL WINAPI SetDIPSHook(DWORD dwThreadId)
{BOOL bok = FALSE;if (dwThreadId != 0){//make sure that the hook is not already installedchASSERT(g_hHook == NULL);//Save our thread ID in a shared variable so that our GetMsgProc//function can post a message back to the thread when the server window//has been createdg_dwThreadIdDIPS = GetCurrentThreadId();//install the hook on the spacified threadg_hHook = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, g_hInstDll, dwThreadId);bok = (g_hHook != NULL);if (bok){//The Hook was installed sucessfully; force a benign message to //the thread's queue so that the hook funtion gets calledbok = PostThreadMessage(dwThreadId, WM_NULL, 0, 0);}else{}}else{//make sure that a hook has been installedchASSERT(g_hHook != NULL);bok = UnhookWindowsHookEx(g_hHook);g_hHook = NULL;}return bok;
}LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{static BOOL bFirstTime = TRUE;if (bFirstTime){//the dll just got injectedbFirstTime = FALSE;chMB("wo jinlaile wokaixin");//create the dips server window to handle the client requestCreateDialog(g_hInstDll, MAKEINTRESOURCE(IDD_DIPS), NULL, Dlg_Proc);//Tell the Dips application that the server is up //and ready to handle requestsPostThreadMessage(g_dwThreadIdDIPS, WM_NULL, 0, 0);}return (CallNextHookEx(g_hHook, nCode, wParam, lParam));
}
void Dlg_OnClose(HWND hWnd)
{DestroyWindow(hWnd);
}
static const TCHAR g_szRegSubKey[] = TEXT("Software\\Wintellect\\Desktop Item Position Saver");void SaveListViewItemPositions(HWND hWndLV)
{int nMaxItems = ListView_GetItemCount(hWndLV);//when saving new positions, delete the old position//information that is currently in the registry.LONG l = RegDeleteKey(HKEY_CURRENT_USER, g_szRegSubKey);//Create the registry key to hold the infoHKEY hKey;l = RegCreateKeyEx(HKEY_CURRENT_USER, g_szRegSubKey, 0, NULL,REG_OPTION_NON_VOLATILE,KEY_SET_VALUE,NULL,&hKey,NULL);chASSERT(l == ERROR_SUCCESS);for (int nItem = 0; nItem }
void RestoreListViewItemPositions(HWND hWndLV)
{HKEY hkey;LONG l = RegOpenKeyEx(HKEY_CURRENT_USER, g_szRegSubKey,0, KEY_QUERY_VALUE, &hkey);if (l == ERROR_SUCCESS){//if the listview has autoarrange on temporarity turn it ofDWORD dwStyle = GetWindowStyle(hWndLV);if (dwStyle & LVS_AUTOARRANGE){SetWindowLong(hWndLV, GWL_STYLE, dwStyle&LVS_AUTOARRANGE);}l = NO_ERROR;for (int nIndex = 0; l != ERROR_NO_MORE_ITEMS; nIndex++){TCHAR szName[MAX_PATH];DWORD cbValueName = _countof(szName);POINT pt;DWORD cbData = sizeof(pt);DWORD nItem;//read a value name and position from the registry.DWORD dwType;l = RegEnumValue(hkey, nIndex, szName, &cbValueName,NULL, &dwType, (PBYTE)&pt, &cbData);if (l == ERROR_NO_MORE_ITEMS){continue;}if ((dwType == REG_BINARY) && (cbData == sizeof(pt))){//the value is something we recognize; try to find an//item in the listview control that matches the name LV_FINDINFO lvfi;lvfi.flags = LVFI_STRING;lvfi.psz = szName;nItem = ListView_FindItem(hWndLV, -1, &lvfi);if (nItem != -1){// we found a match ;change the item's positionListView_SetItemPosition(hWndLV, nItem, pt.x, pt.y);}}}//turn auto arrange back on if it was originally onSetWindowLong(hWndLV, GWL_STYLE, dwStyle);RegCloseKey(hkey);}
}INT_PTR WINAPI Dlg_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {switch (uMsg) {chHANDLE_DLGMSG(hWnd, WM_CLOSE, Dlg_OnClose);case WM_APP:// Uncomment the line below to invoke the debugger// on the process that just got the injected DLL.//ForceDebugBreak();if (lParam)SaveListViewItemPositions((HWND)wParam);elseRestoreListViewItemPositions((HWND)wParam);break;}return FALSE;
}

注意你hook的是explorer 你得确定它的窗口位置用spy++来获取,不再是原先作者提示的一个窗口的唯一子窗口了

第二explorer程序是系统程序如果你是64位系统恭喜你你的整个环境都得换成64位来编译了

命令预处理得加入WIN32(64);_DEBUG;_WINDOWS;_USRDLL;

下面贴exe代码  .cpp

//#include"..\CommonFiles\CmnHdr.h"
#include
#include
#include
#include"resource.h"
#pragma comment(lib,"E:\\VS2017WSpce\\Proxxxxxx\\x64\\Debug\\DIPSLib.lib")
#include"../DIPSLib/DIPSLib.h"
#include"CmnHdr.h"BOOL Dlg_OnInitDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
{chSETDLGICONS(hWnd, IDI_DIPS);return TRUE;
}void Dlg_OnCommand(HWND hWnd, int id, HWND hWndCt1, UINT codeNotify)
{switch (id){case IDC_SAVE:case IDC_RESTORE:case IDCANCEL:EndDialog(hWnd, id);break;default:break;}
}
BOOL WINAPI Dlg_Proc2(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{switch (uMsg){//case(WM_INITDIALOG): return (SetDlgMsgResult(hWnd, message, HANDLE_##message((hWnd), (wParam), (lParam), (Dlg_OnInitDialog))))//case(WM_COMMAND): return (SetDlgMsgResult(hWnd, message, HANDLE_##message((hWnd), (wParam), (lParam), (Dlg_OnInitDialog))))chHANDLE_DLGMSG(hWnd, WM_INITDIALOG,Dlg_OnInitDialog);chHANDLE_DLGMSG(hWnd, WM_COMMAND, Dlg_OnCommand);}return FALSE;
}int WINAPI _tWinMain(HINSTANCE hInstExe, HINSTANCE, PTSTR pszCmdLine, int)
{//Conver Command-Line character to uppercaseCharUpperBuff(pszCmdLine, 1);TCHAR cWhatToDo = pszCmdLine[0];if ((cWhatToDo != TEXT('S')) && (cWhatToDo != TEXT('R'))){//An incalid command-line argument;prompt the usercWhatToDo = 0;}if (cWhatToDo == 0){//NO Command-line argument was used to tell us what to//do; show usage dialog box and prompt the userswitch (DialogBox(hInstExe,MAKEINTRESOURCE(IDD_DIPS),NULL,(DLGPROC)Dlg_Proc2)){case IDC_SAVE:cWhatToDo = TEXT('S');break;case IDC_RESTORE:cWhatToDo = TEXT('R');break;default:break;}}if (cWhatToDo == 0){//The user doesn't want to do anythingreturn (0);}//The Desktop ListView window is the grandchild of the ProMan window.HWND hWndLV2 = FindWindow(TEXT("Progman"), NULL);HWND hwnd3 = GetFirstChild(hWndLV2);HWND hwnd4 = GetNextSibling(hwnd3);HWND hWndLV = GetFirstChild(hwnd4);//HWND hWndLV = GetFirstChild(GetFirstChild(// FindWindow(TEXT("Progman"), NULL)));chASSERT(IsWindow(hWndLV));//Set hook that injects out DLL into the Exporer's address space. After//setting the hook, the DIPS hidden modeless dialog box is created. we//send messages to this window to tell it what we want it to do.DWORD threadId = GetWindowThreadProcessId(hWndLV, NULL);chVERIFY(SetDIPSHook(threadId));//wait for the DIPS server window to be created.MSG msg;GetMessage(&msg, NULL, 0, 0);//find the handle of the hidden dialog box window.HWND hWndDIPS = FindWindow(NULL, TEXT("Wintellect DIPS"));//make sure that the window was created.chASSERT(IsWindow(hWndDIPS));//Tell the DIPS window which ListView window to manipulate//and wherther the item should be saved or restoredBOOL bSave = (cWhatToDo == TEXT('S'));SendMessage(hWndDIPS, WM_APP, (WPARAM)hWndLV, bSave);//Tell the DIPS window to destory itself .Use sendMessage//instead of PostMessage so that we know the window is //destoryed befor the hook is removed.SendMessage(hWndDIPS, WM_CLOSE, 0, 0);//Make sure that the window was destoryed.chASSERT(!IsWindow(hWndDIPS));//Unhook the DLL, removing the DIPS dialog box provedure//form the Exporer's address space.SetDIPSHook(0);return 0;
}

这个不要.h

需要包含那个dll的头文件,生成的dll  lib 都得移动到工程下面来,附加依赖也得把lib加上去

编译平台得移植,Unicode编码一致,字符集得一致,多线程调试得一致

然后你就可以愉快的踩坑了


推荐阅读
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 如何在HTML中获取鼠标的当前位置
    本文介绍了在HTML中获取鼠标当前位置的三种方法,分别是相对于屏幕的位置、相对于窗口的位置以及考虑了页面滚动因素的位置。通过这些方法可以准确获取鼠标的坐标信息。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • 本文介绍了PE文件结构中的导出表的解析方法,包括获取区段头表、遍历查找所在的区段等步骤。通过该方法可以准确地解析PE文件中的导出表信息。 ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
  • 本文介绍了lintcode(12)题目的要求和解题思路,以及给出了相应的代码实现。题目要求在给定的字符串source中找到包括所有目标字符串字母的最短子串,并且时间复杂度为O(n)。解题思路是使用滑动窗口的方法,通过维护一个unordered_map来记录目标字符串中每个字符的出现次数,并使用双指针来寻找最小子串。代码实现部分给出了具体的实现代码。 ... [详细]
  • 如何利用 Myflash 解析 binlog ?
    本文主要介绍了对Myflash的测试,从准备测试环境到利用Myflash解析binl ... [详细]
  • 用Vue实现的Demo商品管理效果图及实现代码
    本文介绍了一个使用Vue实现的Demo商品管理的效果图及实现代码。 ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
  • 导出功能protectedvoidbtnExport(objectsender,EventArgse){用来打开下载窗口stringfileName中 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • 本文介绍了响应式页面的概念和实现方式,包括针对不同终端制作特定页面和制作一个页面适应不同终端的显示。分析了两种实现方式的优缺点,提出了选择方案的建议。同时,对于响应式页面的需求和背景进行了讨论,解释了为什么需要响应式页面。 ... [详细]
author-avatar
mobiledu2502883017
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有