Trouble passing _EXCEPTION_POINTERS * using FileMapping

邮差的信 提交于 2019-12-01 23:45:29

Right, you cannot dereference the pointer in another process, it is only valid in the crashed process. It is only good enough to pass to MiniDumpWriteDump(), MINIDUMP_EXCEPTION_INFORMATION.ExceptionPointers field. Technically you could use ReadProcessMemory() but doing so for a crashed process is unnecessarily risky. The simple solution is to add an extra field to your structure that stores the exception code and written by your exception filter.

mytest passdata ;
passdata.except = ExceptionInfo;
// Note: added field
passdata.ExceptionCode = ExceptionInfo->ExceptionRecord->ExceptionCode;
passdata.ThreadId = GetCurrentThreadId();
// etc..

Also avoid calling winapi functions like OpenFileMapping and MapViewOfFile, it is too risky. They tend to deadlock when the program crashed due to heap corruption of the process heap. A common reason to crash and a deadlock because the heap lock is still held. Just do this at program initialization. You don't need to bother cleaning up either, Windows takes care of it when your watchdog process terminates the crashed process after taking the minidump.

Martin Ba

I will put this together as an answer, although it really should be a comment to Hans's answer (and the comments there) but it seems some explanation is necessary:

The code posted in the question correctly passes the value(s) of the struct mytest structure into shared memory.

The second code snippet:

(For ex)Process 2 :

cout << passdata->except->ExceptionRecord->ExceptionCode << endl ;

Shows a misunderstanding though: While you can read the value of the pointer passdata.except, in process 2 this is just an arbitrary 32/64 bit value, it is not a valid pointer.

You can pass this to MiniDumpWriteDump, this function will eveluate this pointer value in the context of the target process (proc 1). But you cannot dereference it in process #2.

Hans's example gives the solution, iff you need the value of ExeptionCode in process #2, then you need to dereference the pointer in proc#1 and put the value into the data you write to shared memory.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!