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

C#

 
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 
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 
manchukuo wrote:
one declaration and be able to pass any object


I've been thinking about this a little more as it seemed an obvious candidate for generics. This seems to work...
C#
// NativeMethods.cs

using System;
using System.Drawing;
using System.Runtime.InteropServices;

internal static class NativeMethods
{
    // http://msdn.microsoft.com/en-us/library/ms644950(v=vs.85).aspx
    /*
LRESULT WINAPI SendMessage(
__in  HWND hWnd,
__in  UINT Msg,
__in  WPARAM wParam,
__in  LPARAM lParam
);*/
    [DllImport("User32.dll", SetLastError = true)]
    public static extern IntPtr SendMessage(
        IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

    [DllImport("User32.dll", SetLastError = true)]
    public static extern IntPtr SendMessage(
        IntPtr hWnd, int Msg, IntPtr wParam, GCHandle lParam);

    /// <summary>
    /// Creates a pinned GCHandle from the supplied object of the specified type.
    /// </summary>
    /// <typeparam name="T">Type of object to pin.</typeparam>
    /// <param name="obj">The object to pin.</param>
    /// <returns>A pinned GCHandle.</returns>
    public static GCHandle CreateGCHandle<T>(T obj)
    {
        return GCHandle.Alloc(obj, GCHandleType.Pinned);
    }
    /// <summary>
    /// Gets the target of the pinned GCHandle and frees the handle.
    /// </summary>
    /// <typeparam name="T">Type of the pinned object.</typeparam>
    /// <param name="pointer">The GCHandle as an IntPtr.</param>
    /// <returns>The pinned object.</returns>
    public static T GetFromPointer<T>(IntPtr pointer)
    {
        GCHandle gcHandle = GCHandle.FromIntPtr(pointer);
        T result = (T)gcHandle.Target;
        gcHandle.Free();
        return result;
    }
}

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)



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 
AnswerRe: Random numbers Pin
lampiclobe10-May-11 11:56
lampiclobe10-May-11 11:56 
AnswerRe: Random numbers Pin
#realJSOP7-May-11 1:33
mve#realJSOP7-May-11 1:33 

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.