Dll injection problems

Started by
1 comment, last by MyKee 14 years, 5 months ago
Hi, I'm injecting a piece of code into another process but when the code runs, the application crashes with "Access violation writing location 0x580....." and I can't figure out why. The dll injection is done by using CreateRemoteThread and I patch the IAT-table for a C++ library export with my own function that should be called instead. The method I'm hooking is "QLabel::setWordWrap(bool on)" in the QT-library just for testing purpose. void __stdcall QLabel_SetWrap(bool); void __stdcall QLabel_SetWrap(bool on) { void (__stdcall *pQLabel_SetWrap)(bool on); pQLabel_SetWrap = (void (__stdcall *)(bool)) old; MessageBox(NULL, "QLabel_SetWrap called!", "API Hook", MB_OK); pQLabel_SetWrap(on); } When the pQLabel_SetWrap(on) is called the application crashes with the "Access violation". But here is the strange part, it works without a problem If I do one of the following things - Comment out the MessageBox() line - Comment out the pQLabel_SetWrap(on) line - Move the pQLabel_SetWrap(on) to be called before the MessageBox() What have I missed?
Advertisement
Since setWordWrap is a non-static method of the class QLabel, the first thing I notice is that you're missing the "this" pointer in your declaration.
Additionally, I do not believe stdcall is the correct calling convention, you might want to try thiscall.
this pointer seems to come in the ECX register, when I called some other function the ECX register got overwritten with some other value causing it to crash when the SetWrap method was called!

Pushing the ECX value to the stack and then restoring it just before calling SetWrap fixed the problem, thanks!

This topic is closed to new replies.

Advertisement