NetMessageBufferSend
Posted: March 3rd, 2004, 11:36 pm
anyone know how to use NetMessageBufferSend
is it for vb or c++ or both?
I found some code
and
is it for vb or c++ or both?
I found some code
Code: Select all
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Runtime.Remoting.Messaging
Namespace PAB.Util
Public Class NETSender
Public Delegate Function SendMessageDelegate(ByVal Dest As String, ByVal Message As String) As Integer
Public Function SendMessage(ByVal strDestination As String, ByVal strMessage As String) As Integer
Dim str2 As String
Try
Dim str1 As String = String.Concat(New String() {"net send ", strDestination, " ", strMessage, " > C:\sendResult.log"})
Dim fileStream1 As FileStream = New FileStream("c:\NetSender.bat", FileMode.Create, FileAccess.Write)
Dim streamWriter As streamWriter = New streamWriter(fileStream1)
streamWriter.BaseStream.Seek(CLng(0), SeekOrigin.End)
streamWriter.Write(str1)
streamWriter.Flush()
streamWriter.Close()
fileStream1.Close()
Dim process As process = New process
process.StartInfo.FileName = "C:\NetSender.bat"
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
process.Start()
process.WaitForExit()
process.Close()
Dim fileStream2 As FileStream = New FileStream("C:\sendResult.log", FileMode.Open, FileAccess.Read)
Dim streamReader As streamReader = New streamReader(fileStream2)
streamReader.BaseStream.Seek(CLng(0), SeekOrigin.Begin)
str2 = streamReader.ReadLine()
streamReader.Close()
fileStream2.Close()
If str2.IndexOf("successfully sent") > -1 Then
Return 1
Else
Return 9
End If
Catch e As Exception
Return 9
End Try
End Function
Public Sub GetResultsOnCallback(ByVal ar As IAsyncResult)
Dim res As Integer
Dim SendMessageDelegate As SendMessageDelegate = CType(CType(ar, AsyncResult).AsyncDelegate, SendMessageDelegate)
Try
res = SendMessageDelegate.EndInvoke(ar)
Catch e As Exception
End Try
End Sub
Public Function SendMessageAsync(ByVal Destination As String, ByVal Message As String) As String
Dim SendMessageDelegate As SendMessageDelegate = AddressOf Me.SendMessage
Dim asyncCallback As asyncCallback = AddressOf Me.GetResultsOnCallback
SendMessageDelegate.BeginInvoke(Destination, Message, asyncCallback, Nothing)
Return "ok"
End Function
End Class
End Namespace
N.B. - Oops! I spoke too soon. Got the API working now, in C#:
using System;
using System.Runtime.InteropServices;
namespace PAB.Util
{
public class NetSend
{
public NetSend()
{
}
public int netSend(string sFrom, string sTo, string sMessage)
{
byte [] bBuffer = System.Text.Encoding.Unicode.GetBytes(sMessage);
int nRet = NetMessageBufferSend(null, sTo, null, sMessage, sMessage.Length *
2 + 2);
return nRet;
}
[DllImport ("Netapi32", CharSet=CharSet.Unicode)]
public static extern int NetMessageBufferSend(
string servername,
string msgname,
string fromname,
string buf,
int buflen);
}
}
And again, in VB.NET:
Imports Microsoft.VisualBasic
Imports System
Imports System.Runtime.InteropServices
Imports System.Text
Namespace PAB.Util
Public Class NetSend
Public Function netSend(sFrom As String, sTo As String, sMessage As String) As Integer
Dim bBuffer As Byte() = Encoding.Unicode.GetBytes(sMessage)
Return NetMessageBufferSend(Nothing, sTo, Nothing, sMessage, sMessage.Length * 2 + 2)
End Function
<DllImportAttribute("Netapi32", CharSet:=CharSet.Unicode)> _
Public Shared Function NetMessageBufferSend(servername As String, msgname As String, fromname As String, buf As String, buflen As Integer) As Integer
End Function
End Class
End Namespace
and
Code: Select all
VERSION 4.00
Begin VB.Form Form1
Caption = "VB Solutions - Send Net Message"
ClientHeight = 3825
ClientLeft = 2940
ClientTop = 2325
ClientWidth = 7275
Height = 4230
Icon = "FormSend.frx":0000
Left = 2880
LinkTopic = "Form1"
ScaleHeight = 3825
ScaleWidth = 7275
Top = 1980
Width = 7395
Begin VB.Frame Frame1
Height = 3255
Left = 120
TabIndex = 0
Top = 0
Width = 6975
Begin VB.ComboBox Combo1
Height = 315
Left = 600
TabIndex = 5
Top = 360
Width = 2415
End
Begin VB.CommandButton Command_Send
Caption = "Send"
Height = 375
Left = 4920
TabIndex = 2
Top = 360
Width = 1695
End
Begin VB.TextBox Text_Message
Height = 2055
Left = 240
MultiLine = -1 'True
TabIndex = 1
Top = 960
Width = 6375
End
Begin VB.Label Label2
Caption = "To:"
Height = 255
Left = 240
TabIndex = 4
Top = 360
Width = 495
End
Begin VB.Label Label1
Caption = "Label1"
Height = 255
Left = 480
TabIndex = 3
Top = 360
Width = 15
End
End
Begin VB.Label Label_Feedback
BorderStyle = 1 'Fixed Single
Height = 375
Left = 120
TabIndex = 6
Top = 3360
Width = 6975
End
End
Attribute VB_Name = "Form1"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
Private Sub Command_Send_Click()
Dim lReturnCode As Long
Dim sUnicodeToName As String
Dim sUnicodeFromName As String
Dim sUnicodeMessage As String
Dim lMessageLength As Long
'Get the local computer name and convert it to unicode
sUnicodeFromName = StrConv(GetLocalSystemName, vbUnicode)
' Convert the to computer name to Unicode
sUnicodeToName = StrConv(Combo1.Text, vbUnicode)
' Convert the message text to unicode
sUnicodeMessage = StrConv(Text_Message.Text, vbUnicode)
lMessageLength = Len(sUnicodeMessage)
' Hourglass pointer
MousePointer = vbHourglass
Label_feedback.Caption = vbNullString
' Send the message
lReturnCode = NetMessageBufferSend("", _
sUnicodeToName, _
sUnicodeFromName, _
sUnicodeMessage, _
lMessageLength)
' Prove some feedback about the send action
If lReturnCode = 0 Then
Text_Message.Text = vbNullString
Label_feedback.Caption = "Message was successfully sent"
Else
Label_feedback.Caption = "Error - Return code: " & CStr(lReturnCode)
End If
' Default pointer
MousePointer = vbDefault
End Sub
Private Sub Form_Load()
Dim colServerNames As New Collection
Set colServerNames = GetNetworkSystemNames(SERVER_TYPE_NT)
For Num = 1 To colServerNames.Count
Combo1.AddItem colServerNames.Item(Num)
Next
Combo1.AddItem GetDomainName & "*"
End Sub