When running the solution in Debug configuration:
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
When running the solution in Release configuration:
System.AccessViolationException was unhandled
Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
When running the exe application:
Unhandled exception at 0x00000000 in XXXApplicationNameXXX.exe: 0xC0000005: Access violation reading location 0x00000000.
The solution to any of these issues is to PInvoke the method CeGetVersionEx() rather than the one provided in the RAPI class.
//PInvoke Declarations
[StructLayout(LayoutKind.Sequential,
CharSet = CharSet.Unicode)]
public struct CEOSVERSIONINFO
{
internal int dwOSVersionInfoSize;
public int dwMajorVersion;
public int dwMinorVersion;
public int dwBuildNumber;
public PlatformType dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szCSDVersion;
}
[DllImport("rapi.dll",
CharSet = CharSet.Unicode,
SetLastError = true)]
internal static extern int
CeGetVersionEx(ref CEOSVERSIONINFO lpVersionInformation);
//PInvoke Call
CEOSVERSIONINFO osVersionInfo = new CEOSVERSIONINFO();
CeGetVersionEx(ref osVersionInfo);
Source: opennetcf

