Shell Link

Im creating a program that will create the shortcut onto your desktop and im getting the following errors:

In function `HRESULT CreateShortcut(const TCHAR*, const TCHAR*, const TCHAR*)':
`CComPtr' undeclared (first use this function)
expected primary-expression before '>' token
`ipShellLink' undeclared (first use this function)
`CComQIPtr' undeclared (first use this function)
`ipPersistFile' undeclared (first use this function)

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
HRESULT CreateShortcut(/*in*/ LPCTSTR lpszFileName, 
                    /*in*/ LPCTSTR lpszDesc, 
                    /*in*/ LPCTSTR lpszShortcutPath)
{
    HRESULT hRes = E_FAIL;
    DWORD dwRet = 0;
    CComPtr<IShellLink> ipShellLink;
        // buffer that receives the null-terminated string 
        // for the drive and path
    TCHAR szPath[MAX_PATH];    
        // buffer that receives the address of the final 
        //file name component in the path
    LPTSTR lpszFilePart;    
    WCHAR wszTemp[MAX_PATH];
        
    // Retrieve the full path and file name of a specified file
    dwRet = GetFullPathName(lpszFileName, 
                       sizeof(szPath) / sizeof(TCHAR), 
                       szPath, &lpszFilePart);
    if (!dwRet)                                        
        return hRes;

    // Get a pointer to the IShellLink interface
    hRes = CoCreateInstance(CLSID_ShellLink,
                            NULL, 
                            CLSCTX_INPROC_SERVER,
                            IID_IShellLink,
                            (void**)&ipShellLink);

    if (SUCCEEDED(hRes))
    {
        // Get a pointer to the IPersistFile interface
        CComQIPtr<IPersistFile> ipPersistFile(ipShellLink);

        // Set the path to the shortcut target and add the description
        hRes = ipShellLink->SetPath(szPath);
        if (FAILED(hRes))
            return hRes;

        hRes = ipShellLink->SetDescription(lpszDesc);
        if (FAILED(hRes))
            return hRes;

        // IPersistFile is using LPCOLESTR, so make sure 
                // that the string is Unicode
#if !defined _UNICODE
        MultiByteToWideChar(CP_ACP, 0, 
                       lpszShortcutPath, -1, wszTemp, MAX_PATH);
#else
        wcsncpy(wszTemp, lpszShortcutPath, MAX_PATH);
#endif

        // Write the shortcut to disk
        hRes = ipPersistFile->Save(wszTemp, TRUE);
    }

    return hRes;
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{

  static HWND hwndPrgBar;
  static int i = 1;
  static char *Title = TEXT("         Database FX Installer \n\
Developed By: MacKenzie Whipp");
  INITCOMMONCONTROLSEX InitCtrlEx;

  InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  InitCtrlEx.dwICC  = ICC_PROGRESS_CLASS;
  InitCommonControlsEx(&InitCtrlEx);
  enum{ID_CREATE_SHORTCUT, ID_TITLE, ID_PROGRESS, ID_INSTALL };
  switch(msg)  
  {
      case WM_CREATE:
           HWND hInstallButton;
           int idInstallButton;
         hInstallButton = CreateWindow(TEXT("button"), TEXT("Create Shortcut"),
                  WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX,
                  20, 125, 185, 35,        
                  hwnd, (HMENU) ID_CREATE_SHORTCUT, ((LPCREATESTRUCT)lParam)->hInstance, NULL);

           idInstallButton = GetDlgCtrlID(hInstallButton);
 	       CheckDlgButton(hwnd, ID_CREATE_SHORTCUT, BST_UNCHECKED);
	    
           CreateWindow(TEXT("STATIC"), Title, WS_CHILD | WS_VISIBLE | SS_LEFT, 50, 20, 300, 100, hwnd, NULL, NULL, NULL);  

 
		    
		    
          hwndPrgBar = CreateWindowEx(0, PROGRESS_CLASS, NULL, WS_CHILD | WS_VISIBLE | PBS_SMOOTH, 50, 100, 190, 25, hwnd, NULL, g_hinst, NULL);   

          CreateWindow(TEXT("button"), TEXT("Install"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 100, 70, 80, 25, hwnd, (HMENU) ID_INSTALL, g_hinst, NULL);  

        SendMessage(hwndPrgBar, PBM_SETRANGE, 0, MAKELPARAM(0, 150));
        SendMessage(hwndPrgBar, PBM_SETSTEP, 1, 0 );
          break;


      case WM_TIMER:
          SendMessage( hwndPrgBar, PBM_STEPIT, 0, 0 );
          i++;
          if ( i == 150 ) 
          {
              KillTimer(hwnd, ID_TIMER);
              MessageBox(0,"Installation Successful", "Installation Complete", MB_OK );
                        PostQuitMessage(0);
                        //TCHAR path[] = TEXT("C:\\Program Files (x86)\\Database FX\\Database_FX.exe");
                        //ShellExecute(0, "open", path, NULL, NULL, SW_SHOWNORMAL);
          }
          break;
              
      case WM_COMMAND:
		    switch (LOWORD(wParam))
            {
                case ID_CREATE_SHORTCUT:
                     {
                                if(IsDlgButtonChecked(hwnd, ID_CREATE_SHORTCUT) == BST_CHECKED)
                                {                    
                                      LPCTSTR lpszFileName = _T("C:\\Work\\window.exe");
                                        LPCTSTR lpszShortcutDesc = _T("Anything can go here");
                                        LPCTSTR lpszShortcutPath = 
                                    _T("C:\\Documents and Settings\\Administrator\\Desktop\\Sample Shortcut.lnk");
                                    
                                        CreateShortcut(lpszFileName, lpszShortcutDesc, lpszShortcutPath);               
                                }
                 }
                    break;
                case ID_INSTALL:
                     {

                         
                        SendMessage( hwndPrgBar, PBM_SETPOS, 0, 0 );
                        SetTimer(hwnd, ID_TIMER, 5, NULL);
                        mkdir("C:\\Program Files (x86)\\Database FX");
                        CopyFile("F:\\Computer Programming\\Database\\Database_FX.exe", "C:\\Program Files (x86)\\Database FX\\Database_FX.exe", TRUE);
                        
                        
                     }
                        
                     
            }
          break;

      case WM_DESTROY:
          KillTimer(hwnd, ID_TIMER);
          PostQuitMessage(0);
          break; 
      
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}


idk whats wrong with it?
CComPtr is an ATL smartpointer declared in ATL namespace. Don't know what header to include, as the MSDN site shows an error right now.
My advice is to get rid of ATL depedency, as it only works with payed for versions of VS.
Also, you need CoInitialize be called first.
Topic archived. No new replies allowed.