GetWindowLong on GWL_EXSTYLE fails with error 127

W

Wayne Bradney

Simple, really. I have C# code in a Windows Form, thus:

int rc;
rc = Win32API.SetParent(ctrl.Handle, new
IntPtr(Win32API.GetDesktopWindow()));
rc = Win32API.GetWindowLong(ctrl.Handle, Win32API.GWL_EXSTYLE);
if (rc == 0) {
System.Console.WriteLine(Marshal.GetLastWin32Error());
}

where ctrl is a control on the form (it doesn't seem to matter what
type of control), and Win32API has all the relevant imports:

[DllImport("user32.dll")]
public static extern int SetParent(IntPtr childHwnd, IntPtr
parentHwnd);
[DllImport("user32.dll")]
public static extern int GetDesktopWindow();
[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);


I'm doing this as a precursor to making the control a custom drop-down
of sorts (subsequent lines will set WS_EX_TOOLWINDOW, HWND_TOPMOST,
etc. and subclass the GWL_WNDPROC for mouse handling etc.), but I
don't even get that far. In all cases GetWindowLong returns 0, and the
Win32Error is 127 (which decodes as 'The specified procedure could not
be found.')

Anyone successfully done this from .NET?
 
M

Mattias Sjögren

Wayne,
In all cases GetWindowLong returns 0, and the
Win32Error is 127 (which decodes as 'The specified procedure could not
be found.')

I don't think zero necessarily indicates an error for GWL_EXSTYLE. Are
you sure the window has any non-zero extended flags set? Have you
tried the code with some other window?



Mattias
 
W

Wayne Bradney

Hi Mattias,

I tried the same code with 5 or 6 different controls on a form. I also
suspected that the EXSTYLE was actually zero, but even if I do
SetLastError(0) before calling GetWindowLong, the error code is still
127.

However I just re-read the (slightly contradictory) MSDN documentation
for GetWindowLong:

"If the function fails, the return value is zero. To get extended
error information, call GetLastError.

If SetWindowLong has not been called previously, GetWindowLong returns
zero for values in the extra window or class memory."

So I think I can get around this by ignoring the zero return value on
the first call to GetWindowLong. When I SetWindowLong, that seems to
work just fine (and actually returns a large non-zero value for the
previous EXSTYLE value, curiously). Then subsequent calls to
GetWindowLong work as expected, returning the value that I previously
set instead of zero. Very strange.

Thanks and regards,
WMB
 
M

Mattias Sjögren

I tried the same code with 5 or 6 different controls on a form. I also
suspected that the EXSTYLE was actually zero, but even if I do
SetLastError(0) before calling GetWindowLong, the error code is still
127.

That error isn't necessarily set by GetWindowLong. The CLR may call
other APIs before and after your call to GetWindowLong. For example,
the first time you call a DllImported function, the CLR has to bind to
it using LoadLibrary and GetProcAddress.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top