Changing hook (WH_CALLWNDPROC) arguments

B

blueturtle

Hi,

I've implemented a hook (WH_CALLWNDPRO), using the sample posted at
MSDN magazine.
( see url: http://msdn.microsoft.com/msdnmag/issues/02/10/CuttingEdge/
)

I succeed in intercepting the relevant messages, however if I try to
change the parameter to the original message, it doesn't work.
It seems like I'm doing something wrong with the Interop services.

E.g.
When intercepting WM_MOVING, I want keep the window, at a certain
coordinate,
which in C++ would look like:

case WM_MOVING:
{
RECT* p = (RECT*)lParam;
p->left = 10;
break;
}

in C#, in the hook proc the code looks like:

{ // "**unwrap**"
// lParamA is the argument to the hook proc, of type IntPtr
CWPSTRUCT cwp = (Win32.CWPSTRUCT)Marshal.PtrToStructure(lParamA,
typeof(Win32.CWPSTRUCT));

then in case of WM_MOVING:
Win32.RECT rect = (Win32.CWPSTRUCT)Marshal.PtrToStructure(cwp.lParam,
typeof(Win32.RECT));
}

Now, the values in rect are ok.
I change them and the copy back:

// copy rect
Marshal.StructureToPtr(rect, cwp.lParam, true);
// copy CWPSTRUCT
Marshal.StructureToPtr(cwp, lParamA, true);

here if u look again at cwp, using the block code mark as "**unwrap**",
I get the correct (changed) value.

However, eventually it doesnt seem to affect the hooked window,
so I guess something is missing or incorrect.

Any help is appreciated.

Si.
 
A

alikreznik

I have seen that before, when you convert, the first time everything is
fine and then it messes up. This happens because when you put together
the Struct, you need to specify the padding value, so for example:
Notice the Pack field:
[StructLayout (LayoutKind.Sequential, Pack=2)]
public class SISHalObs
{
[MarshalAs (UnmanagedType.I4)]
public int varId;//long
[MarshalAs (UnmanagedType.Struct)]
public SYSTEMTIME captureTime;
[MarshalAs (UnmanagedType.R8)]
public double captureValue;//double
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string captureUnits;
}
hth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top