Announcement

Collapse
No announcement yet.

C übersetzer für eine CreateLink Funktion gesucht

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

  • C übersetzer für eine CreateLink Funktion gesucht

    Wer kann mir sagen, was an folgender Übersetzung aus C falsch ist, bzw. warum Sie nicht funktioniert, bereits die Zeile hres := psl.QueryInterface(IID_IPersistFile,ppf); gibt einen Fehler zurück, vielleicht kann mir auch jemand sagen wie man den übesetzten kann (-2147221008)! Hier die funktion:
    <p>
    <pre>
    //HRESULT CreateLink(LPCSTR lpszPathObj,
    // LPSTR lpszPathLink, LPSTR lpszDesc)
    function CreateLink(lpszPathObj : LPCSTR; lpszPathLink, lpszDesc : LPSTR):Boolean;
    VAR
    // HRESULT hres;
    hres : HRESULT;
    // IShellLink* psl;
    psl : IShellLinkA;
    ppf : IPersistFile;
    //WORD wsz[MAX_PATH];
    wsz : ARRAY[0..max_path] OF Word;
    begin
    result := false;
    // Get a pointer to the IShellLink interface.
    //hres = CoCreateInstance(&CLSID_ShellLink, NULL,
    // CLSCTX_INPROC_SERVER, &IID_IShellLink, &psl);
    hres := CoCreateInstance(CLSID_ShellLink, Nil,
    CLSCTX_INPROC_SERVER, IID_IShellLinkA, psl);
    //if (SUCCEEDED(hres)) [
    scode(
    if SUCCEEDED(hres) then begin
    // IPersistFile* ppf; s.o.

    // Set the path to the shortcut target, and add the
    // description.
    //psl->lpVtbl->SetPath(psl, lpszPathObj);
    psl.SetPath(lpszPathObj);
    //psl->lpVtbl->SetDescription(psl, lpszDesc);
    psl.SetDescription(lpszDesc);

    // Query IShellLink for the IPersistFile interface for saving the
    // shortcut in persistent storage.
    //hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, &ppf);
    hres := psl.QueryInterface(IID_IPersistFile,ppf);
    //if (SUCCEEDED(hres)) [
    if SUCCEEDED(hres) then begin
    //WORD wsz[MAX_PATH];

    // Ensure that the string is ANSI.
    //MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1,
    // wsz, MAX_PATH);
    MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1,
    @wsz, MAX_PATH);
    // Save the link by calling IPersistFile::Save.
    //hres = ppf->lpVtbl->Save(ppf, wsz, TRUE);
    hres := ppf.Save(@wsz,True);
    //ppf->lpVtbl->Release(ppf);
    ppf._Release;
    //???????????????
    // ]
    end;
    //psl->lpVtbl->Release(psl);
    psl._Release;
    //???????????????????
    // ]
    end;
    // return hres;
    result := hres = 0;
    end;

    //zum testen

    var
    prog,
    ziel,
    desc : String;
    initialization
    prog := 'C:\test\EasyCalc.exe'+#0;
    ziel := 'c:\test.LNK'+#0;//ob mit oder ohne .LNK gehts nich.
    desc := 'Dies ist ein Wunder'+#0;
    createLink(@prog[1],@ziel[1],@desc[1]);
    </pre>
    </p>
    P.S. Es geht sicherlich auch anders, das ganze soll aber so klein wie möglich werden, ich möchte die bisher 34 KB für einen Sfx-Header nicht versauen ;-)

  • #2
    Hallo Karsten,

    woran der Funktionsaufruf scheitert weiss ich leider auch nicht, aber schau dir mal im MSDN den folgenden Artikel an:
    http://msdn.microsoft.com/library/specs/S1D153.HTM
    Darin ist die Bedeutung der verschiedenen Bits im HRESULT-Code beschrieben (ich vermute, daß sich die Bits 0-15 auf einen Win32-Errorcode mappen lassen).

    MfG

    Reiner Ziegler
    [email protected]

    Comment


    • #3
      Du hast eigentlich nur eine einzige Zeile vergessen, die aber auch nicht in Deinem beispiel zu finden war, da sie "normalerweise" durch die RTL irgendwann aufgerufen wird (rot).

      <pre><p><b>program</b> Project1;
      <p>
      <b>uses</b>
      Windows, ActiveX, ShlObj;
      <p>
      <font color=blue><i>// Ole2.pas</i></font>
      <b>const</b>
      IID_IPersistFile: TGUID = (
      D1:$0000010B;D2:$0000;D3:$0000;D4$C0,$00,$00,$00,$00,$00,$00,$46));
      <p>
      <b>function</b> CreateLink(lpszPathObj: LPCSTR; lpszPathLink, lpszDesc : LPSTR): Boolean;
      <b>var</b>
      ShellLink: IShellLinkA;
      PersistFile: IPersistFile;
      wsz: <b>array</b> [0..MAX_PATH] <b>of</b> WCHAR;
      <b>begin</b>
      Result := False;
      ShellLink := <b>nil</b>;
      <b>if</b> Succeeded(CoCreateInstance(CLSID_ShellLink, <b>nil</b>, CLSCTX_INPROC_SERVER,
      IID_IShellLinkA, ShellLink)) <b>and</b> Assigned(ShellLink) <b>then</b>
      <b>begin</b>
      ShellLink.SetPath(lpszPathObj);
      ShellLink.SetDescription(lpszDesc);
      PersistFile := <b>nil</b>;
      if Succeeded(ShellLink.QueryInterface(IID_IPersistFil e, PersistFile)) <b>and</b>
      Assigned(PersistFile) <b>then</b>
      <b>begin</b>
      MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);
      Result := PersistFile.Save(wsz, True) = S_OK;
      PersistFile := <b>nil</b>;
      <b>end</b>;
      ShellLink := <b>nil</b>;
      <b>end</b>;
      <b>end</b>;
      <p>
      <b>var</b>
      Prog: <b>string</b>;
      Ziel: <b>string</b>;
      Desc: <b>string</b>;
      <p>
      <b>begin</b>
      <font color=red>CoInitialize(<b>nil</b>)</font>;
      Prog := 'c:\test.exe';
      Ziel := 'c:\test.lnk';
      Desc := 'Dies ist ein Wunder';
      CreateLink(PChar(Prog), PChar(Ziel), PChar(Desc));
      <b>end</b>.<p></pre>

      Gruß Nic

      Comment


      • #4
        Oh sorry, ich hatte diese Fehler (CoInitialize fehlte tatsächlich) inzwischen gefunden, ich war mir aber eigentlich sicher es hier bereits gepostet zu haben, also sorry nochmal und natürlich trotzdem danke für die Antworten

        Comment

        Working...
        X