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 181965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:51:23+00:00 2026-05-11T14:51:23+00:00

On the MSDN I’ve found the following description for the two attributes: PreserveSig Set

  • 0

On the MSDN I’ve found the following description for the two attributes:

PreserveSig Set the PreserveSig field to true to directly translate unmanaged signatures with HRESULT or retval values; set it to false to automatically convert HRESULT or retval values to exceptions. By default, the PreserveSig field is true.

SetLastError Enables the caller to use the Marshal.GetLastWin32Error API function to determine whether an error occurred while executing the method. In Visual Basic, the default is true (which adds some overhead); in C# and C++, the default is false.

My question is: How these two relate to each other? Suppose I have PreserveSig set to ‘false’ – it means that I should have HRESULT converted to exception – if unmanaged function returns integer indicating that error or no error occured, how could this be translated to exception?

Also why do I need to call GetLastWin32Error method if I somehow managed to extract the exception using PreserveSig?

Kind regards PK

  • 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. 2026-05-11T14:51:24+00:00Added an answer on May 11, 2026 at 2:51 pm

    Win32 functions almost never return a HRESULT. Instead they return a BOOL or use special values to indicate error (e.g. CreateFile returns INVALID_HANDLE_VALUE). They store the error code in a per-thread variable, which you can read with GetLastError(). SetLastError=true instructs the marshaler to read this variable after the native function returns, and stash the error code where you can later read it with Marshal.GetLastWin32Error(). The idea is that the .NET runtime may call other Win32 functions behind the scenes which mess up the error code from your p/invoke call before you get a chance to inspect it.

    Functions which return a HRESULT (or equivalent, e.g. NTSTATUS) belong to a different level of abstraction than Win32 functions. Generally these functions are COM-related (above Win32) or from ntdll (below Win32), so they don’t use the Win32 last-error code (they might call Win32 functions internally, though).

    PreserveSig=false instructs the marshaler to check the return HRESULT and if it’s not a success code, to create and throw an exception containing the HRESULT. The managed declaration of your DllImported function then has void as its return type.

    Remember, the C# or VB compiler cannot check the DllImported function’s unmanaged signature, so it has to trust whatever you tell it. If you put PreserveSig=false on a function which returns something other than a HRESULT, you will get strange results (e.g. random exceptions). If you put SetLastError=true on a function which does not set the last Win32 error code, you will get garbage instead of a useful error code.

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

Sidebar

Related Questions

MSDN states the following SortedSet(T).Add Method : If Count is less than the capacity
MSDN provides a digit placeholder example: 1234.5678 (#####) -> 1235 I found this confusing
MSDN gives the following warning about the lock keyword in C#: In general, avoid
MSDN is rather vague regarding this attribute. The custom data string can be set
MSDN defines System.Predicate this way : Represents the method that defines a set of
MSDN displays the following for CreatePatternBrush: You can delete a pattern brush without affecting
MSDN defines System.Enum as an abstract class: [SerializableAttribute] [ComVisibleAttribute(true)] public abstract class Enum :
MSDN gives the following Javascript code for querying the Bing Image Search API. It
The MSDN states that the method returns true if the method is successfully queued;
MSDN states that String.Intern retrieves the system's reference to the specified String and String.IsInterned

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.