Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

two way communication between 2 programs

I am trying to resolve some communication problems.  That is set up some inter-program communication and do not know how to do it.  I have a class library with a COMClass in it. A third party app invokes this class library and it in turn executes a vb.net windows app (EXE) passing command arguments to it.
The windows app can be a console app.  What it does does not have to have a gui.   It does need to send one byte back to the calling program (in a string variable).  How?  Use this scenerio to reply and provide code snippets where necessary for Calling and Called programs

1.  The only function of the called program is to send the letter "Y" back to the calling program.
2.  The only functions of the calling program is to execute the called program and to receive the "Y" from the called program.
Calling Program:ClassLibrary1,  ComClass = Call_it    
Called program = Prog2

Inside call_it

lPid = Shell(strPath & "Prog2.exe", vbNormalFocus)

I have no trouble  calling the program... How do I get the "Y" back to the calling program??

This is worth a million points to me but 500 is all that it will be

0
garyinmiami2003
Asked:
garyinmiami2003
  • 4
  • 4
1 Solution
 
Luis PérezSoftware Architect in .NetCommented:
Well, there are some ways in whay you can do this. The simplest way is that caller class, after calling Prog2.exe, keeps monitoring a file on disk in which Prog2.exe writes the "Y" byte when it finish its work. When caller class detects the "Y" byte on the file, then knows the work is done. This is, as I said, the simplest way you can do it, without involving uses of complicated APIs. If you need specific code on how to do it, or if you prefer other approach, tell me.
0
 
garyinmiami2003Author Commented:
RolandDeschain:

If I could save anything to disk, it would be easy.  Unfortunately, it needs to be passed back somehow without saving.
 
0
 
Luis PérezSoftware Architect in .NetCommented:
Well, we'll try other way. Let's see if you can do it with Atoms.

As Msdn says, an atom-table is a system-defined table that stores strings and, for each string, a corresponding integer id. In every windows system exists a global atom table that can be used and accesed by all windows programs using win32 APIs funcions. This global atom table is, by definition, *global*, that is, if you create a value for that table from withing a program, then you can read that value from withing another program. The value keeps in the table until you delete it from any program.

Unfortunately, you can not choose the ID for the value that you create in the table; instead of that, you create the new value passing the string (you must ensure yourself that the value you pass must be unique), and Atom funcions automatically assign a new ID to your string. But from any other program you can try to find the same exactly string.

Let's see it with a little example:

First, create this little class with all the needed for the atom management. The class code is in the code attached.

Now, let's see how to use:

'From caller class:
lPid = Shell(strPath & "Prog2.exe", vbNormalFocus)
'And keep waiting (each second) for a new atom
Dim iAtom As Short
Do
    'Pause 1 second
    System.Threading.Thread.Sleep(1000)
    'Search for the atom
    iAtom = AtomManagement.FindAtom("MySearchedValue_Y")
Loop Until iAtom <> 0
'If you don't need more the Atom value, delete it to free memory
AtomManagement.DeleteAtom(iAtom)

---------------------------
'From called program, Prog2.exe
.... do all the work, and when done (or when you want, in all case)....
AtomManagement.AddAtom("MySearchedValue_Y")

-------------------------------

Well, I hope that this approach is good for you, as it doesn't use disk files. It doesn't fit exactly to you needs, as it doesn't transmit a "1-byte" *Y* value, but a string in global windows memory, but I think that it can work for your purposes. Hope that helps.
Public Class AtomManagement
    'The API declarations to do the magic
    Private Declare Auto Function GlobalAddAtom Lib "kernel32.dll" Alias "GlobalAddAtomA" (ByVal lpString As String) As Short
    Private Declare Auto Function GlobalFindAtom Lib "kernel32.dll" Alias "GlobalFindAtomA" (ByVal lpString As String) As Short
    Private Declare Auto Function GlobalDeleteAtom Lib "kernel32.dll" (ByVal nAtom As Short) As Short
 
    'Create a new atom and return the ID associated. If failed, returns 0.
    Public Shared Function AddAtom(ByVal TheString As String) As Short
        Return GlobalAddAtom(TheString)
    End Function
 
    'Find an existing atom. If found, returns associated ID. If not found, returns 0.
    Public Shared Function FindAtom(ByVal TheString As String) As Short
        Return GlobalFindAtom(TheString)
    End Function
 
    'Delete an existing atom. This frees a little memory.
    Public Shared Sub DeleteAtom(ByVal TheAtom As Short)
        Call GlobalDeleteAtom(TheAtom)
    End Sub
End Class

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
garyinmiami2003Author Commented:
RolandDeshain:

This is going to work I think but I need the GetAtom function.  I have tried to find specifically that on the internet.  It would be a big help if you could proved the function and the call.  I'm ready to award the points.  Thank you for the big helping hand.

garyinmiami2003
 
0
 
Luis PérezSoftware Architect in .NetCommented:
I guess that what you are saying is GlobalGetAtomName, the function that given an atom ID returns the associated string. I also made 2 small corrections to the code (I apologyze for this... I didn't used atoms since a long time ago!!). The corrections are in adding the null characters at the end of the strings in both the add and the find functions. Well, I attach the complete code of the AtomManagement class including the new function that you requested. Test it, you'll find it very useful.

Hope that helps.
Public Class AtomManagement
    'The API declarations to do the magic
    Private Declare Function GlobalAddAtom Lib "kernel32.dll" Alias "GlobalAddAtomA" (ByVal lpString As String) As Short
    Private Declare Function GlobalFindAtom Lib "kernel32.dll" Alias "GlobalFindAtomA" (ByVal lpString As String) As Short
    Private Declare Function GlobalDeleteAtom Lib "kernel32.dll" (ByVal nAtom As Short) As Short
    Private Declare Function GlobalGetAtomName Lib "kernel32.dll" Alias "GlobalGetAtomNameA" (ByVal nAtom As Short, ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
 
    'Create a new atom and return the ID associated. If failed, returns 0.
    Public Shared Function AddAtom(ByVal TheString As String) As Short
        TheString += Chr(0)
        Return GlobalAddAtom(TheString)
    End Function
 
    'Find an existing atom. If found, returns associated ID. If not found, returns 0.
    Public Shared Function FindAtom(ByVal TheString As String) As Short
        TheString += Chr(0)
        Return GlobalFindAtom(TheString)
    End Function
 
    'Delete an existing atom. This frees a little memory.
    Public Shared Sub DeleteAtom(ByVal TheAtom As Short)
        Call GlobalDeleteAtom(TheAtom)
    End Sub
 
    'Get an atom string given associated atom ID
    Public Shared Function GetAtomName(ByVal AtomID As Short) As String
        Dim sBuffer As String = Space(500)
        Dim iLength As Integer = sBuffer.Length
        Dim iRet As Integer = GlobalGetAtomName(AtomID, sBuffer, iLength)
        If iRet = 0 Then
            Return ""
        Else
            Return sBuffer.Substring(0, iRet)
        End If
    End Function
End Class

Open in new window

0
 
garyinmiami2003Author Commented:
I had never heard of Atom Tables.  I am not sure if there are problems associated in the future with using this but this allows me to move on and I think you for the big help!
0
 
Luis PérezSoftware Architect in .NetCommented:
While you play your program in any Win32 platform (WinNT4, Win2000, WinXP, WinVista/32, Windows Server 2003/32...), you'll never got problems with Atoms. They're part of the kernel of the operative system (in fact, as you can see, the API functions are in kernel32.dll library).
0
 
garyinmiami2003Author Commented:
RolandDeschain:

I would like to point out that I posted a followup question as I am having some trouble implementing
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now