近期有个测试项目,要求产品crash后用户友好的对话框(CER)会弹出来,而不是出现 unhandle exception. 测试方案是人为的让产品crash,
然后看CER能不能工作。Test Design要求在不同的场合crash都能被CER捕获, 所以将导致crash的代码写在所有需要测试的地方不是一个好的方
案,一个理想的情况是,测试者可能让产品在任何时候crash,所以用DLL Hook监听键盘,当测试者按下某些组合键,比如Ctrl+Alt+Backspace的时候,执行Hook 的callback函数(里面有导致crash的代码),让产品crash,来测试CER是否能在产品运行的任何时候都起作用.
Windows Hook简介,见MSDN,这里省略了:)
简单的步骤如下:
- Create a dll which will be used to create hook, store the dll instance at dllmain, e.g.
2. Create a dll export function which will install a hook, note that, in order to crash the test product, I am going to set a local hook instead of global one, code snippet looks like below, our product(.exe) will call this method when need to install the hook.
3. Create a method used to uninstall the hook, in the project, which will be called when our product detach the dll, it also can be call outside the dll(e.g. test product) as I declare it as an export function
4. at last, define the hook callback function which will be called when keyboard event happened in this case