RE: Callback not being called

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi Peter,

Thanks for the reply.

Please take a look at my sample code. This code will work if I do not
specify that I want an array of strings back from the unmanaged code.

I need to be able to return an array of strings back from the unmanaged code.

""Peter Huang" [MSFT]" wrote:

Hi

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you want to set a callback in .NET
code, so that when the unmanaged C++ function tried to call the function
pointer(callback), it will call the .NET managed code.

Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

Based on my research, I think you may try to take a look at the link below
which will demostrate how to use call back between managed and unmanaged
code.
Global System Hooks in .NET
http://www.codeproject.com/csharp/GlobalSystemHook.asp

Here are some code snippet for your reference.
[DllImport("user32")]
public static extern int EnumWindows(CallBack x, ref IntPtr y);
public delegate bool CallBack(IntPtr hwnd, ref IntPtr lParam);
private void button1_Click(object sender, System.EventArgs e)
{
CallBack myCallBack = new CallBack(Report);
IntPtr lp = this.Handle;
EnumWindows(myCallBack, ref lp);
Debug.WriteLine("button1_Click: "+lp.ToInt32().ToString("X"));
}

public static bool Report(IntPtr hwnd,ref IntPtr lParam)
{
string str = "Window handle is :" +hwnd.ToInt32().ToString("X");
Debug.WriteLine(str);
if (hwnd == lParam)
{
Debug.WriteLine("Matched!"+"hwnd:"
+hwnd.ToInt32().ToString("X")+"lParam:"+lParam.ToInt32().ToString("X"));
lParam = new IntPtr(0x1234);//return
return false;
}
return true;


}

Please Apply My Suggestion Above And Let Me Know If It Helps Resolve Your
Problem.

If you still have any concern, please feel free to post here.


Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


.


Quantcast