Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use WinAPI in C#??? 1

Status
Not open for further replies.

minhhoa

Programmer
Oct 20, 2002
13
VN
Does anyone know how to declare the funtions: SetWindowLong and CallWindowProc (in User32.dll) and how to use them in C#?

Thanks.
 
Thanks for your help. I get problems on the type convertion. Could you give me more details?
 
Here it is:

//*******************************************************//

using System;

namespace CSToolWinAddin
{
/// <summary>
/// Summary description for HookMessage.
/// </summary>
///
using System.Runtime.InteropServices;

delegate void MsgEventHandler(object source, MsgEventArgs e);

public class MsgEventArgs: EventArgs
{
public int hWnd;
public int uMsg;
public int wParam;
public int lParam;

public MsgEventArgs(int hWnd, int uMsg, int wParam, int lParam)
{
this.hWnd = hWnd;
this.uMsg = uMsg;
this.wParam = wParam;
this.lParam = lParam;
}
}

class HookMessage
{
const int GWL_WNDPROC= -4;
private int pPrevProc, hWnd;

delegate int MyCallBack(int hWnd, int message, int wParam, int lParam);

[DllImport(&quot;User32.dll&quot;)]
public static extern int SetWindowLong(int hWnd, int nIndex, int newLong);

[DllImport(&quot;User32.dll&quot;, CharSet=CharSet.Auto)]
public static extern int CallWindowProc (int pPrevProc, int hWnd, int message,int wParam,int lParam);

public event MsgEventHandler OnMessage;

public HookMessage(int wHandle)
{
hWnd = wHandle;
MyCallBack wndProc= new MyCallBack(WindowProc);
pPrevProc = SetWindowLong(hWnd, GWL_WNDPROC, (int)wndProc);
}

~HookMessage()
{
SetWindowLong(hWnd, GWL_WNDPROC, pPrevProc);
}

public int WindowProc(int hWnd, int message, int wParam, int lParam)
{
//WindowProc = CallWindowProc(pPrevProc, hWnd, message, wParam, lParam);
MsgEventArgs args= new MsgEventArgs(hWnd,message,wParam,lParam);
this.OnMessage(this,args);
return CallWindowProc(pPrevProc, hWnd, message, wParam, lParam);
}
}
}

//*******************************************************//

The compile error is &quot;D:\Temp\HookMessage.cs(50): Cannot convert type 'CSToolWinAddin.HookMessage.MyCallBack' to 'int'&quot;.

Thanks anyway.
 
It doesn't meet my requirement. Thanks anyway.

I'm trying to hook another processes window messages but I don't know why it isn't recommended !!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top