Sign in to follow this  
Followers 0
JScript

_NamedPipes_CreateNamedPipe freezes the GUI.

2 posts in this topic

Hello,

I'm trying to use the functions "Named Pipes" but I have no experience in the subject, so far made ​​it work but the GUI server is locked until you reach a new message client module ...

Could anyone help me please?

Server:

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_Compression=0                                           ;Compression parameter 0-4  0=Low 2=normal 4=High. Default=2
#AutoIt3Wrapper_UseUpx=n                                                ;(Y/N) Compress output program. Default=Y

#include <WinAPI.au3>
#include <NamedPipes.au3>

;_WinAPI_ReadFile
;_WinAPI_CreateFile
;_WinAPI_WriteFile
;_NamedPipes_CreatePipe

Global $sPipeName = "\\.\pipe\virtual01"
; Pipe name format - \\.\pipe\pipename

Global $BUFFER_SIZE = 4096 ; 4k
Global $TIMEOUT = 1000
Global $ACK_MESG_RECV = "Message received successfully"
Global $hPipe

; Create a named pipe
$hPipe = _NamedPipes_CreateNamedPipe( _
        $sPipeName, _   ; Pipe name
        2, _            ; The pipe is bi-directional
        2, _            ; Overlapped mode is enabled
        0, _            ; No security ACL flags
        1, _            ; Data is written to the pipe as a stream of messages
        1, _            ; Data is read from the pipe as a stream of messages
        0, _            ; Blocking mode is enabled
        1, _            ; Maximum number of instances
        $BUFFER_SIZE, _ ; Output buffer size
        $BUFFER_SIZE, _ ; Input buffer size
        0, _    ; Client time out
        0)              ; Default security attributes

If $hPipe = -1 Then
    MsgBox(48 + 262144, "NamedPipe", "Failed to create new account!" & @CRLF & "Probably one using that 'address' already exists.")
    Exit
EndIf

Global $hGUI = GUICreate("NamedPipe Demo Server", 450, 400, 3 * @DesktopWidth / 4 - 225, -1, -1, 8) ; $WS_EX_TOPMOST

GUICtrlCreateLabel("Message text:", 10, 22, 100, 25)
GUICtrlSetColor(-1, 0x0000CC)
GUICtrlSetFont(-1, 11)

Global $hEdit = GUICtrlCreateEdit("", 15, 50, 300, 340)

Global $hButtonCloseApp = GUICtrlCreateButton("&Exit", 330, 350, 100, 25)

GUISetState()


While 1
    _ReadMessage($hPipe)

    Switch GUIGetMsg()
        Case -3, $hButtonCloseApp
            _NamedPipes_DisconnectNamedPipe($sPipeName)
            Exit
    EndSwitch

WEnd

; Wrapper functions:

Func _ReadMessage($hHandle)

    If _NamedPipes_ConnectNamedPipe($hHandle) Then
        Local $sData = _MailSlotRead($hHandle, $BUFFER_SIZE, 1)
        ;$tBuffer = DllStructCreate("byte[" & $BUFFER_SIZE & "]")
        ;_WinAPI_ReadFile($sPipeName, DllStructGetPtr($tBuffer), $BUFFER_SIZE, $iRead)
        GUICtrlSetData($hEdit, GUICtrlRead($hEdit) & @CRLF & $sData)
    EndIf
        _NamedPipes_DisconnectNamedPipe($hHandle)
EndFunc   ;==>_ReadMessage

