3

I have a couple of applications that depend on each other. One is a robust vb6 application. The other is my eagerly anticipated, stealth technology employing, and likely oscar winning .net (2.0) systray application. The vb6 app starts the systray app. And if the VB6 app is shut down, the systray app will shut itself down assuming it has no other work to do (it's work is saving documents to a database).

My sole remaining concern is what happens if the systray app crashes or cannot do its work due to some fatal error. A likely outcome of this condition is me popping up a modal dialog telling the user "contact support immediately, because you can't save documents any more".

I think the worst case scenario here is that whenever a document is processed in the vb6 app (that is, a user completes the document and it's components are zipped up and saved locally), I'll just have to check and see if the systray app is running. I don't know, that seems a bit cumbersome. Is there a more elegant approach? Is there some way I can "signal" my vb6 app/have my vb6 app listen for such a signal?

peacedog
  • 1,415
  • 20
  • 32

5 Answers5

3

what I have done in the past is a class library in .NET exposed as COM object (in the assembly info, put ComVisible attribute to true).

This way from VB6 you can call the methods you have exported from .NET class library and in C# you can have all the code you need to handle the situation.

In this way I write less code in VB6 and more in C# where we can profit of better exception handling (try-catch-finally), logging frameworks like Log4Net and so on...

this has worked very well for me so far.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • You can add a method called `Ping` too! (just to make all the other answers to this question obsolete) – wqw Aug 15 '11 at 11:46
0

You could use WMI to check if it's running quite easily by using code similar to:

dim foundIt
For Each Process in GetObject( "winmgmts://." ).InstancesOf( "win32_process" )
    If UCase( Process.name ) = "MYDOTNET.EXE" Then
        foundIt = true 
    End If
Next
if not foundIt then
    msgbox( "It's not running, so start the process")
end if

Please note, wrote the above as VBscript, but should work as VB6 code I would have thought.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • 1
    WMI should never be used in applications. It is an admin tool that may be disabled or not even present in a given Windows installation. – Bob77 Aug 11 '11 at 23:19
  • @bob: Depends on the application and who it is for. I'd avoid using WMI for an app that was for home consumers, but if it's an app for in-house use in his company or similar, which I assumed, but which might not be the case, then he could just ask the Admins if WMI is available and there wouldn't be a problem. – Hans Olsson Aug 12 '11 at 07:39
0

I don't have details handy but I've used windows messaging to send a WM_NULL in the past. It amounts to a ping message. So the VB6 app sends a periodic ping to the systray app. If the systray app doesn't respond to the ping in a timely manner then the VB6 app knows it is hung or not running.

Kevin Gale
  • 4,350
  • 6
  • 30
  • 31
-1

My first guess would be introduce intercommunication between the two. Have the VB6 app send a PING request on a defined interval. Then if the VB6 app does not receive a response or the request times out, do what you need to do.

Josh
  • 2,955
  • 1
  • 19
  • 28
  • PING Request to another app on the same machine? – Matt Wilko Aug 12 '11 at 12:26
  • Why not? I think you're taking my PING as a command shell ping? I'm just talking about keep alives. It's done all the time, especially between applications that rely on each other. Just because applications are on the same machine doesn't mean they cannot communicate with each other. It's no different than one monitoring a file the other has created. I've worked at a few large engineering firms that have used this method. It's more performant than finding a window or monitoring processes for a running executable. It also allows flexibility if apps need to run on separate machines. – Josh Aug 12 '11 at 12:44
  • Yes PING to most people would mean shell command PING. Can I suggest you edit your answer to be a bit clearer maybe with some example code? – Matt Wilko Aug 12 '11 at 13:20
-1

I would suggest using something like the following function on a timer:

'declaration for FindWindow API
Private Declare Function FindWindow _
        Lib "user32" _
        Alias "FindWindowA" _
       (ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Long



'Call the following on a timer with the Tray icon form caption
'when it goes False unleash hell
Private Function IsWindowLoaded(ByVal strWindowCaption As String) As Boolean
    Dim lHwnd As Long
    lHwnd = FindWindow(strWindowCaption, vbNullString)
    If lHwnd <> 0 Then
        IsWindowLoaded = True
    Else
        IsWindowLoaded = False
    End If
End Function
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • You're doing the same thing I suggested below but with way more overhead. If you read Kevin Gale's post above he says the same thing. Are you going to vote him down for using a real-world scenario? – Josh Aug 12 '11 at 12:57
  • @Josh - Kevin Gales answer states Windows messaging - I just think you could edit your answer to be clearer as to what you mean. You could probably delete it now anyway as Kebin Gale has said the same thing. – Matt Wilko Aug 12 '11 at 13:25