Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8085235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:03:08+00:00 2026-06-05T18:03:08+00:00

I wrote a class that implements the IGroupPolicyObject Interface in COM. one of the

  • 0

I wrote a class that implements the IGroupPolicyObject Interface in COM.
one of the method now returns IntPtr object in C#:

IntPtr ghKey = objectGPolicy.GetRegistryKey(GpoSectionMachine);

I want to derefrence that pointer when using the RegistryKey object that belongs to Microsot.Win32 namespace.

This DOESN’T work:

(RegistryKey)ghKey = rootKey.CreateSubKey(policyPath);

I can’t find how to derefrence an IntPtr. Can someone help please?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:03:09+00:00Added an answer on June 5, 2026 at 6:03 pm

    IGroupPolicyObject.GetRegistryKey doesn’t return a .NET RegistryKey object, it returns a Win32 registry key handle (an HKEY). There are two useful things you can do with an IntPtr value that contains a native HKEY. First, you can pass it to other Win32 registry access functions. If you use it this way, you also need to manually close it by calling RegCloseKey when you’re done, or it will leak the handle.

    As @HansPassant helpfully points out in his comment, your second (and probably better) option is to turn it into a SafeRegistryHandle and use it to get a RegistryKey object:

    var hkey = gpo.GetRegistryKey(GpoSectionMachine);
    var safeHandle = new SafeRegistryHandle(hkey, true);
    var reg = RegistryKey.FromHandle(safeHandle);
    

    If you wrote a managed implementation of IGroupPolicyObject and it returns an actual RegistryKey object from this function, then:

    1. You did it wrong and your implementation is not portable to any other consumer of IGroupPolicyObject, and
    2. You must have manually marshalled your RegistryKey object into an IntPtr in your implementation, so just do the opposite to get it back out. (But really — don’t do that, you are breaking the COM contract by returning the wrong type.)

    An implementation of this method should instead be written to return a SafeRegistryHandle (the marshaler will, I believe, automatically convert between SafeHandle and an unmanaged handle). RegistryKey.Handle is one of these so you can use the C# classes right up until you need to return something from your implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://ideone.com/u0bVy the link above contain the code that i wrote for a class project.I
We wrote a small Windows class library that implements extension methods for some standard
i wrote a simple class that derives QPushButton, and tried to apply stylesheet on
So I recently wrote a java class that extends exception and used an instance
My main solution is using code from a utility class library, that I wrote
I wrote a widget like this: public class GroupLbl extends Composite implements ClickHandler, MouseOutHandler
I wrote a class (let's call it Model.java) that contains a Deque<T> , with
I wrote a UnitOfWork implementation that does not expose a public Commit() method. Instead
I wrote a simple collection class so that I can store my arrays in
I wrote an app that implements a web server with multithreading. The main activity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.