using the clipboard for communicatign between programs
#1
a simple example of using the clipboard to communicate between programs.

This example requires three programs Clipmaster ,cliptalk1, and cliptalk2.
Compile all three and save them in the same directory to see how this works. 

I almost certainly lifted the idea from somewhere else but I can't recall where, sorry if I'm failing to give proper credit.

Clipmaster
Code: (Select All)
'Clipmaster
'clipboard communication sample
'
'CTA talk to all the cliptalk programs
'CT1 talk to cliptalk1
'CT2 talk to cliptalk2
'QUITALL ends all the programs

_Title "CLIPMaster"
Shell _DontWait "cliptalk1.exe /RUN"
Shell _DontWait "cliptalk2.exe /RUN"
Do
    Line Input "Enter some text to send to other program: ", text$
    If text$ = "QUITALL" Then Exit Do
    If UCase$(text$) = "CLEAR" Then _Clipboard$ = ""
    _Clipboard$ = text$
Loop
_Clipboard$ = "CTAQUITALL"

System

Cliptalk1
Code: (Select All)
'cliptalk1
Screen _NewImage(40, 20, 0)
_Title "CLIPTALK1"
Print "Reading text from clipboard."
Print " Esc key quits!"
MYID$ = "CT1"
Do: _Limit 100
    text$ = _Clipboard$ 'function returns clipboard contents
    If Len(text$) And text$ <> lasttext$ Then
        If text$ = "CTAQUITALL" Then GoTo QEXIT
        If Left$(UCase$(text$), 3) = "CTA" Then lasttext$ = text$
        If Left$(UCase$(text$), 3) = MYID$ Or Left$(UCase$(text$), 3) = "CTA" Then
            tt$ = Left$(UCase$(text$), 3)
            text$ = Right$(text$, Len(text$) - 3)
            Print text$
            If tt$ = MYID$ Then _Clipboard$ = "" 'clear clipboard after a read
        End If
    End If
Loop Until InKey$ = Chr$(27)
QEXIT:
System
End

cliptalk2
Code: (Select All)
'cliptalk2
Screen _NewImage(40, 20, 0)
_Title "CLIPTALK2"
Color 0, 15
Cls
Print "Reading text from clipboard."
Print " Esc key quits!"
MYID$ = "CT2"
Do: _Limit 100
    text$ = _Clipboard$ 'function returns clipboard contents
    If Len(text$) And text$ <> lasttext$ Then
        If Left$(UCase$(text$), 3) = "CTA" Then lasttext$ = text$
        If text$ = "CTAQUITALL" Then GoTo QEXIT
        If Left$(UCase$(text$), 3) = MYID$ Or Left$(UCase$(text$), 3) = "CTA" Then
            tt$ = Left$(UCase$(text$), 3)
            text$ = Right$(text$, Len(text$) - 3)
            Print text$
            If tt$ = MYID$ Then _Clipboard$ = "" 'clear clipboard after a read
        End If
    End If
Loop Until InKey$ = Chr$(27)
QEXIT:
System
End
Reply
#2
Funny, I posted my clipboard routine to communicate to another program here: https://staging.qb64phoenix.com/showthre...04#pid8104

I coded that for my buddy from Germany, so he could open up a second window and load a pic determined in the main program.

It's a nice technique if you want to avoid setting up a communications database.

There is a way to pipe contents using SHELL, too. I have that buried somewhere. I think maybe Spriggsy came up with that method at .rip.

Now I could be wrong, but I see no reason for /RUN in the SHELL statements. The SHELL call itself runs the program.

Fun stuff!

Pete
If eggs are brain food, Biden takes his scrambled.
Reply
#3
Oh that's good, I didn't even see that one because of the thread it was in. It is just such an easy method if you aren't too concerned about noise on the clipboard that could be thrown there by other programs. I have one somewhere in my directories where the program responds to a script sent to it from any text editing program because of the ease of reading the clipboard.


I'm no shell guru I thought /run was needed if you hadn't declared the executable on some sort of path but I could of course be wrong there.