; #FUNCTION# ;===============================================================================
;
; Name...........: _MailSlotRead
; Description ...: Reads messages from the specified mailslot.
; Syntax.........: _MailSlotRead ($hMailSlot , $iSize [, $iMode])
; Parameters ....: $hMailSlot - Mailslot handle
;                  $iSize - The number of bytes to read.
;                  $iMode - Reading mode.
;                             Can be: 0 - read binary
;                                     1 - read ANSI
;                                     2 - read UTF8
; Return values .: Success - Returns read data
;                          - Sets @extended to number of read bytes
;                          - Sets @error to 0
;                  Special: Sets @error to -1 if specified buffer to read to is too small.
;                  Failure - Returns empty string and sets @error:
;                  |1 - DllCall() to ReadFile failed.
;                  |2 - GetLastError function or call to it failed.
;                  |3 - ReadFile function failed. @extended will be set to the return value of the GetLastError function.
; Author ........: trancexx
; Modified.......:
; Link ..........; http://msdn.microsoft.com/en-us/library/aa365467(VS.85).aspx
;                  http://msdn.microsoft.com/en-us/library/ms679360(VS.85).aspx
;
;==========================================================================================
Func _MailSlotRead($hMailSlot, $iSize, $iMode = 0)

    Local $tDataBuffer = DllStructCreate("byte[" & $iSize & "]")

    Local $aCall = DllCall("kernel32.dll", "int", "ReadFile", _
            "ptr", $hMailSlot, _
            "ptr", DllStructGetPtr($tDataBuffer), _
            "dword", $iSize, _
            "dword*", 0, _
            "ptr", 0)

    If @error Then
        Return SetError(1, 0, "")
    EndIf

    If Not $aCall[0] Then
        Local $aLastErrorCall = DllCall("kernel32.dll", "int", "GetLastError")
        If @error Then
            Return SetError(2, 0, "")
        EndIf
        If $aLastErrorCall[0] = 122 Then ; ERROR_INSUFFICIENT_BUFFER
            Return SetError(-1, 0, "")
        Else
            Return SetError(3, $aLastErrorCall[0], "")
        EndIf
    EndIf

    Local $vOut

    Switch $iMode
        Case 1
            $vOut = BinaryToString(DllStructGetData($tDataBuffer, 1))
        Case 2
            $vOut = BinaryToString(DllStructGetData($tDataBuffer, 1), 4)
        Case Else
            $vOut = DllStructGetData($tDataBuffer, 1)
    EndSwitch

    Return SetError(0, $aCall[4], $vOut)

EndFunc   ;==>_MailSlotRead


; #FUNCTION# ;===============================================================================
;
; Name...........: _MailSlotWrite
; Description ...: Writes message to the specified mailslot.
; Syntax.........: _MailSlotWrite ($sMailSlotName , $vData [, $iMode])
; Parameters ....: $hMailSlot - Mailslot name
;                  $vData - Data to write.
;                  $iMode - Writing mode.
;                             Can be: 0 - write binary
;                                     1 - write ANSI
;                                     2 - write UTF8
; Return values .: Success - Returns the number of written bytes
;                          - Sets @error to 0
;                  Failure - Returns empty string and sets @error:
;                  |1 - CreateFileW function or call to it failed.
;                  |2 - WriteFile function or call to it failed.
;                  |3 - Opened mail slot handle could not be closed.
;                  |4 - WriteFile function or call to it failed and additionally opened mail slot handle could not be closed.
; Author ........: trancexx
; Modified.......:
; Link ..........; http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
;                  http://msdn.microsoft.com/en-us/library/aa365747(VS.85).aspx
;                  http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
;
;==========================================================================================
Func _MailSlotWrite($sMailSlotName, $vData, $iMode = 0)

    Local $aCall = DllCall("kernel32.dll", "ptr", "CreateFileW", _
            "wstr", $sMailSlotName, _
            "dword", 0x40000000, _ ; GENERIC_WRITE
            "dword", 1, _ ; FILE_SHARE_READ
            "ptr", 0, _
            "dword", 3, _ ; OPEN_EXISTING
            "dword", 0, _ ; SECURITY_ANONYMOUS
            "ptr", 0)

    If @error Or $aCall[0] = -1 Then
        Return SetError(1, 0, 0)
    EndIf

    Local $hMailSlotHandle = $aCall[0]

    Local $bData

    Switch $iMode
        Case 1
            $bData = StringToBinary($vData, 1)
        Case 2
            $bData = StringToBinary($vData, 4)
        Case Else
            $bData = $vData
    EndSwitch

    Local $iBufferSize = BinaryLen($bData)

    Local $tDataBuffer = DllStructCreate("byte[" & $iBufferSize & "]")
    DllStructSetData($tDataBuffer, 1, $bData)

    $aCall = DllCall("kernel32.dll", "int", "WriteFile", _
            "ptr", $hMailSlotHandle, _
            "ptr", DllStructGetPtr($tDataBuffer), _
            "dword", $iBufferSize, _
            "dword*", 0, _
            "ptr", 0)

    If @error Or Not $aCall[0] Then
        $aCall = DllCall("kernel32.dll", "int", "CloseHandle", "ptr", $hMailSlotHandle)
        If @error Or Not $aCall[0] Then
            Return SetError(4, 0, 0)
        EndIf
        Return SetError(2, 0, 0)
    EndIf

    Local $iOut = $aCall[4]

    $aCall = DllCall("kernel32.dll", "int", "CloseHandle", "ptr", $hMailSlotHandle)
    If @error Or Not $aCall[0] Then
        Return SetError(3, 0, $iOut)
    EndIf

    Return $iOut

