Jump to content

Create NetWork Connection ShortCut


Danyfirex
 Share

Recommended Posts

Well. I was needing to create a networks shortcut. so I did find anything into the forum. So I write this after reading about Nonfile Object.

 

;~ Danyfirex 26/07/2015
#include <WinAPIShellEx.au3>

Opt("MustDeclareVars", 1)

Global Const $SHCONTF_NONFOLDERS = 0x00040
Global Const $SHGDN_NORMAL = 0
Global Const $S_OK = 0
Global Const $sTagSTRRET = "UINT uType;ptr pOleStr;UINT uOffset;CHAR cStr[256]"


Global Const $CLSID_ShellLink = "{00021401-0000-0000-C000-000000000046}"
Global Const $sIID_IShellLinkW = "{000214F9-0000-0000-C000-000000000046}"
Global Const $sTagIShellLink = "getpath hresult(long;long;long;long); " & _
        "getidlist hresult(ptr); " & _
        "setidlist hresult(ptr); " & _
        "getdescription hresult(long;long); " & _
        "setdescription hresult(wstr); " & _
        "getworkingdirectory hresult(long;long); " & _
        "setworkingdirectory hresult(long); " & _
        "getarguments hresult(long;long); " & _
        "setarguments hresult(ptr); " & _
        "gethotkey hresult(long); " & _
        "sethotkey hresult(word); " & _
        "getshowcmd hresult(long); " & _
        "setshowcmd hresult(int); " & _
        "geticonlocation hresult(long;long;long); " & _
        "seticonlocation hresult(wstr;int); " & _
        "setrelativepath hresult(long;long); " & _
        "resolve hresult(long;long); " & _
        "setpath hresult(wstr)"

Global Const $sIID_IPersistFile = "{0000010b-0000-0000-C000-000000000046}"
Global Const $sTagIPersistFile = "GetClassID hresult(long); IsDirty hresult(); Load hresult(long;long); Save hresult(wstr;bool); SaveCompleted hresult(long); GetCurFile hresult(long)"

Global Const $sIID_IEnumIDList = "{000214F2-0000-0000-C000-000000000046}"
Global Const $sTagIEnumIDList = "Next hresult(ulong;ptr*;ulong*);" & _
        "Reset hresult();" & _
        "Skip hresult(ulong);" & _
        "Clone hresult(ptr*);"

Global Const $sIID_IShellFolder = "{000214E6-0000-0000-C000-000000000046}"
Global Const $sTagIShellFolder = "ParseDisplayName hresult(hwnd;ptr;wstr;ulong*;ptr*;ulong*);" & _
        "EnumObjects hresult(hwnd;dword;ptr*);" & _
        "BindToObject hresult(ptr;ptr;clsid;ptr*);" & _
        "BindToStorage hresult(ptr*;ptr;clsid;ptr*);" & _
        "CompareIDs hresult(lparam;ptr*;ptr*);" & _
        "CreateViewObject hresult(hwnd;clsid;ptr*);" & _
        "GetAttributesOf hresult(uint:ptr*;ulong*);" & _
        "GetUIObjectOf hresult(hwnd;uint;ptr*;clsid;uint*;ptr*);" & _
        "GetDisplayNameOf hresult(ptr;dword;ptr);" & _
        "SetNameOf hresult(hwnd;ptr*;wstr;dword;ptr*);"


Local $MyNetWorkConnectionName="Conexión de red inalámbrica"
CreateNetWorkConnectionShortCut($MyNetWorkConnectionName)

