• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

Creating a shortcut to VPN from C++

I need to create a link of one of the VPN connection (I create in the same program) to the desktop. I'm using C++ console application.
Is there a simple way to do that without adding any dependencies?
0
michaelst
Asked:
michaelst
  • 3
  • 3
1 Solution
 
JbamaralCommented:
The example I'm sending was extracted from MSDN library and opens the connection dialog for any connection, including VPN.
You just call dial( "your connection name" ).

If you need more control over your connection you can use the class provided in the following link
http://www.codeguru.com/cpp/i-n/network/dial-upnetworking/article.php/c2445__1
With this class you can bypass the dialog and add your own UI.

dialdlg.txt
0
 
michaelstAuthor Commented:
I do not need to dial. I need to create a shortcut only.
0
 
JbamaralCommented:
Sorry, I didn't understand your question.
I briefly tested this code with VS9 and it works fine.
The createPidl() function can be used for any type of object and retrives the right idl to use to create the .lnk file.
The CreateLink() receives the folder, the link name ( used as both display and file ) and the pidl obtained in createPidl().
Be aware that I didn't review or extensivelly tested this code, therefore you might find some  leak or some other bug. Use this just as a guide to write your own function.
#include "shlobj.h"
 
 
 
/* ------------------------------------------------------------------------- */
int CreateLink( int nFolder, char * linkName , LPITEMIDLIST pidlToLink )
{
   //Check for empty strings ...
   if(NULL == linkName || NULL == pidlToLink )
   {
      return -1;
   }
 
   //File system directory that contains the directories for the
   //common program groups that appear on the Start menu for all
   // users.
   LPITEMIDLIST pidl;
 
   // Get a pointer to an item ID list that represents the path
   // of a special folder
   HRESULT hr = SHGetSpecialFolderLocation(NULL, nFolder, &pidl);
 
   // Convert the item ID list's binary representation into a file
   // system path
   TCHAR linkPath[_MAX_PATH];
   BOOL f = SHGetPathFromIDList(pidl, linkPath);
 
   // Allocate a pointer to an IMalloc interface
   LPMALLOC pMalloc;
 
   // Get the address of our task allocator's IMalloc interface
   hr = SHGetMalloc(&pMalloc);
 
   // Free the item ID list allocated by SHGetSpecialFolderLocation
   pMalloc->Free(pidl);
 
   // Free our task allocator
   pMalloc->Release();
 
 
   char linkFile[_MAX_PATH];
   sprintf_s( linkFile, sizeof( linkFile ) ,  "%s\\%s.lnk", linkPath, linkName );
 
 
    HRESULT hres = NULL;
    IShellLink* psl = NULL;
 
    // Get a pointer to the IShellLink interface.
    hres = CoCreateInstance(CLSID_ShellLink, NULL,
        CLSCTX_INPROC_SERVER, IID_IShellLink,
        reinterpret_cast<void**>(&psl));
    if (SUCCEEDED(hres))
    {
        IPersistFile* ppf = NULL;
 
        // Set the path to the shortcut target
        //psl->SetPath(path);
        psl->SetIDList( pidlToLink );
 
        // Query IShellLink for the IPersistFile interface for
        // saving the shortcut in persistent storage.
        hres = psl->QueryInterface(IID_IPersistFile,
            reinterpret_cast<void**>(&ppf));
 
        if (SUCCEEDED(hres))
        {
            WCHAR wsz[MAX_PATH];
 
            // Ensure that the string is ANSI.
            MultiByteToWideChar(CP_ACP, 0, linkFile, -1,
                wsz, MAX_PATH);
 
            // Save the link by calling IPersistFile::Save.
            hres = ppf->Save(wsz, TRUE);
            ppf->Release();
        }
        psl->Release();
    }
 
    return 0;
 
}
 
/* ------------------------------------------------------------------------- */
LPITEMIDLIST createPidl( int nFolder, char *name )
{
    LPITEMIDLIST pidlParent=NULL;
    LPITEMIDLIST pidlChild=NULL;
    LPITEMIDLIST pidlResult = NULL;
 
    LPSHELLFOLDER lpsfDesktop=NULL;
    LPSHELLFOLDER lpsf=NULL;
 
    if(FAILED(SHGetDesktopFolder(&lpsfDesktop)))
        goto CLEANUP;
 
    if (FAILED(SHGetSpecialFolderLocation(NULL, nFolder, &pidlParent) ) )
       goto CLEANUP;
 
    if ( FAILED( lpsfDesktop->BindToObject(pidlParent,NULL,IID_IShellFolder,(LPVOID*)&lpsf) ) )
        goto CLEANUP;
 
 
    IEnumIDList *lpei;
 
    if ( SUCCEEDED(lpsf->EnumObjects( NULL, 
                                      SHCONTF_NONFOLDERS | 
                                      SHCONTF_INCLUDEHIDDEN , 
                                      &lpei ) ) )
    {
 
        while ( S_OK == lpei->Next(1 , &pidlChild , NULL ) )
        {            
            STRRET result;
            memset( &result , 0 , sizeof( result ) );
            if ( S_OK == lpsf->GetDisplayNameOf( pidlChild , 
                                                 SHGDN_NORMAL,
                                                 &result ) )
            {   
                char resultmb[1024];
 
                if ( result.uType&STRRET_CSTR)
                {
                    strcpy_s( resultmb, sizeof( resultmb ) , result.cStr );
                }
                else
                {                
                    WideCharToMultiByte(  CP_ACP, 
                                          0, 
                                          result.pOleStr, 
                                          -1, 
                                          resultmb , sizeof(resultmb), NULL, NULL );
 
                    CoTaskMemFree( result.pOleStr );
                }
 
 
                if ( 0 == _stricmp( name , resultmb ) )
                {
                    pidlResult = ILCombine( pidlParent , pidlChild );
                }
            }            
        }
 
 
        lpei->Release();
    }
 
 
 
 
CLEANUP:
    if ( lpsfDesktop )
        lpsfDesktop->Release();   
 
    if ( lpsf )
        lpsf->Release();
 
    ILFree( pidlParent );
    ILFree( pidlChild );
 
 
   return pidlResult;
 
}
 
 
/* ------------------------------------------------------------------------- */
int _tmain(int argc, _TCHAR* argv[])
{
    CoInitialize(NULL);
    
    LPITEMIDLIST pidlConn = createPidl( CSIDL_CONNECTIONS, "your connection name here");
 
    CreateLink( CSIDL_DESKTOP , "test" ,  pidlConn );
 
    ILFree( pidlConn );
    
	return 0;
}

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
michaelstAuthor Commented:
The code works with one problem - "linkPath" is a WCHAR. After I realized that everything is fine.
0
 
JbamaralCommented:
If you compile using multi byte chars instead of unicode it will work fine. I tested it before sending ( I did just one test but it worked fine ).
Note that TCHAR resolves to wchar_t if you use unicode and to char if you use multi byte.
But it was my bad anyways as I should have written the code using tprintf and the respective macros to cope with the consts.
As I said before this was just an example to understand the API's involved.
0
 
michaelstAuthor Commented:
No worries, I needed an example  how it works.So, thank you for you help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now