EndFunc   ;==>_MailSlotWrite
Cliente:

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_Compression=0                                           ;Compression parameter 0-4  0=Low 2=normal 4=High. Default=2
#AutoIt3Wrapper_UseUpx=n                                                ;(Y/N) Compress output program. Default=Y

#include <WinAPI.au3>
#include <NamedPipes.au3>

Global $sPipeName = "\\.\pipe\virtual01"
; Pipe name format - \\.\pipe\pipename

Global $BUFFER_SIZE = 4096 ; 4k
Global $TIMEOUT = 1000
Global $ACK_MESG_RECV = "Message received successfully"
Global $hPipe

Global $hGUI = GUICreate("NamePipe Demo Client", 450, 400, @DesktopWidth / 4 - 225, -1, -1, 8) ; $WS_EX_TOPMOST

GUICtrlCreateLabel("Message text:", 10, 22, 100, 25)
GUICtrlSetColor(-1, 0x0000CC)
GUICtrlSetFont(-1, 11)

Global $hEdit = GUICtrlCreateEdit("", 15, 50, 300, 340)

Global $hButtonSend = GUICtrlCreateButton("&Send Pipe", 330, 100, 100, 25)

Global $hButtonCloseApp = GUICtrlCreateButton("&Exit", 330, 350, 100, 25)

GUISetState()


While 1

    Switch GUIGetMsg()
        Case - 3, $hButtonCloseApp
            Exit
        Case $hButtonSend
            _SendMail($sPipeName)
    EndSwitch

WEnd


; Wrapper function:

Func _SendMail($sPipeName)

    Local $sDataToSend = GUICtrlRead($hEdit)

    If $sDataToSend Then

        _MailSlotWrite($sPipeName, $sDataToSend, 1)

        Switch @error
            Case 1
                MsgBox(48, "NamePipe demo error", "Account that you try to send to likely doesn't exist!", 0, $hGUI)
            Case 2
                MsgBox(48, "NamePipe demo error", "Message is blocked!", 0, $hGUI)
            Case 3
                MsgBox(48, "NamePipe demo error", "Message is send but there is an open handle left." & @CRLF & "That could lead to possible errors in future", 0, $hGUI)
            Case 4
                MsgBox(48, "NamePipe demo error", "All is fucked up!" & @CRLF & "Try debugging MailSlot.au3 functions. Thanks.", 0, $hGUI)
            Case Else
                MsgBox(64, "NamePipe demo", "Sucessfully sent!", 0, $hGUI)
        EndSwitch
        GUICtrlSetData($hEdit, "")
    EndIf

EndFunc   ;==>_SendMail

; #FUNCTION# ;===============================================================================
;
; Name...........: _MailSlotRead
; Description ...: Reads messages from the specified mailslot.
; Syntax.........: _MailSlotRead ($hMailSlot , $iSize [, $iMode])
; Parameters ....: $hMailSlot - Mailslot handle
;                  $iSize - The number of bytes to read.
;                  $iMode - Reading mode.
;                             Can be: 0 - read binary
;                                     1 - read ANSI
;                                     2 - read UTF8
; Return values .: Success - Returns read data
;                          - Sets @extended to number of read bytes
;                          - Sets @error to 0
;                  Special: Sets @error to -1 if specified buffer to read to is too small.
;                  Failure - Returns empty string and sets @error:
;                  |1 - DllCall() to ReadFile failed.
;                  |2 - GetLastError function or call to it failed.
;                  |3 - ReadFile function failed. @extended will be set to the return value of the GetLastError function.
; Author ........: trancexx
; Modified.......:
; Link ..........; http://msdn.microsoft.com/en-us/library/aa365467(VS.85).aspx
;                  http://msdn.microsoft.com/en-us/library/ms679360(VS.85).aspx
;
;==========================================================================================
Func _MailSlotRead($hMailSlot, $iSize, $iMode = 0)

    Local $tDataBuffer = DllStructCreate("byte[" & $iSize & "]")

    Local $aCall = DllCall("kernel32.dll", "int", "ReadFile", _
            "ptr", $hMailSlot, _
            "ptr", DllStructGetPtr($tDataBuffer), _
            "dword", $iSize, _
            "dword*", 0, _
            "ptr", 0)

    If @error Then
        Return SetError(1, 0, "")
    EndIf

    If Not $aCall[0] Then
        Local $aLastErrorCall = DllCall("kernel32.dll", "int", "GetLastError")
        If @error Then
            Return SetError(2, 0, "")
        EndIf
        If $aLastErrorCall[0] = 122 Then ; ERROR_INSUFFICIENT_BUFFER
            Return SetError(-1, 0, "")
        Else
            Return SetError(3, $aLastErrorCall[0], "")
        EndIf
    EndIf

    Local $vOut

    Switch $iMode
        Case 1
            $vOut = BinaryToString(DllStructGetData($tDataBuffer, 1))
        Case 2
            $vOut = BinaryToString(DllStructGetData($tDataBuffer, 1), 4)
        Case Else
            $vOut = DllStructGetData($tDataBuffer, 1)
    EndSwitch

    Return SetError(0, $aCall[4], $vOut)

