先上源代码

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<Windows.h>
#pragma warning(pop)
#pragma warning(push,4)
#include<CommCtrl.h>
#include<process.h>#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<WindowsX.h>
#include<CommCtrl.h>
#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 < nMaxItems; nItem++){//Get the name and positon of a ListView item.TCHAR szName[MAX_PATH];ListView_GetItemText(hWndLV, nItem, 0, szName, _countof(szName));POINT pt;ListView_GetItemPosition(hWndLV, nItem, &pt);//Save the name and Position in the registryl = RegSetValueEx(hKey, szName, 0, REG_BINARY, (PBYTE)&pt, sizeof(pt));chASSERT(l == ERROR_SUCCESS);}RegCloseKey(hKey);
}
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<WindowsX.h>
#include<Windows.h>
#include<tchar.h>
#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编码一致,字符集得一致,多线程调试得一致

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

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

  1. Window核心编程

    Window核心编程 第一章:错误处理 FormatMessage/LocalLock\LocalFree 第二章: TEXT("..") TCHAR(CHAR,WCHAR)  安 ...

  2. 将VC++6.0的代码迁移到VS2005常见问题总结(Window核心编程第五版以前代码在VS2005无法编译的解决方案)...

    额喜新厌旧是男人的通病吧,可是呢VS2005的界面看着的确比VC6.0看着舒服,而且也算用习惯了吧.可是网上现在大部分C/C++的代码还是用VC6.0的.这为我们这些菜鸟的学习之路增添了不少障碍,可能 ...

  3. python从入门到实践django看不懂_Python编程:从入门到实践踩坑记 Django

    <>踩坑记 Django Django Python 19.1.1.5 模板new_topic 做完书上的步骤后,对主题添加页面经行测试,但是浏览器显示 服务器异常. 个人采用的开发环境是 ...

  4. 使用exe4j+inno setup打包exe文件踩坑记录

    一.前言 最近用Java给朋友写了一个截图工具和将所有图片生成pdf工具,但是由于朋友不是计算机行业的人员,如果为了使用java常见的运行方式运行工具,则还需要在电脑上安装jdk与jre,着实不方便. ...

  5. Java核心编程(22)

    1.NIO 1.1 NIO通道客户端[应用] 客户端实现步骤 打开通道 指定IP和端口号 写出数据 释放资源 示例代码 public class NIOClient {public static vo ...

  6. 记一次22亿大数据分析处理踩坑实践

    前言:用最简单最少的语言,分享我的坑,理不理解需求不重要,问题都在shell代码中,看shell极度枯燥,希望能有帮助! 一. 起因 需求,分析hive表中两年内用户XX的所有数据,按照每天早,中,晚 ...

  7. Python打包工具Pyintealler打包py文件为windows exe文件过程及踩坑记录+实战例子

    Python打包工具Pyintealler打包py文件为windows exe文件过程及踩坑记录+实战例子 目录 Python打包工具Pyintealler打包py文件为windows exe文件过程 ...

  8. C++Windows核心编程读书笔记(转)

    http://www.makaidong.com/(马开东博客) 这篇笔记是我在读<windows核心编程>第5版时做的记录和总结(部分章节是第4版的书),没有摘抄原句,包含了很多我个人的 ...

  9. 内存映射文件——Windows核心编程学习手札之十七

    内存映射文件 --Windows核心编程学习手札之十七 与虚拟内存一样,内存映射文件保留地址空间,并将物理存储器提交给该区域,差别在于所提交的物理存储器是磁盘上有文件存在的空间,而非系统的页文件,一旦 ...

  10. python核心编程--笔记(不定时跟新)(转)

    的解释器options: 1.1 –d   提供调试输出 1.2 –O   生成优化的字节码(生成.pyo文件) 1.3 –S   不导入site模块以在启动时查找python路径 1.4 –v   ...

最新文章

  1. 数据、人工智能和传感器按COVID-19新冠流感排列
  2. Delphi - 数组 详解
  3. 单片机学习--3D动画演示单片机工作原理
  4. LeetCode 79 Word Search(单词查找)
  5. 用C语言做一个横板过关类型的控制台游戏
  6. salesforce零基础学习(八十九)使用 input type=file 以及RemoteAction方式上传附件
  7. loadrunner发送json_Loadrunner模拟JSON接口请求进行测试
  8. 面试官,求求你不要问我这么简单但又刁难的算法题了
  9. ubuntu怎么切换到root用户,切换到root账号方法
  10. 有些车已经不能再买了!因为国五排放标准就要来了!
  11. linux 安装监控系统,CentOS7安装性能监控系统
  12. 《android 解析WebService》
  13. play另一个模板japid
  14. 《深入理解计算机网络》读后小记 8、IP地址和子网
  15. hdu1598-----Kruscal+枚举
  16. 高仿项目协作工具【Worktile】,从零带你一步步实现组织架构、网盘、消息、项目、审批等功能
  17. 基于微信小程序的美容美发技师预约系统设计与实现毕业设计毕设开题报告
  18. bin文件夹和lib文件夹
  19. 如何定制支持用户自定义boot参数的基于debian os的live cd
  20. 人物渲染篇(二) —— 基础卡通渲染 下

热门文章

  1. Struts框架命名空间问题答疑
  2. 除了迅雷外,还有什么靠谱的BT下载软件?
  3. MySql常用函数大全讲解
  4. ikm2022/IKM考试/ikm java
  5. Vim快捷键-键位图
  6. 学习编程需不需要背代码。
  7. Centos7完全卸载MySQL 安装 启动
  8. 【Matlab数学建模】层次分析法
  9. 分享几点论文降重的方法(干货)
  10. xps仪器及数据说明