3

I am trying to use GetWindowLongPtrA but I keep getting the "Unable to find an entry point named 'GetWindowLongPtrA' in DLL 'user32.dll'". (also SetWindowLongPtrA getting the same error). I've tried many solutions found on Google, but they didn't solve it.

Here is the declaration of the function I've written:

[DllImport("user32.dll")]
public static extern IntPtr GetWindowLongPtrA(IntPtr hWnd, int nIndex);

Tried to put EntryPoint = "GetWindowLongPtrA", changed GetWindowLongPtrA to GetWindowLongPtr, put CharSet = CharSet.Ansi, switched to GetWindowLongPtrW with CharSet = CharSet.Unicode etc., They all didn't work.

My computer is exactly "64-bit" (but cannot call that 64-bit WinAPI function?). the OS is Windows 10.

[1]: https://i.stack.imgur.com/3JrGw.png

But my system drive is running out of free space. Is this a possible cause? enter image description here

What is the solution for this problem?

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
armamoyl
  • 145
  • 1
  • 7
  • 2
    Isn't the function called GetWindowLongPtr? And I think if you target 32 bit process then youbshould call GetWindowLong. – David Heffernan Feb 22 '19 at 19:48
  • I tried GetWindowLongPtr but didn't work too. Yes, GetWindowLong did work but given that my system is 64-bit so I think it'd be better to use GetWindowLong. I've tried using GetWindowLongPtr to remove the maximize & minimize button of another window but it didn't work (these button still stayed there) so I think I should use the "Ptr" version. – armamoyl Feb 22 '19 at 20:01
  • 2
    *My computer is exactly "64-bit"* - but your procees is i guess 32 bit. only the process bitness play role here – RbMm Feb 22 '19 at 20:14
  • `GetWindowLongPtrA` and `GetWindowLongPtrW` is correct name for 64bit process. `GetWindowLongA` and `GetWindowLongW` for 32. and you need correct choose A or W depend are window is unicode currentrly – RbMm Feb 22 '19 at 20:21
  • Okay, I just change the build target to 64-bit and that works. Really really thanks :) – armamoyl Feb 22 '19 at 20:22
  • 1
    Running out of space can cause just about anything. You need to attend to that first. Space reclamation: **``1)``** [Quick Options](https://serverfault.com/a/642178/20599), **``2)``** [Detailed Options](https://stackoverflow.com/a/49347648/129130). Just dump your Downloads and Media folders onto a low-profile USB drive or SD-Disk? Run ``cleanmgr.exe`` afterwards and maybe compression (for a fast SSD drive). – Stein Åsmul Feb 22 '19 at 20:28
  • 3
    Not the source of your problem, but the P-Invoke marshaller knows how to handle the A/W business and you are getting in its way. When you declare the function as `GetWindowLongPtr` and assign `CharSet = CharSet.Auto`, it will correctly pick the `W` or `A` version and handle the string conversions. – GSerg Feb 22 '19 at 20:29

1 Answers1

6

There is no function named GetWindowLongPtr, GetWindowLongPtrA or GetWindowLongPtrW in the 32-bit version of user32.dll:

32-bit user32.dll

The reason that using GetWindowLongPtr regardless of target bitness works C and C++ WinAPI code is that in 32-bit code it's a macro that calls GetWindowLong(A|W). It only exists in the 64-bit version of user32.dll:

64-bit user32.dll

The documentation for importing GetWindowLongPtr on pinvoke.net includes a code sample for how to make this importation transparent to target bitness (remember, the error is thrown when you actually try to call the imported function that doesn't exist, not on the DllImport line):

[DllImport("user32.dll", EntryPoint="GetWindowLong")]
private static extern IntPtr GetWindowLongPtr32(IntPtr hWnd, int nIndex);

[DllImport("user32.dll", EntryPoint="GetWindowLongPtr")]
private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);

// This static method is required because Win32 does not support
// GetWindowLongPtr directly
public static IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex)
{
     if (IntPtr.Size == 8)
     return GetWindowLongPtr64(hWnd, nIndex);
     else
     return GetWindowLongPtr32(hWnd, nIndex);
}
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85