Skip to content

Null value for HINSTANCE #639

@fernandanavamoya

Description

@fernandanavamoya

Is your feature request related to a problem? Please describe.
Inside a method that returns a HINSTANCE, the returning null value has to be explicitly cast to an HINSTANCE: (HINSTANCE)0 .

public unsafe static HINSTANCE LoadLibraryFromSystemPathIfAvailable(string libraryName)
{
   if (GetModuleHandle(Libraries.Kernel32) == 0)
   {
       return (HINSTANCE)0;
   }

    // LOAD_LIBRARY_SEARCH_SYSTEM32 was introduced in KB2533623. Check for its presence
    // to preserve compat with Windows 7 SP1 without this patch.
    fixed (char* lpLibFileName = libraryName)
    {
        HINSTANCE result = LoadLibraryEx(lpLibFileName, (HANDLE)0, LOAD_LIBRARY_FLAGS.LOAD_LIBRARY_SEARCH_SYSTEM32);
        if (result != 0)
        {
             return result;
        }

        // Load without this flag.
        if (Marshal.GetLastWin32Error() != ERROR.INVALID_PARAMETER)
        {
             return (HINSTANCE)0;
        }

        return LoadLibraryEx(lpLibFileName, (HANDLE)0, 0);
    }
}

Describe the solution you'd like
A null value for HINSTANCE, such as:

HINSTANCE.Null

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions