dahall / Vanara

A set of .NET libraries for Windows implementing PInvoke calls to many native Windows APIs with supporting wrappers.
MIT License
1.81k stars 196 forks source link

False-positive `IsNullOrEmpty` on `StrPtrAuto` #455

Closed sovdchains closed 7 months ago

sovdchains commented 7 months ago

Describe the bug and how to reproduce

When subscribing to service creation via AdvApi32.NotifyServiceStatusChange and AdvApi32.SERVICE_NOTIFY_FLAGS.SERVICE_NOTIFY_CREATED the returning structures pszServiceNames contains a false-positive IsNullOrEmpty even though the value is neither null nor empty. The IsNull check is okay, it returns false. But the check for string.Empty returns true, which leads to the false-positive. Checking for IsNull, then retrieving the string and checking the returned string for string.Empty myself gives the desired result.

What code is involved

private void HandleServiceStatusChange(IntPtr pParameter)
{
    AdvApi32.SERVICE_NOTIFY_2 serviceNotify = pParameter.ToStructure<AdvApi32.SERVICE_NOTIFY_2>();

    if (!serviceNotify.pszServiceNames.IsNullOrEmpty)
        Console.WriteLine(serviceNotify.pszServiceNames.ToString());
}

Expected behavior

Returning false on non-empty values.

dahall commented 7 months ago

Fixed for 4.0.1