EndFunc   ;==>_MailSlotRead


; #FUNCTION# ;===============================================================================
;
; Name...........: _MailSlotWrite
; Description ...: Writes message to the specified mailslot.
; Syntax.........: _MailSlotWrite ($sMailSlotName , $vData [, $iMode])
; Parameters ....: $hMailSlot - Mailslot name
;                  $vData - Data to write.
;                  $iMode - Writing mode.
;                             Can be: 0 - write binary
;                                     1 - write ANSI
;                                     2 - write UTF8
; Return values .: Success - Returns the number of written bytes
;                          - Sets @error to 0
;                  Failure - Returns empty string and sets @error:
;                  |1 - CreateFileW function or call to it failed.
;                  |2 - WriteFile function or call to it failed.
;                  |3 - Opened mail slot handle could not be closed.
;                  |4 - WriteFile function or call to it failed and additionally opened mail slot handle could not be closed.
; Author ........: trancexx
; Modified.......:
; Link ..........; http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
;                  http://msdn.microsoft.com/en-us/library/aa365747(VS.85).aspx
;                  http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
;
;==========================================================================================
Func _MailSlotWrite($sMailSlotName, $vData, $iMode = 0)

    Local $aCall = DllCall("kernel32.dll", "ptr", "CreateFileW", _
            "wstr", $sMailSlotName, _
            "dword", 0x40000000, _ ; GENERIC_WRITE
            "dword", 0, _ ; FILE_SHARE_READ
            "ptr", 0, _
            "dword", 3, _ ; OPEN_EXISTING
            "dword", 0, _ ; SECURITY_ANONYMOUS
            "ptr", 0)

    If @error Or $aCall[0] = -1 Then
        Return SetError(1, 0, 0)
    EndIf

    Local $hMailSlotHandle = $aCall[0]

    Local $bData

    Switch $iMode
        Case 1
            $bData = StringToBinary($vData, 1)
        Case 2
            $bData = StringToBinary($vData, 4)
        Case Else
            $bData = $vData
    EndSwitch

    Local $iBufferSize = BinaryLen($bData)

    Local $tDataBuffer = DllStructCreate("byte[" & $iBufferSize & "]")
    DllStructSetData($tDataBuffer, 1, $bData)

    $aCall = DllCall("kernel32.dll", "int", "WriteFile", _
            "ptr", $hMailSlotHandle, _
            "ptr", DllStructGetPtr($tDataBuffer), _
            "dword", $iBufferSize, _
            "dword*", 0, _
            "ptr", 0)

    If @error Or Not $aCall[0] Then
        $aCall = DllCall("kernel32.dll", "int", "CloseHandle", "ptr", $hMailSlotHandle)
        If @error Or Not $aCall[0] Then
            Return SetError(4, 0, 0)
        EndIf
        Return SetError(2, 0, 0)
    EndIf

    Local $iOut = $aCall[4]

    $aCall = DllCall("kernel32.dll", "int", "CloseHandle", "ptr", $hMailSlotHandle)
    If @error Or Not $aCall[0] Then
        Return SetError(3, 0, $iOut)
    EndIf

    Return $iOut

EndFunc   ;==>_MailSlotWrite

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Share this post


Link to post
Share on other sites



Hello

Server: (corrected)

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_Compression=0                                        ;Compression parameter 0-4 0=Low 2=normal 4=High. Default=2
#AutoIt3Wrapper_UseUpx=n                                             ;(Y/N) Compress output program. Default=Y

#include <WinAPI.au3>
#include <NamedPipes.au3>