EDIT: Just checked, yup doesn't need "/RUN"
Reply
#4
Sure beats using a common file to pass data back and forth between programs. I am wondering if I didn't use it for Tabulator, why?

Clipboard is great resource. I like how Steve used as an option for Input, that could save a ton on typing.
b = b + ...
Reply
#5
Microsoft discourages usage of the clipboard in this manner but if it works for your specific purpose then nice.
Ask me about Windows API and maybe some Linux stuff
Reply
#6
(10-23-2022, 08:36 PM)Spriggsy Wrote: Microsoft discourages usage of the clipboard in this manner but if it works for your specific purpose then nice.

Did they give a reason to go along with discouragement?
b = b + ...
Reply
#7
In the old days, we could just POKE what we wanted into memory. Now the professional way to do it is probably limited to using a database. I'm not surprised M$ doesn't endorse using the clipboard, because using other programs with copy will interfere with your QB64 app if it relies on constant communications with the clipboard.

Pete
If eggs are brain food, Biden takes his scrambled.
Reply
#8
Didn't there used to be a little spot in DOS memory called inter-program communication memory? It wasn't huge but it was likely not tampered with unless purposefully written into. Not a big deal in the before times. After windows however and there's all these different programs poking about.

As for discouraging the clipboard being used in this matter it makes sense as any running program can copy into it and and destroy "vital" inter-program communication. But... haven't we all used a host of programs copying and pasting as part of the normal workflow and never had a big issue with info getting overwritten?
Reply
#9
The official way that Microsoft recommends is to use TCP/IP. Which, luckily enough, is built-in with QB64. It's the same way that Fellippe does his InForm stuff. It shouldn't be hard for you to swap it in to your flow.
Ask me about Windows API and maybe some Linux stuff
Reply
#10
It looks like you're trying to make an open host connection. Can I help?

Well, Halloween is coming, so forgive me for channeling Clippy. On second though, douse me with Holly water, and call the fire dept.

Anyway, compile both programs. Call the first one whatever you want, but name the second one Pete2.bas. Save and compile it as Pete2.exe before running the first (host) app, which SHELLs to Pete2.exe.

Host - This is where you will make input choices.
Code: (Select All)
_SCREENMOVE 0, 0
WIDTH 60, 25
DO
    CLS
    DO UNTIL x
        x = _OPENCLIENT("TCP/IP:1234:localhost")
        IF x = 0 THEN
            x = _OPENHOST("TCP/IP:1234")
            a$ = "Opening as host."
        ELSE
            a$ = "Opening as client." ' Should not go here for this demo.
        END IF
        PRINT a$
    LOOP

    IF initiate = 0 THEN
        SHELL _HIDE "start pete2.exe" ' Open the client window.
        initiate = -1
    END IF

    ' Send data
    IF z = 0 THEN
        DO
            z = _OPENCONNECTION(x)
        LOOP UNTIL z
        PRINT "Connection established."
    END IF

    LOCATE 3, 1
    INPUT "Input a number for letter of the alphabet 1, 2, or 3: "; choice
    _KEYCLEAR

    PUT #z, , choice

    DO
        GET #z, , a
    LOOP UNTIL a = -1
    PUT #z, , a
    CLS
LOOP

Now the client window where your choices will be displayed...
Code: (Select All)
_SCREENMOVE 600, 0
WIDTH 50, 25
x = _OPENCLIENT("TCP/IP:1234:localhost")
PRINT "Opened as client.": PRINT
DO UNTIL x = 0 ' Prevents running if this app is opened without using host.
    DO
        _LIMIT 30
        GET #x, , receive
    LOOP UNTIL receive > 0

    PRINT "You chose the letter: ";

    SELECT CASE receive
        CASE 1
            PRINT "A"
        CASE 2
            PRINT "B"
        CASE 3
            PRINT "C"
        CASE ELSE
            PRINT "Wrong input!"
    END SELECT
    PRINT

    task_complete = -1
    PUT #x, , task_complete
LOOP

Pete
Reply




Users browsing this thread: 3 Guest(s)