Note that there are some explanatory texts on larger screens.

plurals
  1. PO.NET Interop IntPtr vs. ref
    primarykey
    data
    text
    <p>Probably a noob question but interop isn't one of my strong points yet. </p> <p>Aside from limiting the number of overloads is there any reason I should declare my DllImports like:</p> <pre><code>[DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam); </code></pre> <p>And use them like this:</p> <pre><code>IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(formatrange)); Marshal.StructureToPtr(formatrange, lParam, false); int returnValue = User32.SendMessage(_RichTextBox.Handle, ApiConstants.EM_FORMATRANGE, wParam, lParam); Marshal.FreeCoTaskMem(lParam); </code></pre> <p>Rather than creating a targeted overload:</p> <pre><code>[DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref FORMATRANGE lParam); </code></pre> <p>And using it like:</p> <pre><code>FORMATRANGE lParam = new FORMATRANGE(); int returnValue = User32.SendMessage(_RichTextBox.Handle, ApiConstants.EM_FORMATRANGE, wParam, ref lParam); </code></pre> <p>The by ref overload ends up being easier to use but I'm wondering if there is a drawback that I'm not aware of.</p> <p><strong>Edit:</strong></p> <p>Lots of great info so far guys.</p> <p>@P Daddy: Do you have an example of basing the struct class off an abstract (or any) class? I changed my signature to:</p> <pre><code>[DllImport("user32.dll", SetLastError = true)] public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, [In, Out, MarshalAs(UnmanagedType.LPStruct)] CHARFORMAT2 lParam); </code></pre> <p>Without the <code>In</code>, <code>Out</code>, and <code>MarshalAs</code> the SendMessage (EM_GETCHARFORMAT in my test) fail. The above example works well but if I change it to:</p> <pre><code>[DllImport("user32.dll", SetLastError = true)] public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, [In, Out, MarshalAs(UnmanagedType.LPStruct)] NativeStruct lParam); </code></pre> <p>I get a System.TypeLoadException that says the CHARFORMAT2 format is not valid (I'll try and capture it for here).</p> <p>The exception:</p> <p>Could not load type 'CC.Utilities.WindowsApi.CHARFORMAT2' from assembly 'CC.Utilities, Version=1.0.9.1212, Culture=neutral, PublicKeyToken=111aac7a42f7965e' because the format is invalid.</p> <p>The NativeStruct class:</p> <pre><code>public class NativeStruct { } </code></pre> <p>I've tried <code>abstract</code>, adding the <code>StructLayout</code> attribute, etc. and I get the same exception.</p> <pre><code>[StructLayout(LayoutKind.Sequential)] public class CHARFORMAT2: NativeStruct { ... } </code></pre> <p><strong>Edit:</strong></p> <p>I didn't follow the FAQ and I asked a question that can be discussed but not positively answered. Aside from that there has been lot's of insightful information in this thread. So I'll leave it up to the readers to vote up an answer. First one to over 10 up-votes will be the answer. If no answer meets this in two days (12/17 PST) I'll add my own answer that summarizes all the yummy knowledge in the thread :-)</p> <p><strong>Edit Again:</strong></p> <p>I lied, accepting P Daddy's answer because he is the man and has been a great help (he has a cute little monkey too :-P)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload