tags:

views:

135

answers:

1

all day I'm trying to get EnumThreadWindows working, but I always get a Wrong Parameter-Error although my code is nearly the same as on pinvoke.net!! dont know why this doesnt work:

public static IntPtr FindMsgBoxFrom(IntPtr MainHWND)
        {
            SetLastError(0);
            uint ThreadID = GetThreadID(MainHWND);
            EnumThreadWindows(ThreadID, new WNDENUMPROC(decoder.FindMsgBox), IntPtr.Zero);
            int last = Marshal.GetLastWin32Error();
            if (last != 0)
                MessageBox.Show("EnumThreadWindows-Error:\n" + GetLastErrorString());

            return MSGHWND;
        }

and this is decoder.FindMsgBox:

public static bool FindMsgBox(IntPtr hwnd, IntPtr lparam)
        {
            if (IsMsgBox(hwnd))
            {
                MSGHWND = hwnd;
                return false;
            }
            else
                return true;

        }

Whats the problem with this?

A: 

I believe your EnumThreadWindows call should work fine, the problem seem to be in the

uint ThreadID = GetThreadID(MainHWND);

call; it looks like you're trying to pass window handle in there and this is not exactly what it expects to get from you, more details here: http://msdn.microsoft.com/en-us/library/ms683233%28VS.85%29.aspx

I've tried to change the line above to the following code:

[DllImport("kernel32.dll")]
static extern uint GetCurrentThreadId();

<...>

uint ThreadID = GetCurrentThreadId();

and the rest of your code worked fine for me

Also if you're looking to get thread ID for the given window handler, the code below might give you an idea on how to do it:

[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();

public static uint GetThreadIdForForegroundWindow()
{
   IntPtr hwnd = GetForegroundWindow();
   return (hwnd!=IntPtr.Zero) ? GetWindowThreadProcessId(hwnd, IntPtr.Zero) : 0;
}

<...>

uint ThreadID = GetThreadIdForForegroundWindow();

regards

serge_gubenko