Jump to content
Danyfirex

Create NetWork Connection ShortCut

Recommended Posts

Danyfirex

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

Share this post


Link to post
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

  • Similar Content

    • nacerbaaziz
      By nacerbaaziz
      Hello my friends
      I have an inquiry and I hope to find the answer here
      I want to create a graphical user interface
      but I want to hide the system menu
      I mean the window menu
      Is this possible?
      If is possible please give me how to do that
      Thanks in advance
    • simy8891
      By simy8891
      Hi guys,
      It's been a while since I wrote my last message here and a while since I used AutoIt. I'm currently sort of desperate and I'm trying to find some help in regards of getting the network usage per process!
      I'm not interested in the total network usage of the NIC, but only on a specific PID's network utilization. They idea is to collect the amount of traffic uploaded and downloaded by a list of specific processes. So far Process Hacker and Process Explorer are capable of getting what I need, but I need to use these numbers in another script so they're sort of useless to me. I can't seem to find a way around it.
      Any idea, help is greatly appreciated.
      Thanks
    • nacerbaaziz
      By nacerbaaziz

      Hello all
      I have a question please
      this  a question is about the context menu
      When i create a list box  Or combo box
      if i create a context menu for this list
      When i press the application key or the shortcut shift + f10
      the menu options is appear normally
      but When i create a List view
      if i create a context menu for this list
      When i press the application key or the shortcut shift + f10
      the menu options isn't appear
      But the right mouse button works normally
      I need a solution please because I deal with the screen readers users
      Unfortunately They can not use the mouse to navigate
      So I have to find a solution to activate keyboard shortcuts
      am waiting  for your help
    • nacerbaaziz
      By nacerbaaziz
      can we  create a list box with  Columns?
      welcome everybody
      Dears I have a question if you let me
      can we  create a list box with  Columns?
      i know we can create a list view with Columns
      but my question is about the list box
      I'm waiting your responses
      Thank you in advance
    • badcoder123
      By badcoder123
      Anyone know how to access the information circled in the photo?
       

×