Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / February 2005

Tip: Looking for answers? Try searching our database.

EnumWindows & returning data

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Thielen - 17 Jan 2005 21:23 GMT
Hi;

When calling EnumWindows from C#, how do I get data back? The C++ way is to
pass in a pointer as the second parameter and in the EnumProc cast the lParam
to the appropiate pointer and set it. But C# won't let you do that.

Signature

thanks - dave

Willy Denoyette [MVP] - 17 Jan 2005 22:29 GMT
Pass a delegate with the correct signature as first argument to the
EnumWindows API.

public delegate bool CallBack(IntPtr hwnd, int lParam);

   [DllImport("user32"), SuppressUnmanagedCodeSecurityAttribute]
   public static extern int EnumWindows(CallBack x, int y);
...
// Call Win32 API passing a callback delegate (function pointer)
EnumWindows(new CallBack(Report), 0);
...

// This is the function that gets called by the API
static void Report(IntPtr hwnd, int lParam)
{
...
}

Willy.

> Hi;
>
[quoted text clipped - 3 lines]
> lParam
> to the appropiate pointer and set it. But C# won't let you do that.
David Thielen - 17 Jan 2005 23:49 GMT
Right, but how do I get data back from CallBack to the method that calls
EnumWindows()?

thanks - dave

> Pass a delegate with the correct signature as first argument to the
> EnumWindows API.
[quoted text clipped - 23 lines]
> > lParam
> > to the appropiate pointer and set it. But C# won't let you do that.
Willy Denoyette [MVP] - 18 Jan 2005 00:03 GMT
That data is the IntPtr hwnd, or what did you expect?.

static void Report(IntPtr hwnd, int lParam)
{
   Console.WriteLine(hwnd);

}

Willy.

> Right, but how do I get data back from CallBack to the method that calls
> EnumWindows()?
[quoted text clipped - 29 lines]
>> > lParam
>> > to the appropiate pointer and set it. But C# won't let you do that.
David Thielen - 18 Jan 2005 00:25 GMT
Right - I have the hwnd in Report. I am looking for a specific window so for
each hwnd I do a GetWindowText() and when it has some specific text in the
caption, I have found my window.

So Report() returns false because I have found the window I need. How do I
get that hwnd value back up to the method that called EnumWindows()?

thanks - dave

> That data is the IntPtr hwnd, or what did you expect?.
>
[quoted text clipped - 39 lines]
> >> > lParam
> >> > to the appropiate pointer and set it. But C# won't let you do that.
"Peter Huang" [MSFT] - 18 Jan 2005 07:38 GMT
Hi

I think you may try define a class member variable to pass the value back
to the method call the enumwindows.

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 - 18 Jan 2005 08:45 GMT
Hi;

I tried that but you pass an int and C# won't let you cast an object to an
int.

Any other approaches?

thanks - dave

> Hi
>
[quoted text clipped - 8 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 19 Jan 2005 01:33 GMT
Hi

I think we can cast the object to int as below which is known as unbox.
object o = 10;
int i = (int)o;
int j = Convert.ToInt32(o);
Console.Write(i);
Console.Write(j);

Type Fundamentals
http://msdn.microsoft.com/msdnmag/issues/1200/dotnet/

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 02:35 GMT
Hi;

Please read the sample code I created. The problem is getting the hwnd from
the EnumProc back up to the method that calls EnumWindows. I don't see how to
do that.

??? - thanks - dave

> Hi
>
[quoted text clipped - 15 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 19 Jan 2005 07:35 GMT
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.
David Thielen - 18 Jan 2005 01:47 GMT
I guess my question wasn't very clear. Here's some sample code:

private const string CAPTION = "F076B0EA-FEE3-40E0-A576-4E68D13FAC75";

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!!!!!

   app.Caption = saveCaption;
   return hwnd;
}

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????
   return false;
}

> Hi;
>
> When calling EnumWindows from C#, how do I get data back? The C++ way is to
> pass in a pointer as the second parameter and in the EnumProc cast the lParam
> to the appropiate pointer and set it. But C# won't let you do that.
Valli - 14 Feb 2005 05:09 GMT
> I guess my question wasn't very clear. Here's some sample code:
>
[quoted text clipped - 30 lines]
>>pass in a pointer as the second parameter and in the EnumProc cast the lParam
>>to the appropiate pointer and set it. But C# won't let you do that.

Why dont you use FindWindow straightaway?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.