// 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));
} // 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);
}