For the following code, I receive the warning Unboxing a possibly null value. (CS8605)
in VS Code with C# Dev Kit:
Original code with warning:
// Originally a struct
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct POLICY_DNS_DOMAIN_INFO {
public LSA_UNICODE_STRING Name;
public LSA_UNICODE_STRING DnsDomainName;
public LSA_UNICODE_STRING DnsForestName;
public GUID DomainGuid;
public IntPtr pSID;
}
// Additional code removed for simplicity
resultQueryPolicy = LsaQueryInformationPolicy(policyHandle,
POLICY_INFORMATION_CLASS.PolicyDnsDomainInformation,
out IntPtr infoStructure
);
winErrorCode = LsaNtStatusToWinError(resultQueryPolicy);
winErrorCode = LsaNtStatusToWinError(resultQueryPolicy);
if (winErrorCode == 0) {
POLICY_DNS_DOMAIN_INFO domain = (POLICY_DNS_DOMAIN_INFO)Marshal.PtrToStructure(infoStructure, typeof(POLICY_DNS_DOMAIN_INFO));
// Additional code removed for simplicity
}
Current working solution:
// I changed the struct to a class
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private class POLICY_DNS_DOMAIN_INFO {
public LSA_UNICODE_STRING Name;
public LSA_UNICODE_STRING DnsDomainName;
public LSA_UNICODE_STRING DnsForestName;
public GUID DomainGuid;
public IntPtr pSID;
}
// Additional code removed for simplicity
resultQueryPolicy = LsaQueryInformationPolicy(policyHandle,
POLICY_INFORMATION_CLASS.PolicyDnsDomainInformation,
out IntPtr infoStructure
);
winErrorCode = LsaNtStatusToWinError(resultQueryPolicy);
if (winErrorCode == 0) {
// I applied T?
POLICY_DNS_DOMAIN_INFO? domain = (POLICY_DNS_DOMAIN_INFO?)Marshal.PtrToStructure(infoStructure, typeof(POLICY_DNS_DOMAIN_INFO));
// Additional code removed for simplicity
}
I’m looking for an alternative solution (hopefully simple) without having to convert the struct
to a class
. Is this possible?