Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Win32 functions almost never return a <code>HRESULT</code>. Instead they return a <code>BOOL</code> or use special values to indicate error (e.g. <code>CreateFile</code> returns <code>INVALID_HANDLE_VALUE</code>). They store the error code in a per-thread variable, which you can read with <code>GetLastError()</code>. <code>SetLastError=true</code> instructs the marshaler to read this variable after the native function returns, and stash the error code where you can later read it with <code>Marshal.GetLastWin32Error()</code>. 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.</p> <p>Functions which return a <code>HRESULT</code> (or equivalent, e.g. <code>NTSTATUS</code>) belong to a different level of abstraction than Win32 functions. Generally these functions are COM-related (above Win32) or from <code>ntdll</code> (below Win32), so they don't use the Win32 last-error code (they might call Win32 functions internally, though). </p> <p><code>PreserveSig=false</code> instructs the marshaler to check the return <code>HRESULT</code> and if it's not a success code, to create and throw an exception containing the <code>HRESULT</code>. The managed declaration of your <code>DllImport</code>ed function then has <code>void</code> as its return type.</p> <p>Remember, the C# or VB compiler cannot check the <code>DllImport</code>ed function's unmanaged signature, so it has to trust whatever you tell it. If you put <code>PreserveSig=false</code> on a function which returns something other than a <code>HRESULT</code>, you will get strange results (e.g. random exceptions). If you put <code>SetLastError=true</code> on a function which does not set the last Win32 error code, you will get garbage instead of a useful error code.</p>
    singulars
    1. This table or related slice is empty.
    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