;_WinAPI_ReadFile
;_WinAPI_CreateFile
;_WinAPI_WriteFile
;_NamedPipes_CreatePipe

Global $sPipeName = "\\.\pipe\virtual01"
; Pipe name format - \\.\pipe\pipename

Global $BUFFER_SIZE = 4096 ; 4k
Global $TIMEOUT = 1000
Global $ACK_MESG_RECV = "Message received successfully"
Global $hPipe

; Create a named pipe
$hPipe = _NamedPipes_CreateNamedPipe( _
     $sPipeName, _ ; Pipe name
     2, _        ; The pipe is bi-directional
     2, _        ; Overlapped mode is enabled
     0, _        ; No security ACL flags
     1, _        ; Data is written to the pipe as a stream of messages
     1, _        ; Data is read from the pipe as a stream of messages
     0, _        ; Blocking mode is enabled
     1, _        ; Maximum number of instances
     $BUFFER_SIZE, _ ; Output buffer size
     $BUFFER_SIZE, _ ; Input buffer size
     0, _ ; Client time out
     0)          ; Default security attributes

If $hPipe = -1 Then
MsgBox(48 + 262144, "NamedPipe", "Failed to create new account!" & @CRLF & "Probably one using that 'address' already exists.")
Exit
EndIf

Global $hGUI = GUICreate("NamedPipe Demo Server", 450, 400, 3 * @DesktopWidth / 4 - 225, -1, -1, 8) ; $WS_EX_TOPMOST

GUICtrlCreateLabel("Message text:", 10, 22, 100, 25)
GUICtrlSetColor(-1, 0x0000CC)
GUICtrlSetFont(-1, 11)

Global $hEdit = GUICtrlCreateEdit("", 15, 50, 300, 340, 0x00200000 + 0x00100000 + 2048)

Global $hButtonCloseApp = GUICtrlCreateButton("&Exit", 330, 350, 100, 25)

GUISetState()

Global $OVERLAPPED_Struct=DllStructCreate($tagOVERLAPPED)

While 1
_ReadMessage($hPipe)

Switch GUIGetMsg()
     Case -3, $hButtonCloseApp
         _NamedPipes_DisconnectNamedPipe($hPipe)
         _NamedPipes_DisconnectNamedPipe($sPipeName)
         Exit
EndSwitch

WEnd

; Wrapper functions:

Func _ReadMessage($hHandle)
Local $sData = _MailSlotRead($hHandle, $BUFFER_SIZE, 1)
; Local $tBuffer = DllStructCreate("byte[" & $BUFFER_SIZE & "]")
; _WinAPI_ReadFile($sPipeName, DllStructGetPtr($tBuffer), $BUFFER_SIZE, $sData)
If $sData Then
_NamedPipes_DisconnectNamedPipe($hHandle)
     GUICtrlSetData($hEdit, GUICtrlRead($hEdit) & @CRLF & $sData)
TrayTip ( $ACK_MESG_RECV, $sData, 1 ,1 )
_NamedPipes_ConnectNamedPipe($hHandle, DllStructGetPtr($OVERLAPPED_Struct))
EndIf
; EndIf
EndFunc ;==>_ReadMessage

; #FUNCTION# ;===============================================================================
;
; Name...........: _MailSlotRead
; Description ...: Reads messages from the specified mailslot.
; Syntax.........: _MailSlotRead ($hMailSlot , $iSize [, $iMode])
; Parameters ....: $hMailSlot - Mailslot handle
;                $iSize - The number of bytes to read.
;                $iMode - Reading mode.
;                            Can be: 0 - read binary
;                                    1 - read ANSI
;                                    2 - read UTF8
; Return values .: Success - Returns read data
;                        - Sets @extended to number of read bytes
;                        - Sets @error to 0
;                Special: Sets @error to -1 if specified buffer to read to is too small.
;                Failure - Returns empty string and sets @error:
;                |1 - DllCall() to ReadFile failed.
;                |2 - GetLastError function or call to it failed.
;                |3 - ReadFile function failed. @extended will be set to the return value of the GetLastError function.
; Author ........: trancexx
; Modified.......:
; Link ..........; http://msdn.microsoft.com/en-us/library/aa365467(VS.85).aspx
;                http://msdn.microsoft.com/en-us/library/ms679360(VS.85).aspx
;
;==========================================================================================
Func _MailSlotRead($hMailSlot, $iSize, $iMode = 0)

Local $tDataBuffer = DllStructCreate("byte[" & $iSize & "]")

