Search code examples
c#.netwinapipinvoke

Safety and stability of SetParent() and GetParent()


I'm using the following functions in my program:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr GetParent(IntPtr hWnd);

My question is: is this approach safe and stable? My development machine is Windows XP and so far I didn't find any problem with its use. I wonder if I can have issues in different versions (e.g. Windows Vista, Windows 7, whatever), different cpus (x64), etc. Can you guys provide some feedback on this and if there are some potencial issues how can I overcome them?

Thanks in advance.

Zubrowka


Solution

  • Those P/invoke declarations are correct for all versions of Windows. No problems with XP/Vista/7 and no 32/64 bit issues. IntPtr floats between 32 and 64 bit depending on the bitness of the process. That's exactly what you need to happen for an HWND.