For your P/Invoke declaration, the
wMsg
parameter should be an
Integer
, not a
Long
. The parameter is a 32-bit integer, whereas
Long
is a 64-bit integer.
The
wParam
parameter and return values should be
IntPtr
, since their size depends on whether you're running as a 32-bit or 64-bit process.
When you expect the function to update a string parameter, you should pass a
StringBuilder
. In .NET, the
String
type is immutable.
However, from what I've read, when the window belongs to another process, you have to allocate a buffer for the name in that process. This means you'll need to call
OpenProcess
,
VirtualAllocEx
,
SendMessage
,
ReadProcessMemory
, and finally
VirtualFreeEx
.
There's a two-part MSDN blog post from 2006 with the C# code to do this: