Announcement

Collapse
No announcement yet.

Problem mit Shell32.dll

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem mit Shell32.dll

    Hallo,
    zur Erzeugung von ShortCuts im Startmenu oder auf dem Desktop möchte ich eine Klasse aus der Shell32.DLL nutzen. Ich habe eine Referenz zur DLL angelegt sowie den Namespace System.Runtime.InteropServices eingebunden.<br>

    using System.Runtime.InteropServices;<br>
    using Shell32;<br>
    Shell32.ShellLinkObjectClass sc = new Shell32.ShellLinkObjectClass();<br>
    Die Kompilierung läuft problemlos durch, allerdings wird bei Erzeugung der Objektinstanz immer eine COM-Exception ausgelöst, die besagt, dass das COM Objekt mit der CLSID {11219420-1768-11D1-95BE-00609797EA4F} entweder nicht gültig oder nicht registriert ist!?<br>Andere Klassen der selben DLL lassen sich problemlos instanziieren! <br>Für jeden Tip dankbar!<br>Uwe

  • #2
    Hallo,

    in Delphi wurde zuerst ein Interface-Zeiger für IUnknown angefordert, der denn in die jeweils anderen Interfaces eingtauscht wurde:
    <pre>

    var
    aObject : IUnknown;
    aSLink : IShellLink;
    aPFile : IPersistFile;
    ...
    begin
    // ShellLink-Objekt anfordern
    aObject := CreateComObject(CLSID_ShellLink);
    // Verbindung zum IShellLink-Interface herstellen
    aSLink := aObject as IShellLink;
    // Verbindung zum IPersistFile-Interface herstellen
    aPFile := aObject as IPersistFile;
    ..
    </pre>

    Ich habe mir noch nicht angesehen, wie sich das unter .NET verhält. Aber aus einem <i>CodeProject</i>-Artikel (<i>http://www.codeproject.com</i>) stammt die folgende Klasse, die den Windows Scripting Host für das Anlegen eines Shortcuts nutzt:

    <pre>

    // ================================================== ===================
    //
    // ShellLink - Using WSH to program shell links
    //
    // by Jim Hollenhorst, [email protected]
    // Copyright Ultrapico, April 2003
    // http://www.ultrapico.com
    //
    // ================================================== ===================
    using System;
    using System.Windows.Forms;
    using System.IO;
    using IWshRuntimeLibrary;

    namespace ShellLinks
    {
    /// <summary>
    /// Summary description for Link.
    /// </summary>
    public class Link
    {
    /// <summary>
    /// Check to see if a shortcut exists in a given directory with a specified file name
    /// </summary>
    /// <param name="DirectoryPath">The directory in which to look</param>
    /// <param name="FullPathName">The name of the shortcut (without the .lnk extension) or the full path to a file of the same name</param>
    /// <returns>Returns true if the link exists</returns>
    public static bool Exists(string DirectoryPath, string LinkPathName)
    {
    // Get some file and directory information
    DirectoryInfo SpecialDir=new DirectoryInfo(DirectoryPath);
    // First get the filename for the original file and create a new file
    // name for a link in the Startup directory
    //
    FileInfo originalfile = new FileInfo(LinkPathName);
    string NewFileName = SpecialDir.FullName+"\\"+originalfile.Name+".lnk";
    FileInfo linkfile = new FileInfo(NewFileName);
    return linkfile.Exists;
    }

    //Check to see if a shell link exists to the given path in the specified special folder
    // return true if it exists
    public static bool Exists(Environment.SpecialFolder folder, string LinkPathName)
    {
    return Link.Exists(Environment.GetFolderPath(folder), LinkPathName);
    }

    /// <summary>
    /// Update the specified folder by creating or deleting a Shell Link if necessary
    /// </summary>
    /// <param name="folder">A SpecialFolder in which the link will reside</param>
    /// <param name="TargetPathName">The path name of the target file for the link</param>
    /// <param name="LinkPathName">The file name for the link itself or, if a path name the directory information will be ignored.</param>
    /// <param name="Create">If true, create the link, otherwise delete it</param>
    public static void Update(Environment.SpecialFolder folder, string TargetPathName, string LinkPathName, bool install)
    {
    // Get some file and directory information
    Link.Update(Environment.GetFolderPath(folder), TargetPathName, LinkPathName, install);
    }

    // boolean variable "install" determines whether the link should be there or not.
    // Update the folder by creating or deleting the link as required.

    /// <summary>
    /// Update the specified folder by creating or deleting a Shell Link if necessary
    /// </summary>
    /// <param name="DirectoryPath">The full path of the directory in which the link will reside</param>
    /// <param name="TargetPathName">The path name of the target file for the link</param>
    /// <param name="LinkPathName">The file name for the link itself or, if a path name the directory information will be ignored.</param>
    /// <param name="Create">If true, create the link, otherwise delete it</param>
    public static void Update(string DirectoryPath, string TargetPathName, string LinkPathName, bool Create)
    {
    // Get some file and directory information
    DirectoryInfo SpecialDir=new DirectoryInfo(DirectoryPath);
    // First get the filename for the original file and create a new file
    // name for a link in the Startup directory
    //
    FileInfo OriginalFile = new FileInfo(LinkPathName);
    string NewFileName = SpecialDir.FullName+"\\"+OriginalFile.Name+".lnk";
    FileInfo LinkFile = new FileInfo(NewFileName);

    if(Create) // If the link doesn't exist, create it
    {
    if(LinkFile.Exists)return; // We're all done if it already exists
    //Place a shortcut to the file in the special folder
    try
    {
    // Create a shortcut in the special folder for the file
    // Making use of the Windows Scripting Host
    WshShell shell = new WshShell();
    IWshShortcut link = (IWshShortcut)shell.CreateShortcut(LinkFile.FullNa me);
    link.TargetPath=TargetPathName;
    link.Save();
    }
    catch
    {
    MessageBox.Show("Unable to create link in special directory: "+NewFileName,
    "Shell Link Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
    }
    else // otherwise delete it from the startup directory
    {
    if(!LinkFile.Exists)return; // It doesn't exist so we are done!
    try
    {
    LinkFile.Delete();
    }
    catch
    {
    MessageBox.Show("Error deleting link in special directory: "+NewFileName,
    "Shell Link Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
    }
    }

    }
    }

    </pre&gt

    Comment


    • #3
      Mit solch mundgerechter Aufbereitung hatte ich nicht gerechnet! )<BR> Danke für die Hilfe!!! <br>
      Uw

      Comment

      Working...
      X