LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

create shortcut to folder

Solved!
Go to solution

I want to create - from within my exe - a shortcut to a folder. I found in the forum another post 'Create shortcut via program' that refers to WINAPI and to example code (http://msdn.microsoft.com/en-us/library/bb776891%28VS.85%29.aspx). When I replace in this example code CLSID_ShellLink for file by CLSID_FolderShortcut for a folder I get an error. The compiler does not know CLSID_FolderShortcut. (There was not such an error message with CLSID_ShellLink).

 

Does anybody know a solution for this problem ?

Or has anybody an example for creating a Windows lnk-file for a shortcut to a folder ?

 

I am using CVI2010 Full Development on Windows 7 x64.

 

Regards, Jos

 

 

0 Kudos
Message 1 of 12
(4,756 Views)

Hello,

 

Which error did you get? Could you place a screenshot of it in attachment?

 

Thanks

0 Kudos
Message 2 of 12
(4,732 Views)

I copied the code from the MSDN page, minor change in includes, and changed the CLS as described on the MSDN page just above the source for creating the shortcut link (http://msdn.microsoft.com/en-us/library/bb776891%28VS.85%29.aspx😞

 

#include <windows.h>
#include <shobjidl.h>
#include <shlguid.h>
#include <strsafe.h>

 

... ...


    HRESULT hres;
    IShellLink* psl;
 
    // Get a pointer to the IShellLink interface. It is assumed that CoInitialize
    // has already been called.
    hres = CoCreateInstance(CLSID_FolderShortcut, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
    if (SUCCEEDED(hres))
    {
        IPersistFile* ppf;
 
        // Set the path to the shortcut target and add the description.
        psl->SetPath(lpszPathObj);
        psl->SetDescription(lpszDesc);

 

On compiling I get the error

 32, 29   Undeclared identifier 'CLSID_FolderShortcut'.

 32, 49   Type error in argument 1 to `CoCreateInstance'; found 'int' expected 'const pointer to const IID'.
 32, 93   Type error in argument 4 to `CoCreateInstance'; found 'GUID' expected 'const pointer to const IID'.


Any help is appreciated, Jos


 

 

0 Kudos
Message 3 of 12
(4,712 Views)

Hi,

 

Thanks for your reply. Could you send me your application that I can repeat your steps to reproduce your issue?

Beside I need some info about what you exactly do.

 

Thanks and hear from you soon.

 

Regards,

0 Kudos
Message 4 of 12
(4,699 Views)

I have not yet inserted anything in an application.

For data storage - where we use only Windows OS - I want to create multiple entries to a single data set. Something like a disk archive with a single subdirectory for each measurement data set, an automated generated link for browser when starting form and entry of the measurement facility, an automated generated link from a project entry, an automated generated link from an operator entry .... ..... All these links will refer to subdirectory folders, not to single files.

So I started with a single test: download the MSDN code and trying to compile this. My only changes are not including the stdadfx-h and changing to CLSID from a shortcut to a file into a shortcut for a folder.

Sorry, there is not yet any application. 

0 Kudos
Message 5 of 12
(4,692 Views)

I also tracked the definition of CLSID_FolderShortCut. This can be found in ...\CVI2010\sdk\include\ShlGuid,h

This file is in my include list. However, CLSID_FolderShortCut is a conditional definition:

 

#if (_WIN32_IE >= 0x0500)
// CLSID_FolderShortcut {0AFACED1-E828-11D1-9187-B532F1E9575D}
DEFINE_GUID(CLSID_FolderShortcut,       0x0AFACED1L, 0xE828, 0x11D1, 0x91, 0x87, 0xB5, 0x32, 0xF1, 0xE9, 0x57, 0x5D);
#endif

Do you know why the value of _WIN32_IE should be for Windows 7 x64?

Do you know why this is conditional definition ?

 

regards, Jos

0 Kudos
Message 6 of 12
(4,682 Views)

Jos,

 

I am assuming you got your code from the MSDN article on Shell Links. I too was having great difficulty with that example because it is meant for C++ and the conversion was not easy. I actually found another example that is easier to follow and got me up and running creating a shortcut much quicker.

 

http://www.codeproject.com/Articles/11467/How-to-create-short-cuts-link-files

 

One thing to note is that in CVI, you will need to add Ole32.lib and uuid.lib to your project. Unfortunately, there is a bug in uuid.lib in certain version of the Windows SDK. So far we are using this version in CVI 9.0 and later. Uuid.lib will work for 64-bit, but you will get link errors for a 32-bit app. So instead of using the version of the lib that ships with CVI 9.0 and later, download an earlier version of the Windows SDK, or download the uuid.lib from this KB article I just created to host it (it may take up to a day for the article to become live on the web).

National Instruments
0 Kudos
Message 7 of 12
(4,670 Views)

Thanks for you reply. I will give this a try. However ....

It looks that your alternative examply also applies to a file shortcut. In my previous post I reported that one problem is the "unknown" CLSID for a folder shortcut. I traced its definition to shlguid.h. And in that file, it is a conditional definition, depending on the value of _WIN32_IE.

 

So today I used (first time) interactive window:

#include <windows.h>
#include <shobjidl.h>
#include <ansi_c.h>
#include <shlguid.h>
static long aap = 0L;
#if (_WIN32_IE)
 aap = _WIN32_IE;
#endif
printf("%ld\n", aap);

 

This reports 0 for ergo _WIN32_IE does not exist (or is zero). In either case, the CLSID for a folder shortcut will not be defined. Any idea's to solve this ?

 

I am using CVI2010 on Windows 7 x64. Ultimo my target is 64-bit, but for the time being I will be satisfied with code for either 32- or 64-bit.

 

0 Kudos
Message 8 of 12
(4,659 Views)

Hi,

 

some time ago, I had a similiar problem; me solution was to create a vbs-script, which I can than execute (perhaps with a batch-wrapper).

 

This script will create a shortcut to a specific folder within my program directory; it links to a folder like "common app data" ...

 

 

Const CommonAppData = &H23&  ' the second & denotes a long integer '
Const myApplication = "myApplication"
' ---------------------------------------------------------------------
Public objFSO
Public sCurPath

Sub Main()
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
  Call GenerateShortcut("log")
End Sub

' ---------------------------------------------------------------------
'* @info Generate a folder tree from the path
'*
'* @param (String) Path
'* @return (Boolean) Folder Exists: Recursion continues (Y/N)
' ---------------------------------------------------------------------
Function GeneratePath(pFolderPath)
  GeneratePath = False
  If Not objFSO.FolderExists(pFolderPath) Then
    If GeneratePath(objFSO.GetParentFolderName(pFolderPath)) Then
      GeneratePath = True
      Call objFSO.CreateFolder(pFolderPath)
    End If
  Else
    GeneratePath = True
  End If
End Function

Function GenerateShortcut(pFolder)
  Set objShell  = CreateObject("Shell.Application")
  Set oWS = WScript.CreateObject("WScript.Shell")
  Set objFolder = objShell.Namespace(CommonAppData)
  Set objFolderItem = objFolder.Self
  sLinkFile = sCurPath & "\" & pFolder & ".lnk"
  sfolder = objFolderItem.Path & "\myCompany\" & myApplication & "\" & pFolder
  Call GeneratePath(sfolder)
  Set oLink = oWS.CreateShortcut(sLinkFile)

  oLink.TargetPath = sfolder
  '	oLink.Arguments = ""
  oLink.Description = pFolder
  '	oLink.HotKey = "ALT+CTRL+F"
  '	oLink.IconLocation = "C:\Program Files\MyApp\MyProgram.EXE, 2"
  '	oLink.WindowStyle = "1"
  '	oLink.WorkingDirectory = "C:\Program Files\MyApp"
  oLink.Save
End Function

Call Main
0 Kudos
Message 9 of 12
(4,649 Views)

I tried today the source that has been referred to by D Biel. And yes, this is easy to convert to a running exe-file. There is probably still some errors (because I do not get the help message) but I have made a shortcut to an existing file.

 

However ... when I replace in this code the CLSID for a file (CLSID_ShellLink) with the code for a folder (CLSID_FolderShortcut) I get the compiler message that this is an undeclared identifier.

In one of my other replies, I explain that this - on my computer - is related to the _WIN32_IE.

Ergo: I have now a better starting position but I am still on hold due the conditional definition of the needed CLSID in shlguid.h

 

Next step: I still hope for an answer that fits direct into CVI2010. Possible I am trying the nice alternative with VBS by Peter Prinzen.

 

0 Kudos
Message 10 of 12
(4,638 Views)