Interoperatioin with Shell32.dll COMObject...
-
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? -
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?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.
-
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.
-
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.
-
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.
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
-
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
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