Marshal & Hwnd - Please Help.

  • Thread starter Thread starter Juan Irigoyen
  • Start date Start date
J

Juan Irigoyen

I have the next code

byte[] data = new byte[14];
Marshal.Copy(this.Hwnd, data, 0, 4);

this code compiled well, but when i execute the code receive a error.

How can copy this.Hwnd (Handle of MessageWindow type IntPtr) to the first 4
bytes in data ?

Thanks.
 
Hi,
Your code tries to copy 4 bytes of data from the location pointed by IntPtr
(this.Hwnd).
If you want to copy the IntPtr value (not the data it points) you could use
such approach:

byte[] data = new byte[14];
int ptrValue= this.Hwnd.ToInt32();
data[0] = (byte)ptrValue;
data[1] = (byte)ptrValue >> 8;
data[2] = (byte)ptrValue >> 16;
data[3] = (byte)ptrValue >> 24;

--
Andrew Gnenny
pulsar2003@/no-spam/email.ru (Please remove /no-spam/ for reply)
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE
 
Sorry this code have n error, Cannot implicity convert type 'int' to 'byte'

Andrew Gnenny said:
Hi,
Your code tries to copy 4 bytes of data from the location pointed by IntPtr
(this.Hwnd).
If you want to copy the IntPtr value (not the data it points) you could use
such approach:

byte[] data = new byte[14];
int ptrValue= this.Hwnd.ToInt32();
data[0] = (byte)ptrValue;
data[1] = (byte)ptrValue >> 8;
data[2] = (byte)ptrValue >> 16;
data[3] = (byte)ptrValue >> 24;

--
Andrew Gnenny
pulsar2003@/no-spam/email.ru (Please remove /no-spam/ for reply)
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE


Juan Irigoyen said:
I have the next code

byte[] data = new byte[14];
Marshal.Copy(this.Hwnd, data, 0, 4);

this code compiled well, but when i execute the code receive a error.

How can copy this.Hwnd (Handle of MessageWindow type IntPtr) to the
first
4
bytes in data ?

Thanks.
 
Try this code:
byte[] data = new byte[14];
int ptrValue= this.Hwnd.ToInt32();
data[0] = (byte)ptrValue;
data[1] = (byte)(ptrValue >> 8);
data[2] = (byte)(ptrValue >> 16);
data[3] = (byte)(ptrValue >> 24);

--
Andrew Gnenny
pulsar2003@/no-spam/email.ru (Please remove /no-spam/ for reply)
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE


Juan Irigoyen said:
Sorry this code have n error, Cannot implicity convert type 'int' to 'byte'

Andrew Gnenny said:
Hi,
Your code tries to copy 4 bytes of data from the location pointed by IntPtr
(this.Hwnd).
If you want to copy the IntPtr value (not the data it points) you could use
such approach:

byte[] data = new byte[14];
int ptrValue= this.Hwnd.ToInt32();
data[0] = (byte)ptrValue;
data[1] = (byte)ptrValue >> 8;
data[2] = (byte)ptrValue >> 16;
data[3] = (byte)ptrValue >> 24;

--
Andrew Gnenny
pulsar2003@/no-spam/email.ru (Please remove /no-spam/ for reply)
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE


Juan Irigoyen said:
I have the next code

byte[] data = new byte[14];
Marshal.Copy(this.Hwnd, data, 0, 4);

this code compiled well, but when i execute the code receive a error.

How can copy this.Hwnd (Handle of MessageWindow type IntPtr) to the
first
4
bytes in data ?

Thanks.
 
Back
Top