Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Interoperatioin with Shell32.dll COMObject...

Interoperatioin with Shell32.dll COMObject...

Scheduled Pinned Locked Moved C#
csharpvisual-studiohelpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Feng Qin
    wrote on last edited by
    #1

    I want to use COMObject in Shell32.dll, and I have ran "Regsvr32 -i shell32.dll", but when I create the object, vs.net tell me "ComObject is not valid or registered." Following is my code: try { Shell32.ShellLinkObjectClass link = new Shell32.ShellLinkObjectClass(); link.Path = "C:\\boot.ini"; link.Save("D:\\Mylink.lnk"); } catch( System.Runtime.InteropServices.COMException ex ) { MessageBox.Show( ex.Message ); } Thanks for your help!:rose: I'm amumu, and you?

    S 1 Reply Last reply
    0
    • F Feng Qin

      I want to use COMObject in Shell32.dll, and I have ran "Regsvr32 -i shell32.dll", but when I create the object, vs.net tell me "ComObject is not valid or registered." Following is my code: try { Shell32.ShellLinkObjectClass link = new Shell32.ShellLinkObjectClass(); link.Path = "C:\\boot.ini"; link.Save("D:\\Mylink.lnk"); } catch( System.Runtime.InteropServices.COMException ex ) { MessageBox.Show( ex.Message ); } Thanks for your help!:rose: I'm amumu, and you?

      S Offline
      S Offline
      Stephane Rodriguez
      wrote on last edited by
      #2

      I am afraid you have to use interop services. See this thread on Google. From what I read in MSDN, you'll have to call the WIN32 API SHGetDesktopFolder() to retrieve the IShellFolder interface, then use either a direct cast to (IShellLinkObject) or use IShellFolder::GetUIObjectOf(...). Once you've got the IShellLinkObject, you can use the Path method call to set the path, but to do the Save it is a little harder : cast this interface to (IPersistFile) than you'll gain access to the Save method.:-D I know this ain't easy because the parameters passed need marshaling, but I hope the thread on Google provides you all you need, PS : I believe the fact you can not directly create a ShellLinkObjectClass instance is due to bad tlbimp from .Net. This is not the first bug, this is not the last either I am fraid....


      And I swallow a small raisin.

      F 1 Reply Last reply
      0
      • S Stephane Rodriguez

        I am afraid you have to use interop services. See this thread on Google. From what I read in MSDN, you'll have to call the WIN32 API SHGetDesktopFolder() to retrieve the IShellFolder interface, then use either a direct cast to (IShellLinkObject) or use IShellFolder::GetUIObjectOf(...). Once you've got the IShellLinkObject, you can use the Path method call to set the path, but to do the Save it is a little harder : cast this interface to (IPersistFile) than you'll gain access to the Save method.:-D I know this ain't easy because the parameters passed need marshaling, but I hope the thread on Google provides you all you need, PS : I believe the fact you can not directly create a ShellLinkObjectClass instance is due to bad tlbimp from .Net. This is not the first bug, this is not the last either I am fraid....


        And I swallow a small raisin.

        F Offline
        F Offline
        Feng Qin
        wrote on last edited by
        #3

        But I think ShellLinkObjectClass should be created.:( I'm amumu, and you?

        S 1 Reply Last reply
        0
        • F Feng Qin

          But I think ShellLinkObjectClass should be created.:( I'm amumu, and you?

          S Offline
          S Offline
          Stephane Rodriguez
          wrote on last edited by
          #4

          I believe this object fails to be created because of a wrong interop class factory. I guess that, instead of using low-level interop -which is definitely the way to go when you are stuck-, there is a solution by explicitely asking the .Net framework to create a factory for us : Marshal.CreateWrapperOfType(o, typeof(Shell32.ShellLinkObjectClass)); or Marshal.CreateWrapperOfType(o, typeof(IShellLinkObject)); Try to get a working sample about that,


          And I swallow a small raisin.

          L 1 Reply Last reply
          0
          • S Stephane Rodriguez

            I believe this object fails to be created because of a wrong interop class factory. I guess that, instead of using low-level interop -which is definitely the way to go when you are stuck-, there is a solution by explicitely asking the .Net framework to create a factory for us : Marshal.CreateWrapperOfType(o, typeof(Shell32.ShellLinkObjectClass)); or Marshal.CreateWrapperOfType(o, typeof(IShellLinkObject)); Try to get a working sample about that,


            And I swallow a small raisin.

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            As far as I am aware, u cannot use unmanaged classes in C#. You will need to create a wrapper around a return object. But like Stephane said, find an example. StephaneRodriguez wrote: Marshal.CreateWrapperOfType(o, typeof(Shell32.ShellLinkObjectClass)); or Marshal.CreateWrapperOfType(o, typeof(IShellLinkObject)); I dont think this will work with platform invoking, only COM interop. MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D

            J 1 Reply Last reply
            0
            • L leppie

              As far as I am aware, u cannot use unmanaged classes in C#. You will need to create a wrapper around a return object. But like Stephane said, find an example. StephaneRodriguez wrote: Marshal.CreateWrapperOfType(o, typeof(Shell32.ShellLinkObjectClass)); or Marshal.CreateWrapperOfType(o, typeof(IShellLinkObject)); I dont think this will work with platform invoking, only COM interop. MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D

              J Offline
              J Offline
              James T Johnson
              wrote on last edited by
              #6

              leppie wrote: I dont think this will work with platform invoking, only COM interop. You're correct; but what he is doing is COM interop ;) James "And we are all men; apart from the females." - Colin Davies

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups