Results 1 to 16 of 16

Thread: Sharing data between 2 vb programs

  1. #1

    Thread Starter
    New Member danisa's Avatar
    Join Date
    Aug 2008
    Posts
    13

    Question Sharing data between 2 vb programs

    Hi everyone,
    I need help on this one...
    I have two vb programs I've wrote reading data from devices attached to separate com ports.
    Prog 1 reads data A from com 1 & displays it
    Prog 2 reads data B from com 2 & displays it
    I need to make Prog 2 to be able to access some of the data read from Prog 1 in real time without accesing com 1 directly (as it is open already from Prog 1). Since the com port 1 data is processed already in Prog 1, I only need to share/import the end string results.
    The data is not complex just strings mostly.
    i.e Prog2 reads data A + some strings from data B

    This one beats me hands down...If someone has an idea, please help.





    Another thing, off this topic may be, I dont know where to post it:
    I tryied 10+ times to register with no avail.
    My LCD at work just packed up and the colors are all f@ck8.
    I did typed "blue" at the no spam text box, but it kept coming back...
    I got home & "blue" worked first time!? don't know why...
    May be the admin should put a oicture or something else that is easier identified by color impared people. Like "describe what is depicted on the picture": horse :-))
    ADMIN - Sorry about this post, feel free to move it where it should be.

    I hope this doesn't sidetrack us from the original post above...
    Thanks
    D

  2. #2
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Sharing data between 2 vb programs

    may be you can use a common public file to store the values you want to access from other program...may be you can generate a txt file and write your values from com1 and from com2 access it.

    or store those values in a database file and access it from both the vb programs.
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  3. #3

    Thread Starter
    New Member danisa's Avatar
    Join Date
    Aug 2008
    Posts
    13

    Question Re: Sharing data between 2 vb programs

    Hi gahes,

    I've tought of writing the data values to a file and then let Prog2 read it from there. But, it will be slow...also it will create constant HDD read/write and possibly slow down the system.

    I need to do this in real time or as fast as possible.
    It's not huge amount of data...only couple of strings...but they are refreshed every 1/4 sec.

    Any way to do it in memory?

    Thanks for the replay.
    D

  4. #4
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Sharing data between 2 vb programs

    Im not sure whether it will be a good practice todo like this...anyway just give a try to using environ variables; set the value from one vb prog and access it from the other...you can search lots of code examples on environ variables....

    http://www.vbforums.com/showthread.php?t=421661
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  5. #5
    Addicted Member Veritas2.0's Avatar
    Join Date
    May 2008
    Posts
    181

    Re: Sharing data between 2 vb programs

    What about using a couple of winsock controls that loop back to the same computer to send the data back and forth?
    Simple little bugs 13 : Me 1

    Law of Bugs - That one bug you missed will be found almost immediately, by your customer.

    I wonder if anyone has ever asked for a User Surly interface?

  6. #6
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Sharing data between 2 vb programs

    Since these two programs share data could you just merge them into one app? It would be, by far, the simplest solution.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  7. #7
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Sharing data between 2 vb programs

    Lots of options: Named Pipes, Mailslots, Window messages, or a parent/child relationship and anonymous pipes.

    Mailslots are handy, and don't eat up TCP or UDP ports. See demo (attached).

    Compile, run two copies. Enjoy.
    Attached Files Attached Files

  8. #8

    Thread Starter
    New Member danisa's Avatar
    Join Date
    Aug 2008
    Posts
    13

    Re: Sharing data between 2 vb programs

    Thanks guys...

    CDDRIVE....I know that, but can't be done. the two progs have to be stand alone...

    diletante...Thanks man... this works...well, I'll play with the idea and see how it goes. I don't fully understand how it works tough...

    In the meanwhile if someone comes up with other ideas let me know...just see if you can actually give some code examples with some explanation of how it works.

    This is new for me...so i need some tut's
    Thanks again everyone
    D

  9. #9
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    Pakistan
    Posts
    388

    Re: Sharing data between 2 vb programs

    There is another option available too. But you will have to sacrifice something for that.
    Use the clipboard to communicate.
    For the sending application:
    Code:
    Dim data As String
    data = "This is information"
    Clipboard.Clear
    Clipboard.SetText data
    For the receiving part:
    Code:
    'in a timer of course
    Dim data As String
    Clipboard.GetText data
    If data<>"" Then
       'something was sent by the sending application
       Clipboard.Clear
    Else
       'nothing sent by the other application
    End If
    Well, many people might cry out at this. For sacrificing the windows clipboard for that but for me it doesn't matter while the purpose is being served rightfully and easily.

  10. #10

    Thread Starter
    New Member danisa's Avatar
    Join Date
    Aug 2008
    Posts
    13

    Question Re: Sharing data between 2 vb programs

    Hi lone_REBEL
    I tought of that, but since the communication between the two progs can run for hours...what is someone or another prog try to use the clipboard?
    May be if I can make a "private" Clipboard...

    Wait...I just realised I haven't described what I need fully. There are 3 programs involved here. sorry about that

    Need:
    Prog1 sends/broadcasts the string values to Prog2 and Prog3.
    It doesn't need to receive an answer (well...may be for debug only)

    Flow:
    Prog1 reads from the com1 device.
    Prog2 reads from com2 device, and the strings from Prog1
    Prog3 reads from com3 device, and the strings from Prog1
    Prog2 and Prog3 don't communicate between each other.
    Prog2 and Prog3 may or may not be initialised when Prog1 is started and may not be sinchronised.

    The MailSlot (link above) works ok, but I need to be able to have 2 servers in Prog1, to satisfy the above flow. (i think...)

    How do I do it dilletante?..( is it possible at all)


    Thanks everyone
    D

  11. #11
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Sharing data between 2 vb programs

    Quote Originally Posted by danisa
    I don't fully understand how it works tough...
    Not too surprising, most people don't realize some of these things even exist. Before we had TCP/IP everywhere named Pipes and Mailslots were the closest thing Microsoft had. They are part of Windows Networking, along with file and printer sharing, RPC, DCOM, and other things that don't (necessarily) need TCP/IP.

    The docs say:
    A mailslot is a pseudofile that resides in memory, and you use standard file functions to access it. The data in a mailslot message can be in any form, but cannot be larger than 424 bytes when sent between computers. Unlike disk files, mailslots are temporary. When all handles to a mailslot are closed, the mailslot and all the data it contains are deleted.

    A mailslot server is a process that creates and owns a mailslot. When the server creates a mailslot, it receives a mailslot handle. This handle must be used when a process reads messages from the mailslot. Only the process that creates a mailslot or has obtained the handle by some other mechanism (such as inheritance) can read from the mailslot. All mailslots are local to the process that creates them. A process cannot create a remote mailslot.

    A mailslot client is a process that writes a message to a mailslot. Any process that has the name of a mailslot can put a message there. New messages follow any existing messages in the mailslot.

    Mailslots can broadcast messages within a domain. If several processes in a domain each create a mailslot using the same name, every message that is addressed to that mailslot and sent to the domain is received by the participating processes. Because one process can control both a server mailslot handle and the client handle retrieved when the mailslot is opened for a write operation, applications can easily implement a simple message-passing facility within a domain.

    To send messages that are larger than 424 bytes between computers, use named pipes or Windows Sockets instead.
    About Mailslots

    Note that within one computer mailslots can pass more than 424 byte messages, but both 425 and 426 byte messages are problematic and may be dropped. An odd quirk, I admit, but you work around such things. In simple cases you can check the length before sending and pad with vbNullChar as needed, or use a more complex scheme. This limitation is an accident of history.


    In my sample I used a UserControl that I wrote to make simple Mailslot usage look a little bit like using a Winsock control. SimpleMailslot.ctl can be dropped into a VB6 project folder along with its .ctx fle and just added to the project (menu: Project|Add file...).

    As written, each instance of SimpleMailslot provides one Mailslot server to listen on, and 0 to N Mailslot clients for sending to other Mailslots.

    When a process creates a mailslot, the mailslot name must have the following form.

    \\.\mailslot\[path\]name

    A mailslot name requires the following elements: two backslashes to begin the name, a period, a backslash following the period, the word "mailslot", and a trailing backslash. Names are not case sensitive. A mailslot name can be preceded by a path consisting of the names of one or more directories, separated by backslashes. For example, if a user expects messages on the subject of taxes from Bob, Pete, and Sue, the user's mailslot application might allow the user to create individual mailslots for each sender, as shown here:


    \\.\mailslot\taxes\bobs_comments
    \\.\mailslot\taxes\petes_comments
    \\.\mailslot\taxes\sues_comments
    To put a message into a mailslot, a process opens a mailslot by name. To write to a mailslot on the local computer, a process can use a mailslot name that has the same form used for creating a mailslot. This situation, however, is relatively uncommon. More frequently, you would use the following form to write to a mailslot on a specific remote computer:

    \\ComputerName\mailslot\[path\]name

    To put a message into every mailslot in the specified domain with a given name, use the following form:

    \\DomainName\mailslot\[path\]name

    To put a message into every mailslot with a given name in the system's primary domain, use the following form:

    \\*\mailslot\[path\]name
    Mailslot Names

    In many ways using Mailslots is like using UDP datagrams via the Winsock control.

    Because Win9x can't do overlapped (async) I/O except in very limited cases, I made SimpleMailslot so it polls for received messages instead. A version that does not have to poll but works only on NT-family OSs (NT4.0, Win2K and later) could be created instead.

    Since a Mailslot only works in one direction (client to server) I wrote the example so that it creates a Mailslot for itself to listen on, and sends to the other program via the second Mailslot.

    To get these two names the program first tries to create:

    \\.\mailslot\mailslotdemo\0

    If it succeeds, it assumes it started first and that the other program will use:

    \\.\mailslot\mailslotdemo\1

    Otherwise it swaps the values, assuming it is the second copy started.


    SOME USES

    You can use Mailslots to make a LAN P2P chat or IM system that doesn't require a server.

    I have used Mailslots with a database program to broadcast alerts to all copies of the program when some important table is changed by anyone. The alert tells each copy of the program to refresh its copy of the data so everyone sees the same items in a Listbox or grid control, whether new ones were added or old ones deleted.

    The nice thing with mailslots is you don't have to pick a UDP port number to use like you would with Winsock. Instead you have Mailslot names, which offer a lot more than the 65,535 valid values you have for UDP ports - many of which are already allocated.
    Last edited by dilettante; Aug 21st, 2008 at 09:06 AM.

  12. #12
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Sharing data between 2 vb programs

    diletante, this is interesting stuff! Thanks for posting it.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  13. #13
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Sharing data between 2 vb programs

    dilettante, just thought I'd mention that I did a disk search in my MSDN files and found nothing with MailSlot key word. How the heck did you find this?
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  14. #14

    Thread Starter
    New Member danisa's Avatar
    Join Date
    Aug 2008
    Posts
    13

    Question Re: Sharing data between 2 vb programs

    GREAT POST:
    Thanks Dilettante!

    A mailslot is a pseudofile that resides in memory, and you use standard file functions to access it. The data in a mailslot message can be in any form, but cannot be larger than 424 bytes when sent between computers. Unlike disk files, mailslots are temporary. When all handles to a mailslot are closed, the mailslot and all the data it contains are deleted.
    This one is a winner. I'm getting excited now

    1: Temp file in memory...
    2: Prog1 sends data to Prog2...
    3: Prog1 sends data to Prog3...

    Dilettante, correct me if i'm wrong.
    Flow:
    Prog1 reads from the com1 device.
    Prog2 reads from com2 device, and the strings from Prog1
    Prog3 reads from com3 device, and the strings from Prog1
    Prog2 and Prog3 don't communicate between each other.
    Prog2 and Prog3 may or may not be initialised when Prog1 is started and may not be sinchronised.

    To comply with the "Flow"
    The server only listens/receives.
    I need to have a separate Server in Prog2 and Prog3 (different mailslot names needed?? )
    And then Prog1 should also have 2 mailslots for each server? ?

    Now I'm getting lost a bit...



    BTW what is the .ctx for (the icon of the control?)

    Thanks again for the wonderfull help.

  15. #15
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    Pakistan
    Posts
    388

    Re: Sharing data between 2 vb programs

    @Denisa

    You can still use the clipboard to communicate to communicate between three programs too. All that you need to devise is a "communication" format for the programs. This might be like this:
    If data in the clipboard begins with Chr(201) it means the data was sent for prog1.
    If data in the clipboard begins with Chr(202) it means it was sent for prog2.
    If data in the clipboard begins with Chr(203) it means it was sent for prog3.
    Now when prog1 reads the data from the clipboard and it begins with Chr(201). It would neglect the control character (201, in this case). If it reads another control character on the second byte (lets assume 202), it will also neglect this control character and read the data from character 3 e.g. Mid(data,3). But if there is no control character present at byte 2, it will read the data from byte 2 e.g. Mid(data,2). Now after reading the data, it will clear the control byte for itself, so that it does not re-read the same data again.
    The same applies to prog2 and prog3.
    If you are still confused how to manipulate it, just PM me and I will code a simple application for you that will communicate data between its threads.

  16. #16

    Thread Starter
    New Member danisa's Avatar
    Join Date
    Aug 2008
    Posts
    13

    Question Re: Sharing data between 2 vb programs

    Hi lone_REBEL,
    Won't I lose (or gain) "data" if another prog or person try to use the Clipboard? I've stayed away from clpbrd untill now so don't truly know the cons of using it.
    Thanks for posting the sample.

    @dilettante:
    It looks like my problem is that you can't have two Mailslots servers running on the same PC. (one mailslot server and plenty mailshot clients =OK though)
    Is this correct or not?
    I've found this on the net:
    http://www.codeproject.com/KB/thread...dMailslot.aspx
    It basically says 2 servers can be ran on the same PC but the sample is in C+ and I don't know it
    Isn't there a way one program to bradcast data to 2 other programs on the same PC?

    Thanks
    D
    p.s. Forgot to mention the SimpleMailsot work ok on my PC at work, but got home (same OS,etc.) and it did't work.
    Comes up with "Write failed" altough "send" appears in the textbox below.
    I've extracted the files fresh, but same thing... What am I missing? (MS messenger service?)
    please reply
    Last edited by danisa; Aug 22nd, 2008 at 03:50 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width