Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Kernel32.GetModuleHandle should return IntPtr instead of SafeHandle #563

Closed
HavenDV opened this issue Jan 20, 2021 · 7 comments · Fixed by #567
Closed

Kernel32.GetModuleHandle should return IntPtr instead of SafeHandle #563

HavenDV opened this issue Jan 20, 2021 · 7 comments · Fixed by #567
Labels

Comments

@HavenDV
Copy link

HavenDV commented Jan 20, 2021

The GetModuleHandle function returns a handle to a mapped module without incrementing its reference count. However, if this handle is passed to the FreeLibrary function, the reference count of the mapped module will be decremented. Therefore, do not pass a handle returned by GetModuleHandle to the FreeLibrary function. Doing so can cause a DLL module to be unmapped prematurely.

@zgabi
Copy link
Contributor

zgabi commented Jan 22, 2021

Maybe it should retun a SafeHandle with ownsHandle=false.

@AArnott AArnott closed this as completed Jan 22, 2021
@AArnott
Copy link
Collaborator

AArnott commented Jan 22, 2021

@zgabi That would be great if .NET offered a way to do that. AFAIK it does not.

@zgabi
Copy link
Contributor

zgabi commented Jan 22, 2021

@AArnott : you mean that the .net marshalling can't reate SafeHandle with ownsHandle=false?

in this case a private method shoudl created (with a differnt name or in an internal class):
private static extern IntPtr GetModuleHandle(string lpModuleName);
And create a wrapper:

        public static SafeLibraryHandle GetModuleHandle(string lpModuleName)
        {
            return new SafeLibraryHandle(GetModuleHandleInt(lpModuleName), false);
        }

Or create a request to MS to allow to return a SafeHandle with ownsHandle=false with an attribute :)

@AArnott
Copy link
Collaborator

AArnott commented Jan 22, 2021

you mean that the .net marshalling can't reate SafeHandle with ownsHandle=false

Yes, that's my meaning.

Yes, a wrapper method can help resolve this problem. You could do this in your own code as well.

@8
Copy link

8 commented Feb 18, 2021

Hi,

I just stumbled over the same bug and found this issue and that it is already fixed via IntPtr return.

Is there some release available with this fix included that I could use?
I am using:

<PackageReference Include="PInvoke.User32" Version="0.7.78" />

and this still returns the safe handle and I've looked for a prerelease version, but didn't find any.

Is there something more that needs to be done, before it can be released that I am missing?

Thanks for the great project!

Take care,
Martin

@AArnott
Copy link
Collaborator

AArnott commented Feb 18, 2021

Sure. Watch for 0.7.104 to release to nuget shortly.

@8
Copy link

8 commented Feb 19, 2021

Thank you!
Thanks a lot for your work - the library is great productivity booster!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants