Results 1 to 3 of 3

Thread: Casting IntPtr to COM Interface

  1. #1

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Casting IntPtr to COM Interface

    Hello, can anyone tell me how to cast IntPtr to a COM Interface?

    I'm obtaining a pointer to an IShellFolder interface using SHGetDesktopFolder:

    Code:
    Dim sfiRoot As IntPtr = IntPtr.Zero;
    Dim hresult As Integer = ShellAPI.SHGetDesktopFolder(sfiRoot);
    Now I want to get an IShellFolder instance out of this pointer.

    I know that I can change the declaration of SHGetDesktopFolder's parameter to IShellFolder instead of IntPtr, but for a number of reasons I need to keep it as IntPtr, moreover, I get the feeling that I will need to cast pointers to interfaces and back several times.

    Basically, I need to know what it takes to get an instance of an Interface from IntPtr and back - get the pointer (IntPtr) out of Interface instance:

    Code:
    interface IExample
    ...
    
    Dim ptr As IntPtr = SomeGetPointerMethod();
    
    Dim inst As IExample = ???? ' cast ptr to IExample
    Thank you.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Casting IntPtr to COM Interface


  3. #3

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Casting IntPtr to COM Interface

    Quote Originally Posted by .paul. View Post
    I looked, there appears to be 2 candidates:
    GetTypedObjectForIUnknown and GetObjectForIUnknown, will try both. Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width