简体   繁体   中英

How does one pass nullptr through P/Invoke declared with a StringBuilder?

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM