Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null value for HINSTANCE #639

Closed
fernandanavamoya opened this issue Aug 5, 2022 · 5 comments · Fixed by #640
Closed

Null value for HINSTANCE #639

fernandanavamoya opened this issue Aug 5, 2022 · 5 comments · Fixed by #640
Labels
enhancement New feature or request

Comments

@fernandanavamoya
Copy link

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
@JeremyKuhne
Copy link
Member

In general this would be nice for the wrapper structs (HWND, HDC, etc.).

@elachlan
Copy link
Contributor

elachlan commented Aug 5, 2022

Maybe we can add it along side #610?

@elachlan
Copy link
Contributor

elachlan commented Aug 5, 2022

So pretty much:

public static readonly IntPtr Null;

@jnm2
Copy link
Contributor

jnm2 commented Aug 5, 2022

Wouldn't this be:

public static readonly HINSTANCE Null;

@elachlan
Copy link
Contributor

elachlan commented Aug 5, 2022

Yes. I realised this as I went to bed. I'll fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants