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

  • Home
  • SEARCH
  • 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 3438426
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:11:11+00:00 2026-05-18T08:11:11+00:00

For example, this is from .NET Framework source file UnsafeNativeMethods.cs : [DllImport(ExternDll.User32, ExactSpelling=true, CharSet=CharSet.Auto)]

  • 0

For example, this is from .NET Framework source file UnsafeNativeMethods.cs:

[DllImport(ExternDll.User32, ExactSpelling=true, CharSet=CharSet.Auto)] 
public static extern bool GetWindowRect(HandleRef hWnd, 
    [In, Out] ref NativeMethods.RECT rect);

and this is from PInvoke.Net:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(HandleRef hwnd, out RECT lpRect);
  1. Which is the correct/best signature for this function? (only one of them has [return: MarshalAs(UnmanagedType.Bool)], or [In, Out] ref, etc.)

  2. I’ve noticed that in .NET Framework source files many/most signatures have ExactSpelling=true, CharSet=CharSet.Auto, but on PInvoke they don’t. Is this required?

  • 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-05-18T08:11:12+00:00Added an answer on May 18, 2026 at 8:11 am

    They will both get the job done. There’s just more than one way to skin a pinvoke cat. Specifically for this example:

    • ExactSpelling=true is an optimization, it avoids having the pinvoke marshaller looking for the GetWindowRectA and GetWindowRectW versions. They don’t exist for this particular API function since it doesn’t take a string argument. Seeing an actual difference in run time would be a miracle.

    • CharSet=CharSet.Auto is always a good idea since the default (Ansi) is so inefficient. It just so happens to not make any difference here since the function doesn’t take any string arguments.

    • [In, Out] is unnecessary because that’s the default for a blittable type. An expensive word that means that the pinvoke marshaller can directly pass a pointer to the managed memory, no conversion is required. As efficient as possible. Same idea as CharSet though, being explicit about it helps to create self-documenting code and to remember to deal with the unusual case. Being able to only use [In] or [Out] can be a significant optimization, just not here since it is already optimized. Fwiw, [Out] would have been the correct choice.

    • out vs ref, same idea as above. Using out is more correct since the API doesn’t actually use any passed-in values inside the RECT. It doesn’t however make any difference at runtime since the JIT compiler always initializes a struct anyway.

    • [return: MarshalAs(UnmanagedType.Bool)] is unnecessary, it is the default marshaling for a Windows BOOL. Not sure why pinvoke.net always includes it.

    So in a nutshell, neither is perfect but they both will work. Such are the hazards of pinvoke.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small question: For example I'm using System.IO.File.Copy() method from .NET Framework.
This is an Example from MSDN about Object Class in .NET FrameWork. using namespace
This example is from php.net: <?php function Test() { static $a = 0; echo
This is from an example accompanying the agsXMPP .Net assembly. I've read up on
i googled the phrase lightweight ORM for j2ee and find this page http://java-source.net/open-source/persistence from
In this example from The Ruby Programming Language (p.270), I'm confused why the instance_eval
In this example from the App Engine docs , why does the example declare
With regards this example from Code Complete: Comparison Compare(int value1, int value2) { if
Why in this example from MSDN, in GetEnumerator method, PeopleEnum returns IEnumerator ? public
I tried to copy this example from this Multiprocessing lecture by jesse noller (as

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.