471,350 Members | 1,265 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Changing hook (WH_CALLWNDPROC) arguments


Hi,

I've implemented a hook (WH_CALLWNDPRO), using the sample posted at
MSDN magazine.
( see url: http://msdn.microsoft.com/msdnmag/is...0/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.

Dec 5 '05 #1
1 7479
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
bl********@walla.co.il wrote:
Hi,

I've implemented a hook (WH_CALLWNDPRO), using the sample posted at
MSDN magazine.
( see url: http://msdn.microsoft.com/msdnmag/is...0/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.


Dec 5 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by murlbrown | last post: by
1 post views Thread by Raphael Iloh | last post: by
4 posts views Thread by Weinand Daniel | last post: by
10 posts views Thread by Steve | last post: by
7 posts views Thread by jpierson | last post: by
reply views Thread by zeng.hui.stephen | last post: by
22 posts views Thread by schneider | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.