函数参考


_WinAPI_CreateSymbolicLink

Creates a symbolic link.

#Include <WinAPIEx.au3>
_WinAPI_CreateSymbolicLink ( $sSymlink, $sTarget [, $fDirectory] )

参数

$sSymlink The name of the new file.
$sTarget The name of the existing file.
$fDirectory [可选参数] Specifies whether the link target is a directory.
TRUE - The link target is a directory.
FALSE - The link target is a file. (Default)

返回值

成功: 返回 1.
失败: 返回 0 并设置 @error 标志为非 0 值.

注意/说明

To remove a symbolic link, delete the file (using _WinAPI_DeleteFile() or similar APIs) or remove the directory
(using _WinAPI_RemoveDirectory() or similar APIs) depending on what type of symbolic link is used.

The calling process must have $SE_CREATE_SYMBOLIC_LINK_NAME privilege, otherwise, the function fails, and
_WinAPI_GetLastError() returns ERROR_PRIVILEGE_NOT_HELD (1314).

本函数需要 Windows Vista 或更高版本系统.

相关

详情参考

在MSDN中搜索


示例/演示


#Include <APIConstants.au3>
#Include <WinAPIEx.au3>

Opt('MustDeclareVars', 1)

If _WinAPI_GetVersion() < '6.0' Then
    MsgBox(16, 'Error', 'Require Windows Vista or later.')
    Exit
EndIf

Global $hToken, $aAdjust

; Enable "SeCreateSymbolicLinkPrivilege" privilege to create a symbolic links
$hToken = _WinAPI_OpenProcessToken(BitOR($TOKEN_ADJUST_PRIVILEGES, $TOKEN_QUERY))
_WinAPI_AdjustTokenPrivileges($hToken, $SE_CREATE_SYMBOLIC_LINK_NAME, $SE_PRIVILEGE_ENABLED, $aAdjust)
If @error Or @extended Then
    MsgBox(16, 'Error', 'You do not have the required privileges.')
    Exit
EndIf

; Create symbolic link to the directory where this file is located with prefix "@" on your Desktop
_WinAPI_CreateSymbolicLink(@DesktopDir & '\' & StringRegExpReplace(@ScriptDir, '^.*\\', '@'), @ScriptDir, 1)
If @error Then
    _WinAPI_ShowLastError()
EndIf

; Restore "SeCreateSymbolicLinkPrivilege" privilege by default
_WinAPI_AdjustTokenPrivileges($hToken, $aAdjust, 0, $aAdjust)
_WinAPI_CloseHandle($hToken)