Link to home
Start Free TrialLog in
Avatar of micd
micd

asked on

Call NetMessageBufferSend function from vb.net

I need to call the NetMessageBufferSend function from an a vb.net application.  I used to do it as follows in VB6 but I cannot convert the function for it to work in vb.net....  I cannot find the proper way to pass the parameters to the function it seems



' Declare the function for the net nessage call
Private Declare Function NetMessageBufferSend _
    Lib "netapi32.dll" (ByVal lpServerName As Byte, _
    lpMsgName As Byte, ByVal lpFromName As Byte, _
    lpBuf As Byte, ByVal lnBufLen As Long) As Long
                         
' Declare the function for the send message call
Private Function SendMsg(lpServerName As String, lpMsgName As String, _
    lpFromName As String, lpBuf As String, lngBufLen As Long) As Long
                         
    Dim arServerName() As Byte
    Dim arMsgName() As Byte
    Dim arFromName() As Byte
    Dim arBuf() As Byte
                       
    arServerName = ConvertToByte(lpServerName & "")
    arMsgName = ConvertToByte(lpMsgName & "")
    arFromName = ConvertToByte(lpFromName & "")
    arBuf = ConvertToByte(lpBuf & "")
                       
    SendMsg = NetMessageBufferSend(0, arMsgName(0), _
        arFromName(0), 0, lngBufLen)

End Function
Avatar of naveenkohli
naveenkohli

You can use PInvoke to call the unmanaged API functions. Then you don't have to worry about converting the parameters to Byte type. You can use the regular string variables to pss the values.
Here is an example from documentation..

Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic

<StructLayout(LayoutKind.Sequential)> Public Class MySystemTime
   Public wYear As Short
   Public wMonth As Short
   Public wDayOfWeek As Short
   Public wDay As Short
   Public wHour As Short
   Public wMinute As Short
   Public wSecond As Short
   Public wMiliseconds As Short
End Class

Public Class Win32
   Declare Auto Sub GetSystemTime Lib "Kernel32.dll"(sysTime _
      As MySystemTime)
   Declare Auto Function MessageBox Lib "User32.dll"(hWnd As Integer, _
      txt As String, caption As String, Typ As Integer) As Integer
End Class

Public Class TestPlatformInvoke    
   Public Shared Sub Main()
           Dim sysTime As New MySystemTime()
           Win32.GetSystemTime(sysTime)

           Dim dt As String
           dt = "System time is:" & ControlChars.CrLf & _
            "Year: " & sysTime.wYear & _
              ControlChars.CrLf & "Month: " & sysTime.wMonth & _
              ControlChars.CrLf & "DayOfWeek: " & sysTime.wDayOfWeek & _
              ControlChars.CrLf & "Day: " & sysTime.wDay
         Win32.MessageBox(0, dt, "Platform Invoke Sample", 0)      
   End Sub
End Class

You use the same technique to call methods fron NetApi

For more information..

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconconsumingunmanageddllfunctions.asp?frame=true
Avatar of micd

ASKER

can you explain how am  to convert the netAPI function using this style?... i got this far but i still can't figure out how to call the function

Imports System.Runtime.InteropServices

Public Class Win32
    <DllImport("netapi32.dll", CharSet:=CharSet.Auto)> _
    Public Shared Function NetMessageBufferSend(ByVal lpServerName As Byte, _
    ByVal lpMsgName As Byte, ByVal lpFromName As Byte, _
    ByVal lpBuf As Byte, ByVal lnBufLen As Long) As Long
    End Function
End Class
I am not very good at VB. I will try to give you some pseudo code. One more theng, you can chnage all Byte type variables to String.

Public Class Win32
   <DllImport("netapi32.dll", CharSet:=CharSet.Auto)> _
   Public Shared Function NetMessageBufferSend(ByVal lpServerName As String, _
   ByVal lpMsgName As String, ByVal lpFromName As String, _
   ByVal lpBuf As Byte, ByVal lnBufLen As Long) As Long
   End Function
End Class

In your function some where...

Dim strServer As String = "Foo"
Dim strMsg As String = "MsgDoh"
Win32.NetMessageBufferSend(strServer, strMsg, strSomeFormName, bytrBuf, 4)
Avatar of micd

ASKER

OK... i am executing the API call as follows:

Public Class Win32
        <DllImport("netapi32.dll", CharSet:=CharSet.Unicode)> _
        Public Shared Function NetMessageBufferSend(ByVal lpServerName As String, _
        ByVal lpMsgName As String, ByVal lpFromName As String, _
        ByVal lpBuf As String, ByVal lnBufLen As Long) As Long
        End Function
    End Class

success = Win32.NetMessageBufferSend(sServer, sTo, sFrom, sMessage, 255)

The message is working... the message box appears and all, the only thing is that it does not return  NERR_SUCCESS (0) which is supposed to be returned if the function executed without errors.  It instead returns a big figure that is not always the same...such as 7599828666155008
ASKER CERTIFIED SOLUTION
Avatar of naveenkohli
naveenkohli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of micd

ASKER

