borderAndreaVB free resources for Visual Basic developersborder

AndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincani
:: Send a message to a user in your network

Author  

Andrea Tincani

Language  

VB5, VB6

Operating Systems  

Windows NT, Me, XP and 2K
API Declarations
Option Explicit

Private Declare Function NetMessageBufferSend Lib "netapi32.dll" _
(ByVal servername As String, _
ByVal msgname As String, _
ByVal fromname As String, _
ByVal Buffer As String, _
ByVal BufSize As Long) As Long

Private Const NERR_SUCCESS As Long = 0
Private Const NERR_BASE As Long = 2100
Private Const NERR_NetworkError As Long = (NERR_BASE + 36)
Private Const NERR_NameNotFound As Long = (NERR_BASE + 173)
Private Const NERR_UseNotFound As Long = (NERR_BASE + 150)
Private Const ERROR_ACCESS_DENIED As Long = 5
Private Const ERROR_BAD_NETPATH As Long = 53
Private Const ERROR_NOT_SUPPORTED As Long = 50
Private Const ERROR_INVALID_PARAMETER As Long = 87
Private Const ERROR_INVALID_NAME As Long = 123
Module
Public Function NetSendMessage(ByVal sSendTo As String, ByVal sMessage As String) As Long
    Dim ret As Long
    
    'convert ANSI strings to UNICODE
    sSendTo = StrConv(sSendTo, vbUnicode)
    sMessage = StrConv(sMessage, vbUnicode)
    'Send a network message to a remote computer
    NetSendMessage = NetMessageBufferSend(vbNullString, sSendTo, vbNullString, _
        sMessage, Len(sMessage))
End Function

'returns the description of the Netapi Error Code
Public Function NetSendErrorMessage(ErrNum As Long) As String
    Select Case ErrNum
        Case NERR_SUCCESS
            NetSendErrorMessage = "The message was successfully sent"
        Case NERR_NameNotFound
            NetSendErrorMessage = "Send To not found"
        Case NERR_NetworkError
            NetSendErrorMessage = "General network error occurred"
        Case NERR_UseNotFound
            NetSendErrorMessage = "Network connection not found"
        Case ERROR_ACCESS_DENIED
            NetSendErrorMessage = "Access to computer denied"
        Case ERROR_BAD_NETPATH
            NetSendErrorMessage = "Sent From server name not found."
        Case ERROR_INVALID_PARAMETER
            NetSendErrorMessage = "Invalid parameter(s) specified."
        Case ERROR_NOT_SUPPORTED
            NetSendErrorMessage = "Network request not supported."
        Case ERROR_INVALID_NAME
            NetSendErrorMessage = "Illegal character or malformed name."
        Case Else
            NetSendErrorMessage = "Unknown error executing command."
   End Select
End Function
Usage
Private Sub Command2_Click()
    Dim ret As Long

    'send a message to "andrea" user in your network, replace "andrea" with the name
    'of the user or the computer you want to send the message to

    'in order to receive and send messages in both computers (sender and receiver) you
    'must start the messenger service
    ret = NetSendMessage("andrea", "this is a message from a VB application")
    If ret <> 0 Then
        MsgBox NetSendErrorMessage(ret), vbCritical, "Error"
    Else
        MsgBox NetSendErrorMessage(ret), vbInformation, "NetSend"
    End If
End Sub
:: Navigation

Home

NetAPI, Winsock and Internet

Previous Tip

Next Tip

:: Search this site
Google
:: Related Topics
icon 11-12-2007 Re: Shell Extension and Outlook by stickleprojects
icon 10-12-2007 Shell Extension and Outlook by SamuelHenderson
icon 28-02-2007 Email from VB by Creatica
icon 03-01-2007 SendMessage call by cleopard
icon 24-02-2006 please tell me why my customer form gets hung up by kabba
:: Sponsored Links



Partners: Il portale per lui e lei | Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder