Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8129137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:10:35+00:00 2026-06-06T08:10:35+00:00

I can get this to work: [<DllImport(user32.dll)>] extern bool GetClientRect(nativeint, RECT*) let getClientRect hwnd

  • 0

I can get this to work:

[<DllImport("user32.dll")>]
extern bool GetClientRect(nativeint, RECT*)

let getClientRect hwnd =
    let mutable r = RECT()
    if GetClientRect(hwnd, &&r) = false then
        raise <| System.Exception("GetClientRect failed")
    r

But for whatever reason, this just leaves r zero’d, with no exception thrown:

[<DllImport("user32.dll")>]
extern bool GetClientRect(nativeint, [<Out>] RECT rect)

let getClientRect hwnd =
    let mutable r = RECT()
    if GetClientRect(hwnd, r) = false then
        raise <| System.Exception("GetClientRect failed")
    r

Of course, the problem with using pointers is that I get the warning warning FS0051: The use of native pointers may result in unverifiable .NET IL code, and understandably so.

What could I be missing?

Edit: Even though it’s been answered already, for the record, the struct definition:

[<Struct>]
type RECT =
    val left:int
    val top:int
    val right:int
    val bottom:int
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:10:36+00:00Added an answer on June 6, 2026 at 8:10 am

    It’s not very well documented, but using the ampersand (&) 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# ref parameter; adding [<Out>] to the parameter gives the same signature as a C# out parameter.

    [<DllImport("user32.dll")>]
    extern bool GetClientRect(nativeint hWnd, [<Out>] RECT& rect)
    
    let getClientRect hwnd =
        let mutable r : RECT = Unchecked.defaultOf<_>
        if GetClientRect(hwnd, &r) = false then
            raise <| System.Exception("GetClientRect failed")
        r
    

    Notice that once you change the P/Invoke signature to use byref<RECT> instead of nativeptr<RECT>, you also need to change the address-of operator (&&) you used on r to a single ampersand (&).

    Since you’re only getting a value out of GetClientRect, it’s a good practice to initialize the mutable result value to Unchecked.defaultOf<_> so it’s clear that it’s meant to be overwritten by GetClientRect.

    EDIT: If changing the P/Invoke signature to use byref (&) 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).

    If you want to try that:

    [<DllImport("user32.dll")>]
    extern [<return: MarshalAs(UnmanagedType.Bool)>] bool GetClientRect(
        nativeint hWnd, [<Out>] RECT& rect)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't get this to work. I am trying to prevent a user to
I can't get this to work.. I have an String which I want to
im new at javascript and i can get this slider to work but not
Could someone please help explain why I can't get this to work? I properly
Very frustrated here. Can't get this to work and don't know why. I am
I am trying to get this to work but can't see where I'm going
I just cannot get this to work, would appreciate if someone can help. So
Can someone please shed some light on how I would get this to work:
can't seem to get this parser to work for google maps, I think its
I can't find any solution to get this jquery events to work. $(window).one('click',function(){ console.log('hide

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.