Subscribe to Windows IT Pro
May 01, 1997 12:00 AM

Build a Network Message Utility

Windows IT Pro
InstantDoc ID #559
Rating: (4)
Create a graphical alternative to NET SEND using the Win32 network API

[Editor's Note: VB Solutions is about using Visual Basic (VB) to build a variety of solutions to specific business problems. This column doesn't teach you how to write VB, but how to use VB as a tool to provide quick, easy-to-implement solutions that you can use right away.]

Windows NT's NET SEND command provides a useful way to send network messages; however, you have to run it from the NT command prompt. This month, I present an alternative to NET SEND that lets you send network messages without leaving NT's graphical interface. The NetMessage Visual Basic (VB) utility is a graphical equivalent of NET SEND. Instead of going to the NT command prompt, you can simply run NetMessage from your NT desktop or from the Start menu. Screen 1, page 184, shows the primary interface for NetMessage.

Using NetMessage is easy. The utility prompts you for the name to send a network message to and provides a multiline text box in which you enter the message's text. To send a message to a networked system, you enter the destination user's system name or logon name in the To box. NetMessage lets you either manually enter the name or select it from a drop-down box. You can also send a broadcast message to all users in your domain or other domains by selecting the domain name from the drop-down box, as shown in Screen 2, page 184, or by entering it manually. After entering the recipient's name and message text, you click Send to send the message. Screen 3, page 184, shows an example of the resulting pop-up message displayed on the target system.

The target system must be running the NT Messenger service to receive and display a network message. NT's Messenger service is started by default in most NT installations. The NetMessage utility sends the message via NetBIOS as the network transport.

Inside NetMessage
Now that you've seen how NetMessage works, let's build it. Because the NetMessage utility performs network calls, you might think building it requires complicated C communications coding and a knowledge of system internals. Not true. NT provides a rich set of network APIs, and you can call most of them from VB. By calling these APIs, your VB programs can access a variety of networking functions unavailable in standalone VB. The NetMessage utility uses NT's Win32 network API to handle the networking parts of the application.

The Win32 network API is in NT's netapi32.dll. This DLL contains many functions, but the NetMessage utility uses only three of them: NetServerEnum, NetWkstaGetInfo, and NetMessageBufferSend. Like several other VB Solutions utilities, NetMessage uses NetServerEnum to retrieve a list of networked systems--specifically, a list of all NT systems in the network. The NetWkstaGetInfo function retrieves the names of the network domain and local system--both of which are required to send a message across the network. The NetMessageBufferSend function sends a message across the network.

Because the functions contained in netapi32.dll are not built into VB, you must declare them before you can use them. (I explained the NetServerEnum function in the November 1996 VB Solutions column). The VB declaration for the NetWkstaGetInfo function is

Declare Function NetWkstaGetInfo _

Lib "Netapi32"
_

(ByVal sServerName$, _

ByVal lLevel&, vBuffer As Any) _

As Long

NetWkstaGetInfo is a fairly simple function that takes only three parameters. The first parameter is a Unicode string that contains the name of the server on which the function will be executed. A null string specifies that the function will run on the local system. The second parameter determines the level of information the function will return. NetWkstaGetInfo returns several different types of information depending on the value you specify in this parameter. However, the NetMessage utility uses only the most basic information, specified by passing the value 100. The third parameter is a pointer to a buffer that contains a data structure filled with the network information that corresponds to the information level specified by the second parameter.

The VB declaration for the NetMessageBufferSend function is

Declare Function _NetMessageBufferSend _Lib "Netapi32"_

(ByVal sServerName$, _

ByVal sMsgName$, _

ByVal sFromName$, _

ByVal sMessageText$, _

ByVal lBufferLength&) _

As Long

The NetMessageBufferSend function's first four parameters take Unicode strings. The first parameter contains the name of the server on which the function will be executed. The second parameter contains the message recipient's name. The third parameter contains the message sender's name. The fourth parameter contains the text of the message to be sent. The fifth parameter, a long integer, identifies the length of the message to be sent.

Because the NetMessageBufferSend function is declared in a DLL, all the string parameters are declared with the ByVal keyword. Although ByVal ordinarily causes VB to pass a parameter's value, this keyword works differently for passing string parameters to an external DLL such as netapi32.dll. Using ByVal to pass strings to functions contained in external DLLs causes VB to convert the strings from VB string format into the C string format required by most DLLs, including netapi32.dll. The fifth parameter also uses the ByVal keyword, but because this parameter contains a numeric variable and not a string, ByVal works as you'd expect: It causes VB to pass the value of the numeric variable, rather than passing a pointer to the variable.

Related Content:

ARTICLE TOOLS

Comments
  • GaneshKumar
    4 years ago
    Apr 29, 2008

    Hi all,
    This program is well. But I have to change the title name in Screen3, instead of Messenger service, our own title will be present in title bar. Could it passible? if possible, please can you let me know?

    Awaiting for your reply.

    Thankyou,

  • ulambayar
    7 years ago
    Dec 07, 2005

    Hello,

    My question is the same someone asked before.


    >>>
    It would be interesting to know how to intercept the receive message before it pops up into a message box.

    nhu chung - October 17, 2003
    >>>

    Regards,

  • Anonymous User
    7 years ago
    May 10, 2005

    To recieve messages, use this code.

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Const BM_CLICK = &HF5
    Const WM_GETTEXTLENGTH = &HE
    Private Const WM_GETTEXT = &HD

    Private Sub Timer1_Timer()
    Dim lWin As Long
    Dim Control As Long
    Dim text As String
    Dim textlen As Long
    lWin = FindWindow(vbNullString, "Messenger Service ")
    If lWin <> 0 Then
    Control = FindWindowEx(lWin, 0, "Static", vbNullString)
    textlen = SendMessage(Control, WM_GETTEXTLENGTH, vbNull, vbNull)
    textlen = textlen + 1
    text = Space$(textlen)
    Call SendMessage(Control&, WM_GETTEXT, textlen, ByVal text)

    Control = FindWindowEx(lWin, 0, "Button", vbNullString)
    SendMessage Control, BM_CLICK, 0, 0
    List1.AddItem text
    End If
    End Sub

  • Anonymous User
    7 years ago
    Mar 21, 2005

    hey if u have multiple domains in ur network, then this code wont run. for an updated code, please write back to me at kumar754@gmail.com

  • Anonymous User
    7 years ago
    Jan 08, 2005

    >>then the entire program locks up until it
    >>returns an error message. Is there any way I
    >>can prevent this ?

    Just and idea but perhaps you could move the NetMessageBufferSend call out of the click event and into its own function in the standard module and then use the CreateThread API.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.