Great!!!
function returns 0...the code is below.
thanks naveenkohli

    Public Structure NetMessageData
        Dim sServerName As String
        Dim sSendTo As String
        Dim sSendFrom As String
        Dim sMessage As String
    End Structure

    Public Class Win32
        <DllImport("netapi32.dll", CharSet:=CharSet.Unicode)> _
        Public Shared Function NetMessageBufferSend(ByVal lpServerName As String, _
        ByVal lpMsgName As String, ByVal lpFromName As String, _
        ByVal lpBuf As String, ByVal lnBufLen As Long) As Int32
        End Function
    End Class


    Public Function NetSendMessage(ByVal msgData As NetMessageData) As Long
        Dim sMessage As String
        Dim success As Long
        With msgData
            If Len(.sMessage) > 0 And Len(.sSendTo) > 0 Then
                sMessage = Space$(255)
                sMessage = .sMessage
                success = Win32.NetMessageBufferSend(.sServerName, .sSendTo, .sSendFrom, sMessage, 255)
                NetSendMessage = success
            End If
        End With  'With msgData
    End Function
Hi Naveen and MICD,

  guys the code is gr8 .. but couple of times the message fails to be sent across..the network

Reason being the return value of success (in NetSendMessage) comes out to be 2273..

I am having some serious problem with the NETMESSAGEBUFFERSEND()

Please do throw your light...
Here is the source code which i intend to execute
-----------------------------------------------------------------------

Imports System.Runtime.InteropServices

Public Class TressPasser
    Inherits System.Windows.Forms.Form
.......
 
    Public Structure NetMessageData
        Dim sServerName As String
        Dim sSendTo As String
        Dim sSendFrom As String
        Dim sMessage As String
    End Structure


    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click


        msgData.sServerName = "SERVER"
        msgData.sSendTo = "WS1"                       'WS1 --> WORK STATION 01
        msgData.sSendFrom = "SERVER"
        msgData.sMessage = "Hello"

        success = NetSendMessage(msgData)

        If success = 0 Then
            MessageBox.Show("Message Sent Successfully")
        Else
            MessageBox.Show("Message Failed")
        End If

    End Sub

    Public Function NetSendMessage(ByVal msgData As NetMessageData) As Long
        Dim sMessage As String
        Dim success As Long
       
        With msgData
            If Len(.sMessage) > 0 And Len(.sSendTo) > 0 Then
                sMessage = Space$(255)
                sMessage = .sMessage
                success = Win32.NetMessageBufferSend(.sServerName, .sSendTo, .sSendFrom, sMessage, 255)
                MsgBox(success)
                NetSendMessage = success
            End If
        End With  'With msgData
    End Function
End Class


Public Class Win32
    <DllImport("netapi32.dll", CharSet:=CharSet.Unicode)> _
    Public Shared Function NetMessageBufferSend(ByVal lpServerName As String, _
    ByVal lpMsgName As String, ByVal lpFromName As String, _
    ByVal lpBuf As String, ByVal lnBufLen As Long) As Int32
    End Function
End Class
Avatar of micd

ASKER

This is very similar to above using a NetSend class.....

-----------------------------------------------------------------------
Class:
-----------------------------------------------------------------------

Imports System.Runtime.InteropServices

Public Class NetSend

    Private Const ERROR_ACCESS_DENIED As Long = 5
    Private Const ERROR_BAD_NETPATH As Long = 53
    Private Const ERROR_INVALID_PARAMETER As Long = 87
    Private Const ERROR_NOT_SUPPORTED As Long = 50
    Private Const ERROR_INVALID_NAME As Long = 123
    Private Const NERR_BASE As Long = 2100
    Private Const NERR_SUCCESS As Long = 0
    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)

    ' Declare the function for the net nessage call

    Public Structure NetMessageData
        Dim sServerName As String
        Dim sSendTo As String
        Dim sSendFrom As String
        Dim sMessage As String
    End Structure

    Public Class Win32
        <DllImport("netapi32.dll", CharSet:=CharSet.Unicode)> _
        Public Shared Function NetMessageBufferSend(ByVal lpServerName As String, _
        ByVal lpMsgName As String, ByVal lpFromName As String, _
        ByVal lpBuf As String, ByVal lnBufLen As Long) As Int32
        End Function
    End Class

    Public Function NetSendMessage(ByVal msgData As NetMessageData) As Long
        Dim sMessage As String
        Dim success As Long
        With msgData
            If Len(.sMessage) > 0 And Len(.sSendTo) > 0 Then
                sMessage = Space$(255)
                sMessage = .sMessage
                success = Win32.NetMessageBufferSend(.sServerName, .sSendTo, .sSendFrom, sMessage, 255)
                NetSendMessage = success
            End If
        End With  'With msgData
    End Function

End Class

-----------------------------------------------------------------------
Usage:
-----------------------------------------------------------------------

Dim aMsg As NetSend.NetMessageData
Dim nMsg As New NetSend()

aMsg.sMessage = sMessage
aMsg.sSendTo = sIP
aMsg.sSendFrom = sAPPName
nMsg.NetSendMessage(aMsg)


Hi Micd,
   
   It works fine if I were to send msg from MS WIndows Proffessional / Server 2000(source) to any of the Windows 2000 Proff/XP ...(target)

But I recieve the success = 2273 when i send the msg to WIN NT 4.0 OS (target)...
Is there a compatibility issue with WIn NT Version OS..??

Could you tell me why is it so...
Avatar of micd

ASKER

I found that in NT4, you can send a message using the Computer Name to send to.  If you use an IP address then error 2273 is generated.  In WIN2k both ways work.
HI micd...
   Well for aMsg.sSendTo I was providing the machine name rather than the IP address...