Dela via


/CLRSUPPORTLASTERROR (Preserve Last Error Code for PInvoke Calls)

/CLRSUPPORTLASTERROR, which is on by default, preserves the last error code of functions called through the P/Invoke mechanism, which allows you to call native functions in DLLS, from code compiled with /clr.

/CLRSUPPORTLASTERROR{:NO | SYSTEMDLL}

Remarks

Preserving the last error code implies a decrease in performance. If you do not want to incur the performance impact of preserving the last error code, link with /CLRSUPPORTLASTERROR:NO.

You can minimize the performance impact by linking with /CLRSUPPORTLASTERROR:SYSTEMDLL, which only preserves the last error code for functions in system DLLs. A system DLL is defined as one of the following:

ACLUI.DLL

ACTIVEDS.DLL

ADPTIF.DLL

ADVAPI32.DLL

ASYCFILT.DLL

AUTHZ.DLL

AVICAP32.DLL

AVIFIL32.DLL

CABINET.DLL

CLUSAPI.DLL

COMCTL32.DLL

COMDLG32.DLL

COMSVCS.DLL

CREDUI.DLL

CRYPT32.DLL

CRYPTNET.DLL

CRYPTUI.DLL

D3D8THK.DLL

DBGENG.DLL

DBGHELP.DLL

DCIMAN32.DLL

DNSAPI.DLL

DSPROP.DLL

DSUIEXT.DLL

GDI32.DLL

GLU32.DLL

HLINK.DLL

ICM32.DLL

IMAGEHLP.DLL

IMM32.DLL

IPHLPAPI.DLL

IPROP.DLL

KERNEL32.DLL

KSUSER.DLL

LOADPERF.DLL

LZ32.DLL

MAPI32.DLL

MGMTAPI.DLL

MOBSYNC.DLL

MPR.DLL

MPRAPI.DLL

MQRT.DLL

MSACM32.DLL

MSCMS.DLL

MSI.DLL

MSIMG32.DLL

MSRATING.DLL

MSTASK.DLL

MSVFW32.DLL

MSWSOCK.DLL

MTXEX.DLL

NDDEAPI.DLL

NETAPI32.DLL

NPPTOOLS.DLL

NTDSAPI.DLL

NTDSBCLI.DLL

NTMSAPI.DLL

ODBC32.DLL

ODBCBCP.DLL

OLE32.DLL

OLEACC.DLL

OLEAUT32.DLL

OLEDLG.DLL

OPENGL32.DLL

PDH.DLL

POWRPROF.DLL

QOSNAME.DLL

QUERY.DLL

RASAPI32.DLL

RASDLG.DLL

RASSAPI.DLL

RESUTILS.DLL

RICHED20.DLL

RPCNS4.DLL

RPCRT4.DLL

RTM.DLL

RTUTILS.DLL

SCARDDLG.DLL

SECUR32.DLL

SENSAPI.DLL

SETUPAPI.DLL

SFC.DLL

SHELL32.DLL

SHFOLDER.DLL

SHLWAPI.DLL

SISBKUP.DLL

SNMPAPI.DLL

SRCLIENT.DLL

STI.DLL

TAPI32.DLL

TRAFFIC.DLL

URL.DLL

URLMON.DLL

USER32.DLL

USERENV.DLL

USP10.DLL

UXTHEME.DLL

VDMDBG.DLL

VERSION.DLL

WINFAX.DLL

WINHTTP.DLL

WININET.DLL

WINMM.DLL

WINSCARD.DLL

WINTRUST.DLL

WLDAP32.DLL

WOW32.DLL

WS2_32.DLL

WSNMP32.DLL

WSOCK32.DLL

WTSAPI32.DLL

XOLEHLP.DLL

Note

Preserving the last error is not supported for unmanaged functions that are consumed by CLR code, in the same module.

To set this linker option in the Visual Studio development environment

  1. Open the project's Property Pages dialog box. For details, see Setting Visual C++ Project Properties.

  2. Click the Linker folder.

  3. Click the Command Line property page.

  4. Type the option into the Additional Options box.

To set this linker option programmatically

Example

The following sample defines a native DLL with one exported function that modifies last error.

// CLRSUPPORTLASTERROR_dll.cpp
// compile with: /LD
#include <windows.h>
#include <math.h>

#pragma unmanaged
__declspec(dllexport) double MySqrt(__int64 n) {
   SetLastError(DWORD(-1));
   return sqrt(double(n));
}

The following sample consumes the DLL, demonstrating how to use /CLRSUPPORTLASTERROR.

// CLRSUPPORTLASTERROR_client.cpp
// compile with: /clr CLRSUPPORTLASTERROR_dll.lib /link /clrsupportlasterror:systemdll
// processor: x86
#include <windows.h>
#include <wininet.h>
#include <stdio.h>
#include <math.h>

#pragma comment(lib, "wininet.lib")

double MySqrt(__int64 n);

#pragma managed
int main() {
   double   d = 0.0;
   __int64 n = 65;
   HANDLE  hGroup = NULL;
   GROUPID groupID;
   DWORD   dwSet = 127, dwGet = 37;

   SetLastError(dwSet);
   d = MySqrt(n);
   dwGet = GetLastError();

   if (dwGet == DWORD(-1))
      printf_s("GetLastError for application call succeeded (%d).\n",
             dwGet);
   else
      printf_s("GetLastError for application call failed (%d).\n",
             dwGet);

   hGroup = FindFirstUrlCacheGroup(0, CACHEGROUP_SEARCH_ALL,
                           0, 0, &groupID, 0);
   dwGet = GetLastError();
   if (dwGet == 183)
      printf_s("GetLastError for system call succeeded (%d).\n",
             dwGet);
   else
      printf_s("GetLastError for system call failed (%d).\n",
             dwGet);
}
GetLastError for application call failed (127).
GetLastError for system call succeeded (183).

See Also

Reference

Setting Linker Options

Linker Options