Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

wm_CopyData example

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • wm_CopyData example

    WM_COPYDATA demo 2004/01/17
    PB/Win 7.02
    This program will run 4 instances of itself to show WM_COPYDATA in action.
    Have fun...

    Thank to Semen Matusovski, Don Dickinson, Tom Hanlin and Eric von Bostel.

    Pierre

    Code:
    #COMPILE EXE "wmCopyData.exe"  'If you change the exe name then change it also in PBMAIN
    #DIM ALL
    #REGISTER NONE
    #INCLUDE "Win32Api.Inc" '2003-10-27
     
    %Listbox     = 100
    %ButtonSend  = 101
    %ButtonClear = 102
    %ButtonExit  = 103
     
    GLOBAL ProcessCount AS LONG
    GLOBAL AppTitle     AS STRING
    '_________________________________________________________________________________________________
     
    CALLBACK FUNCTION DlgProc
     STATIC pDataToGet AS COPYDATASTRUCT POINTER
     STATIC DataToSend AS COPYDATASTRUCT
     STATIC pzBuffer   AS ASCIIZ PTR
     STATIC zBuffer    AS STRING * 50
     LOCAL  Buffer     AS STRING
     LOCAL  hTest      AS DWORD
     LOCAL  Counter    AS LONG
     
     SELECT CASE CBMSG
       CASE %WM_INITDIALOG
         PostMessage CBHNDL, %WM_USER + 400, 0, 0 'Execute after the dialog is drawn
     
       CASE %WM_USER + 400
         SLEEP 150 * ProcessCount                             'Give time to other instance to start
         FOR Counter = 1 TO ProcessCount                      'Find other wmCopyData dialog handle
           Buffer = "CopyData - " & FORMAT$(Counter, "00")    'Look for wmCopyData - 01, 02, 03 and 04
           hTest = FindWindow(BYVAL %NULL, BYCOPY Buffer)     'Do we have a handle
           IF (hTest <> CBHNDL) AND (hTest <> 0) THEN         'Yes
             REDIM PRESERVE hArray(1 TO Counter) AS STATIC DWORD
             hArray(Counter) = hTest                          'Keep handle in hArray
           END IF
         NEXT
     
       CASE %WM_COPYDATA                       'We are receiving data
         hTest = CBWPARAM                      'Handle of sending window
         pDataToGet = CBLPARAM                 'Pointer is in CBLPARAM
         pzBuffer   = @pDataToGet.lpData       'Pointer to data string
         LISTBOX ADD CBHNDL, %Listbox, "Hndl=" & HEX$(hTest, 4) & _
                                       ", dwData=" & FORMAT$(@pDataToGet.dwData) & _
                                       ", " & @pzBuffer
         IF @pDataToGet.dwData = 321 THEN PostMessage CbHndl, %WM_SYSCOMMAND, %SC_CLOSE, 0 'End the program
         FUNCTION = 1
     
      CASE %WM_CLOSE
        DataToSend.dwData = 321
        FOR Counter = 1 TO UBOUND(hArray)
          'Ask other instance to terminate...
          IF IsWindow(hArray(Counter)) THEN SendMessage hArray(Counter), %WM_COPYDATA, CBHNDL, VARPTR(DataToSend)
        NEXT
     
      CASE %WM_COMMAND
        SELECT CASE LOWRD(CBWPARAM)
     
          CASE %ButtonSend
            IF CBCTLMSG = %BN_CLICKED THEN
              zBuffer            = AppTitle
              DataToSend.lpData  = VARPTR(zBuffer)
              DataToSend.cbdata  = LEN(zBuffer) + 1
              DataToSend.dwData  = 123
              FOR Counter = 1 TO UBOUND(hArray)
                SendMessage hArray(Counter), %WM_COPYDATA, CBHNDL, VARPTR(DataToSend) 'Send data to other instance
              NEXT
            END IF
     
          CASE %ButtonClear
            IF CBCTLMSG = %BN_CLICKED THEN LISTBOX RESET CBHNDL, %Listbox
     
          CASE %ButtonExit
            IF CBCTLMSG = %BN_CLICKED THEN DIALOG END CBHNDL, 0
     
        END SELECT
     
     END SELECT
     
    END FUNCTION
    '_________________________________________________________________________________________________
     
    FUNCTION PBMAIN
     LOCAL hDlg          AS DWORD
     LOCAL ProcessNumber AS LONG
     LOCAL Retval        AS LONG
     
     ProcessCount  = 4
     ProcessNumber = 1
     AppTitle  = "CopyData - 01"
     DO 'Change dialog caption if already used
       SLEEP 10
       IF FindWindow(BYVAL %NULL, BYCOPY AppTitle) THEN
         ProcessNumber = VAL(RIGHT$(AppTitle, 2)) + 1
         IF ProcessNumber > ProcessCount THEN EXIT FUNCTION
         AppTitle = "CopyData - " & FORMAT$(ProcessNumber, "00")
       ELSE
         EXIT DO
       END IF
     LOOP
     IF ProcessNumber < ProcessCount THEN
       'This application will start another copy of itself until ProcessCount is reached
       Retval = SHELL("wmCopyData.exe") 'If you change the exe name then change it also at the #COMPILE EXE line.
     END IF
     
     DIALOG NEW 0, AppTitle, 10 + (ProcessNumber - 1) * 170, 10, 160, 210, _
      %WS_POPUP OR %DS_MODALFRAME OR %WS_CAPTION OR %WS_MINIMIZEBOX OR %WS_SYSMENU, _
      0 TO hDlg
     
      CONTROL ADD LISTBOX, hdlg, %Listbox, , 5, 5, 150, 190, _
      %WS_CHILD OR %WS_VISIBLE  OR %WS_VSCROLL OR %WS_TABSTOP, 0
     
      CONTROL ADD BUTTON, hDlg, %ButtonSend, "SEND", 20, 192, 35, 15, _
        %WS_CHILD OR %WS_VISIBLE OR %BS_PUSHBUTTON OR %WS_TABSTOP
     
      CONTROL ADD BUTTON, hdlg, %ButtonClear, "CLEAR", 60, 192, 35, 15, _
        %WS_CHILD OR %WS_VISIBLE OR %BS_PUSHBUTTON OR %WS_TABSTOP
     
      CONTROL ADD BUTTON, hDlg, %ButtonExit, "EXIT", 100, 192, 35, 15, _
        %WS_CHILD OR %WS_VISIBLE OR %BS_PUSHBUTTON OR %WS_TABSTOP
     
      DIALOG SHOW MODAL hDlg CALL DlgProc
     
    END FUNCTION
    '_________________________________________________________________________________________________
    ------------------
    Pierre



    [This message has been edited by Pierre Bellisle (edited January 18, 2004).]

  • #2
    I get this error on compile, possibly a different version of win32api.inc?


    Code:
    ---------------------------
    PowerBASIC Compiler for Windows
    ---------------------------
    PowerBASIC Compiler for Windows
    
    PB/Win  Version 7.0  
    
    Copyright (c) 1996-2002 PowerBasic Inc.
    
    Carmel, California, USA
    
    All Rights Reserved
    
    Error 480 in D:\PBasic\Code\copydata.bas(53:10):  Parameter mismatch, may need ByCopy...
    
    Line 53:       If IsWindow hArray(Counter) Then SendMessage hArray(Counter), %WM_COPYDATA, CbHndl, VarPtr(DataToSend)
    
    
    ---------------------------
    OK   
    ---------------------------

    ------------------
    Paul Dwyer
    Network Engineer
    Aussie in Tokyo

    [This message has been edited by Paul Dwyer (edited January 18, 2004).]

    Comment


    • #3



      Pierre


      Thanks for a great example
      It works fine on win XP pro


      Steve


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

      Comment


      • #4
        Paul,
        it's more like different compiler version, 7.00 <> 7.02,
        7.02 is more flexible in the way parenthesis are handled,
        code is now updated so it will compile fine
        with 6.10, 6.11, 7.00, 7.01 and 7.02 version.

        Changed
        IF IsWindow hArray(Counter) THEN ...
        to
        IF IsWindow(hArray(Counter)) THEN ...


        Steve,
        I tried the code only on 98se,
        so I'm glad to know that it work under XP.


        ------------------
        Pierre

        [This message has been edited by Pierre Bellisle (edited January 18, 2004).]

        Comment


        • #5
          >7.02 is more flexible in the way parenthesis are handled

          If you mean the fact that parens are not required to delimit function arguments when making a direct assignment of a return value...i.e.,

          Code:
           Result = functionname param [,param]...
          .. rather than the documented requirement for same, i.e.
          Code:
           Result = functionname [b]([/b] param [,param]...[b])[/b]
          .. then what we have here is a case of actual behavior not the same as documented behavior: a bug.

          i.e., either the newly permitted syntax (no parens required) - if it is in fact, now supported - should be documented, or the compile should fail on "( expected".

          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment

          Working...
          X