Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's not very well documented, but using the ampersand (&amp;) symbol in an F# P/Invoke signature is the correct (and best) way to pass a value by reference. It compiles to the same type signature as a C# <code>ref</code> parameter; adding <code>[&lt;Out&gt;]</code> to the parameter gives the same signature as a C# <code>out</code> parameter.</p> <pre><code>[&lt;DllImport("user32.dll")&gt;] extern bool GetClientRect(nativeint hWnd, [&lt;Out&gt;] RECT&amp; rect) let getClientRect hwnd = let mutable r : RECT = Unchecked.defaultOf&lt;_&gt; if GetClientRect(hwnd, &amp;r) = false then raise &lt;| System.Exception("GetClientRect failed") r </code></pre> <p>Notice that once you change the P/Invoke signature to use <code>byref&lt;RECT&gt;</code> instead of <code>nativeptr&lt;RECT&gt;</code>, you also need to change the address-of operator (&amp;&amp;) you used on <code>r</code> to a single ampersand (&amp;).</p> <p>Since you're only getting a value <em>out</em> of <code>GetClientRect</code>, it's a good practice to initialize the mutable result value to <code>Unchecked.defaultOf&lt;_&gt;</code> so it's clear that it's meant to be overwritten by <code>GetClientRect</code>.</p> <p>EDIT: If changing the P/Invoke signature to use byref (&amp;) doesn't work, you could also try explicitly specifying the marshalling behavior on the return type. The MSDN documentation for GetClientRect says it returns a BOOL, which is not quite the same as a .NET bool (BOOL is a 4-byte integer value).</p> <p>If you want to try that:</p> <pre><code>[&lt;DllImport("user32.dll")&gt;] extern [&lt;return: MarshalAs(UnmanagedType.Bool)&gt;] bool GetClientRect( nativeint hWnd, [&lt;Out&gt;] RECT&amp; rect) </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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