3

I'm getting this warning from the Code Analysis tool in Visual Studio 2012. The code looks like this:

using System;
using System.Runtime.InteropServices;

namespace MyProgramNamespace
{
    class NativeMethods
    {
        [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
        public static extern IntPtr GetWindowLongPtr(IntPtr handle, int flag);

        [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
        public static extern IntPtr SetWindowLongPtr(IntPtr handle, int flag, IntPtr ownerHandle);
    }
}

I'm compiling for x64 only so I'm not concerned with using the old GetWindowLong and SetWindowLong. These entry point names are correct as far as I can tell.

Edit: SOLVED. Turns out the problem is that Visual Studio itself (and therefore the Code Analysis tool) are 32bit. When the code analysis tool checks user32.dll to see if those functions are there, it checks the 32bit version of user32.dll (in C:/Windows/SysWOW64/) instead of the one that the program will actually use (the 64bit version in C:/Windows/System32), and these functions only exist in the 64bit version (32bit version uses GetWindowLong/SetWindowLong instead of GetWindowLongPtr/SetWindowLongPtr (notice the PTR part)).

3
  • 2
    This is conjecture, but maybe the Code Analysis Tool is only checking the 32 bit version of user32.dll. Get/SetWindowLongPtr don't exist in the 32 bit version of user32.dll. If the Code Analysis Tool is 32 bit itself that could be the reason.
    – shf301
    Oct 30, 2012 at 21:35
  • Also, if I comment out the bottom 2 lines (for SetWindowLongPtr), all warnings go away. Yet with those lines uncommented, both the Get and Set methods give the warning. If I comment out just the top 2 lines (Get), the Set still gives a warning Oct 30, 2012 at 21:37
  • 1
    @Jargon I would post the solution as an answer and mark it since none of the answers given were correct (including mine).
    – Tergiver
    Oct 30, 2012 at 21:56

3 Answers 3

1

The reason they don't work is that by specifying EntryPoint = in the DllImport attribute, you are telling the Marshaller, "This is the exact function I want you to call".

There is no function called GetWindowLongPtr in user32.dll. There is GetWindowLongPtrA and GetWindowLongPtrW.

When you leave out the EntryPoint=, the Marshaller will call one or the other based on the running OS.

So either leave it out, or specify the A or W versions. If you specify A or W, you will also want to specify CharSet=CharSet.Ansi for the A version or CharSet=CharSet.Unicode for the W version.

6
  • Tried removing the EntryPoint, doesn't fix it, and also tried adding the CharSet (tried A with Ansi and W with Unicode), none made the warning go away Oct 30, 2012 at 21:32
  • @Jargon Warning? Where is this warning coming from? Certainly not the compiler. Are you using some kind of code checking tool?
    – Tergiver
    Oct 30, 2012 at 21:33
  • @Jargon Then I googled it (msdn.microsoft.com/en-us/library/ms182208.aspx). It's from FxCop. Just disable the warning and test that the call succeeds at runtime.
    – Tergiver
    Oct 30, 2012 at 21:37
  • 1
    Yeah, I mentioned in the first line of my post that it's the Code Analysis tool in Visual Studio 2012 (which used to be FxCop in older versions). I could suppress the warning, but I'd rather know what's causing it to make sure I'm doing the interop correctly. Oct 30, 2012 at 21:40
  • 1
    That must be it. I copied user32.dll from the System32 folder and the warnings went away. If I copy user32.dll from SysWOW64 (the 32bit version) the warnings stay. I guess that's what I get for writing x64 code in an x86 IDE Oct 30, 2012 at 21:49
1

(This answer also posted in an edit at the bottom of the original question, to help people find it quick and easy)

Turns out the problem is that Visual Studio itself (and therefore the Code Analysis tool) are 32bit. When the code analysis tool checks user32.dll to see if those functions are there, it checks the 32bit version of user32.dll (in C:/Windows/SysWOW64/) instead of the one that the program will actually use (the 64bit version in C:/Windows/System32), and these functions only exist in the 64bit version (32bit version uses GetWindowLong/SetWindowLong instead of GetWindowLongPtr/SetWindowLongPtr (notice the PTR part)).

0

Try the following:

    [DllImport("user32.dll", EntryPoint = "GetWindowLongPtrW")]
    public static extern IntPtr GetWindowLongPtr(IntPtr handle, int flag);

    [DllImport("user32.dll", EntryPoint = "SetWindowLongPtrW")]
    public static extern IntPtr SetWindowLongPtr(IntPtr handle, int flag, IntPtr ownerHandle);
3
  • 1
    Tried it with the W and with an A as well (saw that somewhere). Neither works :S Oct 30, 2012 at 21:20
  • Yeah, ExactSpelling seems to make no difference Oct 30, 2012 at 21:31
  • My mistake, ExactSpelling is not necessary if EntryPoint is specified...I think this warning is normal, because similar warnings appear when I compile x64 assemblies (it warns about Reference Assemblies)
    – Jigsore
    Oct 30, 2012 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.