2

Consider a typical P/Invoke declaration like this:

[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool LookupAccountName(
string SystemName,
string accountName,
IntPtr pSid,
ref uint cbSid,
[MarshalAs(UnmanagedType.LPWStr)] StringBuilder ReferencedDomainName,
ref uint ReferencedDomainNameCount,
out SID_NAME_USE SIDUse);

MSDN documentation for LookupAccountName says that pSid and ReferencedDomainName may be nullptr if the customer wishes. Passing nullptr for pSid is easy; just pass IntPtr.Zero. But what should one pass for a StringBuilder?

I don't want to pass an empty StringBuilder, because I don't want this call to fail with ERROR_INSUFFICIENT_BUFFER.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552

1 Answers1

8

You can just pass null for this parameter; it will be marshalled as a null pointer.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758