Local $aCall = DllCall("kernel32.dll", "int", "ReadFile", _
         "ptr", $hMailSlot, _
         "ptr", DllStructGetPtr($tDataBuffer), _
         "dword", $iSize, _
         "dword*", 0, _
         "ptr", 0)

If @error Then
     Return SetError(1, 0, "")
EndIf

If Not $aCall[0] Then
     Local $aLastErrorCall = DllCall("kernel32.dll", "int", "GetLastError")
     If @error Then
         Return SetError(2, 0, "")
     EndIf
     If $aLastErrorCall[0] = 122 Then ; ERROR_INSUFFICIENT_BUFFER
         Return SetError(-1, 0, "")
     Else
         Return SetError(3, $aLastErrorCall[0], "")
     EndIf
EndIf

Local $vOut

Switch $iMode
     Case 1
         $vOut = BinaryToString(DllStructGetData($tDataBuffer, 1))
     Case 2
         $vOut = BinaryToString(DllStructGetData($tDataBuffer, 1), 4)
     Case Else
         $vOut = DllStructGetData($tDataBuffer, 1)
EndSwitch

Return SetError(0, $aCall[4], $vOut)

EndFunc ;==>_MailSlotRead


; #FUNCTION# ;===============================================================================
;
; Name...........: _MailSlotWrite
; Description ...: Writes message to the specified mailslot.
; Syntax.........: _MailSlotWrite ($sMailSlotName , $vData [, $iMode])
; Parameters ....: $hMailSlot - Mailslot name
;                $vData - Data to write.
;                $iMode - Writing mode.
;                            Can be: 0 - write binary
;                                    1 - write ANSI
;                                    2 - write UTF8
; Return values .: Success - Returns the number of written bytes
;                        - Sets @error to 0
;                Failure - Returns empty string and sets @error:
;                |1 - CreateFileW function or call to it failed.
;                |2 - WriteFile function or call to it failed.
;                |3 - Opened mail slot handle could not be closed.
;                |4 - WriteFile function or call to it failed and additionally opened mail slot handle could not be closed.
; Author ........: trancexx
; Modified.......:
; Link ..........; http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
;                http://msdn.microsoft.com/en-us/library/aa365747(VS.85).aspx
;                http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
;
;==========================================================================================
Func _MailSlotWrite($sMailSlotName, $vData, $iMode = 0)

Local $aCall = DllCall("kernel32.dll", "ptr", "CreateFileW", _
         "wstr", $sMailSlotName, _
         "dword", 0x40000000, _ ; GENERIC_WRITE
         "dword", 1, _ ; FILE_SHARE_READ
         "ptr", 0, _
         "dword", 3, _ ; OPEN_EXISTING
         "dword", 0, _ ; SECURITY_ANONYMOUS
         "ptr", 0)

If @error Or $aCall[0] = -1 Then
     Return SetError(1, 0, 0)
EndIf

Local $hMailSlotHandle = $aCall[0]

Local $bData

Switch $iMode
     Case 1
         $bData = StringToBinary($vData, 1)
     Case 2
         $bData = StringToBinary($vData, 4)
     Case Else
         $bData = $vData
EndSwitch

Local $iBufferSize = BinaryLen($bData)

Local $tDataBuffer = DllStructCreate("byte[" & $iBufferSize & "]")
DllStructSetData($tDataBuffer, 1, $bData)

$aCall = DllCall("kernel32.dll", "int", "WriteFile", _
         "ptr", $hMailSlotHandle, _
         "ptr", DllStructGetPtr($tDataBuffer), _
         "dword", $iBufferSize, _
         "dword*", 0, _
         "ptr", 0)

If @error Or Not $aCall[0] Then
     $aCall = DllCall("kernel32.dll", "int", "CloseHandle", "ptr", $hMailSlotHandle)
     If @error Or Not $aCall[0] Then
         Return SetError(4, 0, 0)
     EndIf
     Return SetError(2, 0, 0)
EndIf

Local $iOut = $aCall[4]

$aCall = DllCall("kernel32.dll", "int", "CloseHandle", "ptr", $hMailSlotHandle)
If @error Or Not $aCall[0] Then
     Return SetError(3, 0, $iOut)
EndIf

Return $iOut

EndFunc ;==>_MailSlotWrite

Func: _NamedPipes_ConnectNamedPipe($hHandle); freezes the program, not only GUI

Greetings wilenty :)

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
Sign in to follow this  
Followers 0