title: jna CoCreateInstance
body: <p>am trying to create a link in windows 7 that has an icon. I am using the JNA library for this. I am having trouble with the call to CoCreateInstance. It returns error. First I am not sure if the GUID for IShellLink is the correct ( I am not a windows programer). Below is my code so far. Once I get the pointer to an IShellLink I would need to fill the paramaters for the link (target,icon, description, etc). I am modeling this call from the C code below found at this link (<a href="http://www.codeproject.com/Articles/11467/How-to-create-short-cuts-link-files" rel="nofollow">http://www.codeproject.com/Articles/11467/How-to-create-short-cuts-link-files</a>). I know I could use mklink command for this but it does have an option to add an icon and description.</p>
  
  <pre><code>private void createLink(){
      Pointer reserved = null; //Must be null
      int dwCoInit = 0;
  
      HRESULT oleInitResult = Ole32.INSTANCE.CoInitializeEx(reserved,dwCoInit);
      if(oleInitResult.equals(W32Errors.S_OK)){
          GUID rclsid = Ole32Util.getGUIDFromString("{e82a2d71-5b2f-43a0-97b8-81be15854de8}"); //Shell object
          GUID riid = Ole32Util.getGUIDFromString("{000214EE-0000-0000-C000-000000000046}"); //CLSID of the IShellLink object
          PointerByReference ppv = new PointerByReference();
          HRESULT oleCreateResult = Ole32.INSTANCE.CoCreateInstance(rclsid,null,ObjBase.CLSCTX_INPROC,riid,ppv);
          if(oleCreateResult.equals(W32Errors.S_OK)){
  
          }else{
              System.out.println("Failed to create link error "+oleCreateResult.intValue());
          }
      }
      Ole32.INSTANCE.CoUninitialize();
  }
  </code></pre>
  
  ###################### C++ Sample code
  
  <pre><code>/*
  --------------------------------------------------------------------------------
  Description: Creates the actual 'lnk' file (assumes COM has been initialized).
  Parameters: pszTargetfile    - File name of the link's target, must be a non-empty string.
  pszTargetargs    - Command line arguments passed to link's target, may be an empty string.
  pszLinkfile      - File name of the actual link file, must be a non-empty string.
  pszDescription   - Description of the linked item. If this is an empty string the description is not set.
  iShowmode        - ShowWindow() constant for the link's target. Use one of:
                         1 (SW_SHOWNORMAL) = Normal window.
                         3 (SW_SHOWMAXIMIZED) = Maximized.
                         7 (SW_SHOWMINNOACTIVE) = Minimized.
                       If this is zero the showmode is not set.
  pszCurdir        - Working directory of the active link. If this is an empty string the directory is not set.
  pszIconfile      - File name of the icon file used for the link.  If this is an empty string the icon is not set.
  iIconindex       - Index of the icon in the icon file. If this is &lt; 0 the icon is not set.
  Returns: HRESULT value &gt;= 0 for success, &lt; 0 for failure.
  --------------------------------------------------------------------------------
  */
  static HRESULT CreateShortCut(LPSTR pszTargetfile, LPSTR pszTargetargs, LPSTR pszLinkfile, LPSTR pszDescription, 
  int iShowmode, LPSTR pszCurdir, LPSTR pszIconfile, int iIconindex) {
  HRESULT       hRes;                  /* Returned COM result code */
  IShellLink*   pShellLink;            /* IShellLink object pointer */
  IPersistFile* pPersistFile;          /* IPersistFile object pointer */
  WORD          wszLinkfile[MAX_PATH]; /* pszLinkfile as Unicode string */
  int           iWideCharsWritten;     /* Number of wide characters written */
  hRes = E_INVALIDARG;
  if (
         (pszTargetfile != NULL) &amp;&amp; (strlen(pszTargetfile) &gt; 0) &amp;&amp;
         (pszTargetargs != NULL) &amp;&amp;
         (pszLinkfile != NULL) &amp;&amp; (strlen(pszLinkfile) &gt; 0) &amp;&amp;
         (pszDescription != NULL) &amp;&amp; 
         (iShowmode &gt;= 0) &amp;&amp;
         (pszCurdir != NULL) &amp;&amp; 
         (pszIconfile != NULL) &amp;&amp;
         (iIconindex &gt;= 0)
  ) {
      hRes = CoCreateInstance(&amp;CLSID_ShellLink,     /* pre-defined CLSID of the IShellLink object */
                              NULL,                 /* pointer to parent interface if part of aggregate */
                              CLSCTX_INPROC_SERVER, /* caller and called code are in same process */
                              &amp;IID_IShellLink,      /* pre-defined interface of the IShellLink object */
                              &amp;pShellLink);         /* Returns a pointer to the IShellLink object */
      if (SUCCEEDED(hRes))
      {
        /* Set the fields in the IShellLink object */
        hRes = pShellLink-&gt;lpVtbl-&gt;SetPath(pShellLink, pszTargetfile);
        hRes = pShellLink-&gt;lpVtbl-&gt;SetArguments(pShellLink, pszTargetargs);
        if (strlen(pszDescription) &gt; 0)
        {
          hRes = pShellLink-&gt;lpVtbl-&gt;SetDescription(pShellLink, pszDescription);
        }
        if (iShowmode &gt; 0)
        {
          hRes = pShellLink-&gt;lpVtbl-&gt;SetShowCmd(pShellLink, iShowmode);
        }
        if (strlen(pszCurdir) &gt; 0)
        {
          hRes = pShellLink-&gt;lpVtbl-&gt;SetWorkingDirectory(pShellLink, pszCurdir);
        }
        if (strlen(pszIconfile) &gt; 0 &amp;&amp; iIconindex &gt;= 0)
        {
          hRes = pShellLink-&gt;lpVtbl-&gt;SetIconLocation(pShellLink, pszIconfile, iIconindex);
        }
  
        /* Use the IPersistFile object to save the shell link */
        hRes = pShellLink-&gt;lpVtbl-&gt;QueryInterface(pShellLink,        /* existing IShellLink object */
                                                  &amp;IID_IPersistFile, /* pre-defined interface of the IPersistFile object */
                                                  &amp;pPersistFile);    /* returns a pointer to the IPersistFile object */
        if (SUCCEEDED(hRes))
        {
          iWideCharsWritten = MultiByteToWideChar(CP_ACP, 0, pszLinkfile, -1, wszLinkfile, MAX_PATH);
          hRes = pPersistFile-&gt;lpVtbl-&gt;Save(pPersistFile, wszLinkfile, TRUE);
          pPersistFile-&gt;lpVtbl-&gt;Release(pPersistFile);
        }
        pShellLink-&gt;lpVtbl-&gt;Release(pShellLink);
      }
  
    }
    return (hRes);
  }
  </code></pre>