We help IT Professionals succeed at work.

Messaging to users on network using VB

177 Views
Last Modified: 2013-12-25
I have a Visual Basic application which is used by 15 people on a network.  It uses a Microsoft Access database.  From time to time, the database needs to be repaired/compacted and for this, all users have to be out of the app.  Is there a way, in Visual Basic, to either send all users of the application a message saying to quit the system or, even better, force them out?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2013

Commented:
You can force a reboot/ log off of a remote system with the windows shutdown command.
Personally I prefer the Sysinternals (now Microsoft) free equivalent which I find more dependable and gives you more options. You can create a one line batch file with a text list of the computers you want to affect and a message like; "your system will be re-booted in 10 minutes, please save your work".
http://www.microsoft.com/technet/sysinternals/utilities/psshutdown.mspx
CERTIFIED EXPERT
Top Expert 2011

Commented:
Add a VBA module and paste the following:

It can then be called from your application like this:

sendmessage("yourid", "theirid", "messagetext")

Chris
Private Declare Function NetMessageBufferSend Lib _
  "NETAPI32.DLL" (yServer As Any, yToName As Byte, _
  yFromName As Any, yMsg As Byte, ByVal lSize As Long) As Long
Private Const NERR_Success As Long = 0&
Public Function SendMessage(RcptToUser As String, _
   FromUser As String, BodyMessage As String) As Boolean
 
   Dim RcptTo() As Byte
   Dim From() As Byte
   Dim Body() As Byte
 
   RcptTo = RcptToUser & vbNullChar
   From = FromUser & vbNullChar
   Body = BodyMessage & vbNullChar
 
   If NetMessageBufferSend(ByVal 0&, RcptTo(0), ByVal 0&, _
        Body(0), UBound(Body)) = NERR_Success Then
     SendMessage = True
   End If
 
End Function
Private Sub Form_Load()
    'Example created by X-MaD (x-mad@zolnetwork.com)
    'Visit his website at http://www.zolnetwork.com/x-mad
    Dim RetVal As Boolean
    RetVal = SendMessage("Utente", "FromUser", "BodyText")
End Sub

Open in new window

CERTIFIED EXPERT
Top Expert 2011

Commented:
Credit goes to http://allapi.mentalis.org/apilist/NetMessageBufferSend.shtml where I found it ... and it works too!

Note you can ignore the form_load sub at the end, it's not required in this case.

Chris
CERTIFIED EXPERT
Top Expert 2011

Commented:
Sorry to | from | message is teh correct order!
CERTIFIED EXPERT
Top Expert 2011

Commented:
Out of interest this works by using the net send protocol so it is the same as using net send manually just loop in VBA for each user/computer identity and send each of them the message.

Chris

Author

Commented:
Thanks for the quick response Chris.  This looks like it's very close to what I need.  However, do I need to know explicitly the identity of everyone on the network or is there a generic way of finding this out?
CERTIFIED EXPERT
Top Expert 2011

Commented:
I think that'll be * as the recipient ... don't dare try it out here as hundreds will complain!

Chris

Author

Commented:
Thanks Chris.

I've added the code as suggested.  However, in my test environment, I can only get a reaction if I send the message to myself - i.e., if I have the recipient in as "Administrator" the message pops up on my screen but even though I've tried the user-name and the machine name of my other machine, the message doesn't appear on my other machine.  Also, the "*" parameter doesn't seem to work either.

John
CERTIFIED EXPERT
Top Expert 2011

Commented:
It is netwrok based ... I tried it out with others singly with no issues.  You are using teh corrected syntax i.e.

sendmessage("theirid", "yourid", "messagetext")

ALso what happens with net send .... as far as I can see the operation is identical so if you can net send them then this ought to work.

Chris

Author

Commented:
My problem I think is in the "theirid" parameter.  Is this their login, or their machine name, or what?
CERTIFIED EXPERT
Top Expert 2011

Commented:
Notionally their network logon name.
CERTIFIED EXPERT
Top Expert 2011

Commented:
MAchine name ought to work as well

Commented:
i think you will have a problem with the code given by chris. net send will only work on computers that have their messenger service enabled. in some computers this services is disabled.

Computers with disabled messenger services will not receive any message.

I suggest using winsock component. You can send messages to other computers and can also manipulate the running system to forcefully close the program.
CERTIFIED EXPERT
Top Expert 2013

Commented:
Have you considered the psshutdown option?

As an example, I created a 1 line batch file to shut down PC's. You can shutdown, reboot, or logoff.
Download  PSShutdown free from:
http://www.microsoft.com/technet/sysinternals/utilities/psshutdown.mspx
 
Create a batch file with a line similar to:
psshutdown.exe @CompList.txt -f -c -t 15 -m "You computer will shutdown in 15 minutes . You can cancel if necessary."
 
Then create a text file in the same folder with a list of the computers to be shut down, in this example named the file CompList.txt:
 
Computer1
Computer2
Computer3
 
Options chosen in the above example:
CompList.txt  = text file with the list of computers
-f                    =forces all running applications to exit
-c                   =give user ability to cancel shutdown
-t 15                =time in minutes until shutdown
-m "message"  =message displayed in message box on remote computer
 
Director of Software Engineering
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.