;~ Success: Return True
;~ Failure: Return False
;~ if $lnkPath = "" ShortCut will be Create on Desktop with the name of the Conections($sConnectionName)
Func CreateNetWorkConnectionShortCut($sConnectionName, $lnkPath = "")
    Local $pConecctionsFolder = 0
    Local $oConecctionsFolder = 0
    Local $pEnumIDList = 0
    Local $oEnumIDList = 0
    Local $pPIDLItem = 0
    Local $celtFetched = 0
    Local $pPIDLConectionsFolder = 0
    Local $bRet = False
    Local $tSTRRET = 0
    Local $twstring = 0
    Local $pShellFolder = SHGetDesktopFolder()
    Local $oShellFolder = ObjCreateInterface($pShellFolder, $sIID_IShellFolder, $sTagIShellFolder)
    If @error Then Return $bRet
    $pPIDLConectionsFolder = _WinAPI_ShellGetSpecialFolderLocation($CSIDL_CONNECTIONS)
    If SUCCEEDED($oShellFolder.BindToObject($pPIDLConectionsFolder, Null, $sIID_IShellFolder, $pConecctionsFolder)) Then
        $oConecctionsFolder = ObjCreateInterface($pConecctionsFolder, $sIID_IShellFolder, $sTagIShellFolder)
        If IsObj($oConecctionsFolder) Then
            If SUCCEEDED($oConecctionsFolder.EnumObjects(Null, $SHCONTF_NONFOLDERS, $pEnumIDList)) Then
                $oEnumIDList = ObjCreateInterface($pEnumIDList, $sIID_IEnumIDList, $sTagIEnumIDList)
                If IsObj($oEnumIDList) Then
                    $tSTRRET = DllStructCreate($sTagSTRRET)
                    While (SUCCEEDED($oEnumIDList.Next(1, $pPIDLItem, $celtFetched)) And $celtFetched > 0)
                        If SUCCEEDED($oConecctionsFolder.GetDisplayNameOf($pPIDLItem, $SHGDN_NORMAL, DllStructGetPtr($tSTRRET))) Then
                            $twstring = DllStructCreate("wchar Name[512]", $tSTRRET.pOleStr)
                            If $twstring.Name = $sConnectionName Then
                                If $lnkPath = "" Then $lnkPath = @DesktopDir & "\" & $twstring.Name & ".lnk"
                                $bRet = CreateLnk($pPIDLConectionsFolder, $pPIDLItem, $lnkPath)
                                ExitLoop
                            EndIf
                            $twstring = 0
                        EndIf
                    WEnd
                EndIf
            EndIf
        EndIf
    EndIf

    If $pPIDLConectionsFolder Then _WinAPI_CoTaskMemFree($pPIDLConectionsFolder)

    Return $bRet

EndFunc   ;==>CreateNetWorkShortCut

Func CreateLnk($PIDL1, $PIDL2, $lnkPath)
    Local $oShellLink = 0
    Local $oIPersistFile = 0
    Local $pIPersistFile = 0
    Local $pAppendID = 0
    Local $bRet = False
    $oShellLink = ObjCreateInterface($CLSID_ShellLink, $sIID_IShellLinkW, $sTagIShellLink)
    If @error Then Return $bRet
    $pAppendID = ILAppendID($PIDL1, $PIDL2)
    If IsObj($oShellLink) And $pAppendID Then
        If SUCCEEDED($oShellLink.SetIDList($pAppendID)) Then
            $oShellLink.QueryInterface($sIID_IPersistFile, $pIPersistFile)
            $oIPersistFile = ObjCreateInterface($pIPersistFile, $sIID_IPersistFile, $sTagIPersistFile)
            If IsObj($oIPersistFile) Then
                $bRet = SUCCEEDED($oIPersistFile.Save($lnkPath, True))
            EndIf
        EndIf
    EndIf
    Return $bRet
EndFunc   ;==>CreateLnk


Func ILAppendID($PIDLIST_RELATIVE, $LPSHITEMID, $fAppend = True)
    Local $aRet = DllCall("Shell32.dll", "ptr", "ILAppendID", "ptr", $PIDLIST_RELATIVE, "ptr", $LPSHITEMID, "bool", $fAppend)
    If @error Then Return SetError(@error, @extended, 0)
    If $aRet[0] = 0 Then Return SetError(10, $aRet[0], 0)
    Return $aRet[0]
EndFunc   ;==>ILAppendID


Func SHGetDesktopFolder()
    Local $aRet = DllCall("Shell32.dll", "long", "SHGetDesktopFolder", "ptr*", Null)
    If @error Then Return SetError(@error, @extended, 0)
    If $aRet[0] Then Return SetError(10, $aRet[0], 0)
    Return $aRet[1]
EndFunc   ;==>SHGetDesktopFolder


Func SUCCEEDED($hr)
    Return ($hr >= 0)
EndFunc   ;==>SUCCEEDED

Saludos

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...