Hi
I think use the member variable to do the job as below, if I have any
misunderstanding, please feel free to post here.
int tmpWindowHandle=0;
public static int GetParentWindow(Application app) {
// Setting caption in Word - but Word then adds more to this text
string saveCaption = app.Caption;
app.Caption = CAPTION;
CallBack myCallBack = new CallBack(Report);
EnumWindows(myCallBack, 0);
// I need hwnd from below here!!!!!
//We can get the hwnd based on tmpWindowHandle
app.Caption = saveCaption;
return hwnd; //we can change to return tmpWindowHandle;
}
private static bool Report(int hwnd, int lParam) {
string lpText = new string((char) 0, 100);
GetWindowText(new IntPtr(hwnd), lpText, lpText.Length);
if (lpText.IndexOf(CAPTION) == -1)
return true;
// how do I get hwnd returned to GetParentWindow method????
tmpWindowHandle =hwnd ;//found the windows handle and assign to the temp
variable
return false;
}
Best regards,
Perter Huang
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
David Thielen - 19 Jan 2005 14:15 GMT
Hi;
Yes, that's what I am doing right now. But it means the code is not thread
safe. It should be ok for my situation but it is not a good way to write code
like this. And the usual use of the lParam in C++ was to pass a pointer in -
but you can't do that with C#.
Is there any way around using the static variable to pass the hwnd back?
thanks - dave
> Hi
>
[quoted text clipped - 38 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 20 Jan 2005 02:36 GMT
Hi
If you want to make it thread safe we can use the synchronized method to
guarantee it.e.g. the Mutex class.
Also in C# we can pass a IntPtr byref so that it will be passed by address.
[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;
}
Best regards,
Perter Huang
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
David Thielen - 20 Jan 2005 03:39 GMT
The ref IntPtr worked great - THANK YOU.
> Hi
>
[quoted text clipped - 34 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.