#include <WinAPI.au3>
CONST $FILE_CREATE_PIPE_INSTANCE = 4
CONST $GENERIC_ALL = 0x10000000
CONST $GENERIC_READ = 0x80000000
CONST $GENERIC_WRITE = 0x40000000
CONST $FILE_EXECUTE = 0x20
CONST $FILE_TRAVERSE = 0x20
$hHeap = _GetProcessHeap()
$pAcl = _HeapAlloc($hHeap, 0, 1024)
_InitializeAcl($pAcl, 1024)
$tSecurDesc = _InitializeSecurityDescriptor()
$pSecurDesc = DllStructGetPtr($tSecurDesc)
$tSid = _LookupAccountName(@UserName)
$pSid = DllStructGetPtr($tSid)
_AddAccessAllowedAce($pAcl, $pSid, $GENERIC_READ)
_SetSecurityDescriptorDacl($pSecurDesc, $pAcl)
$tagShareInfo502 = "ptr;dword;ptr;dword[3];ptr[2];dword;ptr"
$tShare = DllStructCreate($tagShareInfo502)
$pShare = DllStructGetPtr($tShare)
$tName = DllStructCreate("char[128]")
$pName = DllStructGetPtr($tName)
$tPath = DllStructCreate("char[260]")
$pPath = DllStructGetPtr($tPath)
_WinAPI_MultiByteToWideCharEx("d", $pName)
_WinAPI_MultiByteToWideCharEx("d:", $pPath)
DllStructSetData($tShare, 1, $pName)
DllStructSetData($tShare, 2, 0)
DllStructSetData($tShare, 4, -1, 2)
DllStructSetData($tShare, 5, $pPath, 1)
DllStructSetData($tShare, 7, $pSecurDesc)
$iResult = DllCall("netapi32.dll", "int", "NetShareAdd", _
"wstr", "\\127.0.0.1", "dword", 502, "ptr", $pShare, "int*", 0)
_HeapFree($hHeap, $pAcl)
If $iResult[0] = 0 Then
Msgbox(0, $iResult[0], "Succeeded!")
Else
Msgbox(0, "Failed!", "ErrorCode: " & $iResult[0])
EndIf
Func _AddAccessAllowedAce($pACL, $pSid, $iAccess)
Local $iResult
$iResult = DllCall("advapi32.dll", "int", "AddAccessAllowedAce", _
"ptr", $pACL, "dword", 2, _
"dword", $iAccess, "ptr", $pSid)
Return SetError(Not $iResult[0], 0, $iResult[0] <> 0)
EndFunc ;==>_AddAccessAllowedAce()
Func _GetProcessHeap()
Local $hHeap
$hHeap = DllCall("Kernel32.dll", "hWnd", "GetProcessHeap")
Return $hHeap[0]
EndFunc ;==>_GetProcessHeap()
Func _HeapAlloc($hHeap, $iflag, $iSize)
Local $pMem
$pMem = DllCall("Kernel32.dll", "ptr", "HeapAlloc", _
"hWnd", $hHeap, "int", $iflag, "dword", $iSize)
Return SetError(Not $pMem[0], 0, $pMem[0])
EndFunc ;==>_HeapAlloc()
Func _HeapFree($hHeap, $pMem)
Local $iResult
$iResult = DllCall("Kernel32.dll", "int", "HeapFree", _
"hWnd", $hHeap, "dword", 1, "ptr", $pMem)
Return SetError(Not $iResult[0], 0, $iResult[0] <> 0)
EndFunc ;==>_HeapFree()
Func _InitializeAcl($pAcl, $iSize)
Local $iResult
$iResult = DllCall("advapi32.dll", "int", "InitializeAcl", _
"ptr", $pAcl, "dword", $iSize, "dword", 2)
Return SetError(Not $iResult[0], 0, $iResult[0] <> 0)
EndFunc ;==>_InitializeAcl()
Func _InitializeSecurityDescriptor()
Local $iResult, $tSecurDesc, $pSecurDesc
$tSecurDesc = DllStructCreate("dword[5]")
$pSecurDesc = DllStructGetPtr($tSecurDesc)
$iResult = DllCall("advapi32.dll", "int", "InitializeSecurityDescriptor", _
"ptr", $pSecurDesc, "dword", 1)
Return SetError(Not $iResult[0], 0, $tSecurDesc)
EndFunc ;==>_InitializeSecurityDescriptor()
Func _SetSecurityDescriptorDacl($pSecurDesc, $pACL, $iPresent = 1, $iDefault = 0)
Local $iResult
$iResult = DllCall("advapi32.dll", "int", "SetSecurityDescriptorDacl", _
"ptr", $pSecurDesc, "int", $iPresent, _
"ptr", $pACL, "int", $iDefault)
Return SetError(Not $iResult[0], 0, $iResult[0] <> 0)
EndFunc ;==>_SetSecurityDescriptorDacl()
Func _LookupAccountName($sAccount, $sSystem = "")
Local $iResult, $tSid, $pSid, $zSid, $tDomain, $pDomain, $zDomain
$iResult = DllCall("advapi32.dll", "int", "LookupAccountName", _
"str", $sSystem, "str", $sAccount, _
"ptr", 0, "dword*", 0, "ptr", 0, "dword*", 0, "int*", 0)
$zSid = $iResult[4]
$zDomain = $iResult[6]
$tSid = DllStructCreate("ubyte[" & $zSid & "]")
$pSid = DllStructGetPtr($tSid)
$tDomain = DllStructCreate("char[" & $zDomain & "]")
$pDomain = DllStructGetPtr($tDomain)
$iResult = DllCall("advapi32.dll", "int", "LookupAccountName", _
"str", $sSystem, "str", $sAccount, _
"ptr", $pSid, "dword*", $zSid, _
"ptr", $pDomain, "dword*", $zDomain, _
"int*", 0)
Return SetError(Not $iResult[0], $iResult[7], $tSid)
EndFunc ;==>_LookupAccountName()