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 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

Ask A Question

Stats

  • Questions 272k
  • Answers 273k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I had problems installing Delphi 2007 from my original DVD… May 13, 2026 at 2:03 pm
  • Editorial Team
    Editorial Team added an answer select sum(rows) from sys.partitions; This is a correct answer, for… May 13, 2026 at 2:03 pm
  • Editorial Team
    Editorial Team added an answer You're running Ubuntu? How about just sudo apt-get install libapache2-mod-wsgi… May 13, 2026 at 2:03 pm

Related Questions

On the MSDN I've found the following description for the two attributes: PreserveSig Set
I have the following directory structure Project \Images +view.png control.xaml and in the control
I have been building a client / server app with Silverlight, web services, and
Hi i have a linq query below var Free = (from row in dt.AsEnumerable()
I'm trying to implement an HTML Parsing web service as described in Chapter 23

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.