Does anyone know what is wrong with these imports from User32.dll?

  • Thread starter Thread starter Will Pittenger
  • Start date Start date
W

Will Pittenger

The constants.windowLongIndices type is an enum. That is there so I can
ensure the appropriate values are always used. Since I had to replace the
GWL macros with new values, I decided to prevent passing 7 -- or whatever
the author of the caller felt like. I tried both ExactSpelling=true and
setting the entry point manually. I also tried forcing the Unicode and
ASCII versions. (According to the Win32 documentation, both functions have
UNICODE versions in NT/2000/XP. My tests are on a XP Pro SP1 system.


[System.Runtime.InteropServices.DllImport("user32", SetLastError=true)]
public static extern IntPtr SetWindowLongPtr(IntPtr hWnd,
constants.windowLongIndices index, IntPtr iNewValue);

[System.Runtime.InteropServices.DllImport("user32", SetLastError=true)]
public static extern IntPtr GetWindowLongPtr(IntPtr hWnd,
constants.windowLongIndices index);
 
Hello Will,

Try

public static extern IntPtr SetWindowLongPtrW(IntPtr hWnd,
constants.windowLongIndices index, IntPtr iNewValue);

and specify the ExactSpelling option. You can also apply
the MarshalAs attribute to the "index" argument to ensure
it is indeed treated as an int32 type parameter.
 
Will,

On Win32, SetWindowLongPtr is implemented as a macro that just calls
the old SetWindowLong function (and the same is true for
GetWindowLong/Ptr). So you have to specify that as your entry point

[DllImport("user32", SetLastError=true, EntryPoint="SetWindowLong",
CharSet=CharSet.Auto)]
public static extern IntPtr SetWindowLongPtr(IntPtr hWnd,
constants.windowLongIndices index, IntPtr iNewValue);



Mattias
 
Gosh, how could I forget this old trick! So, as an additional advice to the
original poster: if you have Platform SDK installed, you can investigate the
corresponding API declaration in the Windows header files to ensure given
API function is not actually a macro or something.
 
That turns out to be correct. Given that we are supposed to take
precautions to ensure portablity to 64-bit code, how should this be
imported?
 
I think that Mattias had the better answer.

----------
Will Pittenger
E-Mail: mailto:[email protected]
All mail filtered by Qurb (www.qurb.com)
Dmitriy Lapshin said:
Hello Will,

Try

public static extern IntPtr SetWindowLongPtrW(IntPtr hWnd,
constants.windowLongIndices index, IntPtr iNewValue);

and specify the ExactSpelling option. You can also apply
the MarshalAs attribute to the "index" argument to ensure
it is indeed treated as an int32 type parameter.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Will Pittenger said:
The constants.windowLongIndices type is an enum. That is there so I can
ensure the appropriate values are always used. Since I had to replace the
GWL macros with new values, I decided to prevent passing 7 -- or whatever
the author of the caller felt like. I tried both ExactSpelling=true and
setting the entry point manually. I also tried forcing the Unicode and
ASCII versions. (According to the Win32 documentation, both functions have
UNICODE versions in NT/2000/XP. My tests are on a XP Pro SP1 system.


[System.Runtime.InteropServices.DllImport("user32", SetLastError=true)]
public static extern IntPtr SetWindowLongPtr(IntPtr hWnd,
constants.windowLongIndices index, IntPtr iNewValue);

[System.Runtime.InteropServices.DllImport("user32", SetLastError=true)]
public static extern IntPtr GetWindowLongPtr(IntPtr hWnd,
constants.windowLongIndices index);
 

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

Back
Top