Click here to Skip to main content
15,887,746 members
Home / Discussions / C#
   

C#

 
QuestionCan't publish a project even though no build errors Pin
Nathan D Cook6-May-11 9:31
Nathan D Cook6-May-11 9:31 
AnswerRe: Can't publish a project even though no build errors Pin
AspDotNetDev6-May-11 9:39
protectorAspDotNetDev6-May-11 9:39 
Questionpass a Size object to sendmessage Pin
manchukuo6-May-11 7:37
manchukuo6-May-11 7:37 
AnswerRe: pass a Size object to sendmessage Pin
DaveyM696-May-11 8:05
professionalDaveyM696-May-11 8:05 
GeneralRe: pass a Size object to sendmessage Pin
manchukuo6-May-11 8:12
manchukuo6-May-11 8:12 
GeneralRe: pass a Size object to sendmessage Pin
DaveyM696-May-11 8:27
professionalDaveyM696-May-11 8:27 
GeneralRe: pass a Size object to sendmessage [modified] Pin
manchukuo6-May-11 8:30
manchukuo6-May-11 8:30 
GeneralRe: pass a Size object to sendmessage [modified] Pin
DaveyM696-May-11 9:26
professionalDaveyM696-May-11 9:26 
That is the purpose of an IntPtr. When passing data using unmanaged memory a portion of that memory should be allocated to holding the data. The pointer to that memory location is then passed to the function. On the receiving side of the message the pointer is retrieved and the data read at that location then the memory freed. There are several ways to do this, the easiest is a combination of the BitConverter and Marshal classes.

Something like this will convert your Size to/from an IntPtr:
public static IntPtr SizeToPointer(Size size)
{
    byte[] bytes = new byte[8];
    Array.Copy(BitConverter.GetBytes(size.Width), 0, bytes, 0, 4);
    Array.Copy(BitConverter.GetBytes(size.Height), 0, bytes, 4, 4);
    IntPtr result = Marshal.AllocHGlobal(8);
    Marshal.Copy(bytes, 0, result, 8);
    return result;
}
public static Size PointerToSize(IntPtr pointer)
{
    byte[] bytes = new byte[8];
    Marshal.Copy(pointer, bytes, 0, 8);
    Size result = new Size(BitConverter.ToInt32(bytes, 0), BitConverter.ToInt32(bytes, 4));
    Marshal.FreeHGlobal(pointer);
    return result;
}

It is very important that the memory is freed! I have done this in the PointerToSize function. If this is not called then the memory will leak.

[Edit]
I have just tested this with two forms. With both forms shown, this in form1:
C#
NativeMethods.SendMessage(
    form2.Handle, 100, IntPtr.Zero, NativeMethods.SizeToPointer(
        new Size(123, 456)));

and this in form2
C#
protected override void WndProc(ref Message m)
{
    if (m.Msg == 100)
    {
        Size size = NativeMethods.PointerToSize(m.LParam);
        MessageBox.Show(size.ToString());
    }
    base.WndProc(ref m);
}

The 100 was just a random number I chose for the message - it will be important to use numbers that Windows doesn't already use!
[/Edit]
[Edit2]
Check this[^] out to make sure you don't clash your message numbers.
[/Edit2]
Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



modified on Friday, May 6, 2011 3:34 PM

GeneralRe: pass a Size object to sendmessage Pin
DaveyM696-May-11 10:13
professionalDaveyM696-May-11 10:13 
GeneralRe: pass a Size object to sendmessage Pin
DaveyM696-May-11 11:11
professionalDaveyM696-May-11 11:11 
GeneralRe: pass a Size object to sendmessage Pin
manchukuo6-May-11 11:14
manchukuo6-May-11 11:14 
QuestionRandom numbers [modified] Pin
meet_ssr6-May-11 2:46
meet_ssr6-May-11 2:46 
AnswerRe: Random numbers Pin
PIEBALDconsult6-May-11 2:52
mvePIEBALDconsult6-May-11 2:52 
GeneralRe: Random numbers PinPopular
David19876-May-11 2:56
David19876-May-11 2:56 
AnswerRe: Random numbers Pin
Luc Pattyn6-May-11 3:06
sitebuilderLuc Pattyn6-May-11 3:06 
GeneralRe: Random numbers PinPopular
David19876-May-11 3:45
David19876-May-11 3:45 
AnswerRe: Random numbers Pin
Luc Pattyn6-May-11 3:52
sitebuilderLuc Pattyn6-May-11 3:52 
GeneralRe: Random numbers PinPopular
David19876-May-11 3:57
David19876-May-11 3:57 
GeneralRe: Random numbers Pin
PIEBALDconsult6-May-11 16:19
mvePIEBALDconsult6-May-11 16:19 
GeneralRe: Random numbers Pin
David19876-May-11 21:41
David19876-May-11 21:41 
AnswerRe: Random numbers Pin
Orcun Iyigun6-May-11 8:37
Orcun Iyigun6-May-11 8:37 
AnswerRe: Random numbers Pin
Prasanta_Prince6-May-11 15:32
Prasanta_Prince6-May-11 15:32 
GeneralRe: Random numbers Pin
meet_ssr6-May-11 22:30
meet_ssr6-May-11 22:30 
AnswerRe: Random numbers Pin
lampiclobe7-May-11 0:49
lampiclobe7-May-11 0:49 
GeneralRe: Random numbers Pin
meet_ssr8-May-11 20:54
meet_ssr8-May-11 20:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.