
'====================================================================
'' Data copy demo, modal dialog as main.
''
'' This app uses the WM_COPYDATA message to transfer data between
'' two processes (actually two separate instances of the same app).
'' I did not try to determine the maximum number of bytes that can
'' be transferred using this method, but I did determine that under
'' Windows 2000, 100000 bytes can be transferred without problems.
'====================================================================

#include "dialogs.bas"

#define TESTDATA  1
#define BUFFSIZE  100000

Dim Shared As Integer instance2

'====================================================================

Function DialogProc( Byval hDlg As  HWND, _
                     Byval uMsg As UINT, _
                     Byval wParam As WPARAM, _
                     Byval lParam As LPARAM ) As Integer

  Static As HWND hWndOtherInstance, hWndEdit, hWndStatic
  Static As COPYDATASTRUCT cds
  Static As COPYDATASTRUCT Ptr pcds
  Static As Byte Ptr pBuff

  Select Case uMsg

    Case WM_INITDIALOG

      pBuff = allocate( BUFFSIZE )

      hWndEdit = GetDlgItem( hDlg, 101 )
      hWndStatic = GetDlgItem( hDlg, 103 )

      SendMessage( hWndEdit, EM_SETLIMITTEXT, BUFFSIZE, 0 )

    Case WM_COMMAND

      If hiword(wParam) = BN_CLICKED And loword(wParam) = 102 Then

        '' Get the window handle of the other instance.
        ''
        If instance2 Then
          hWndOtherInstance = FindWindow( "#32770", "Data Copy 1" )
        Else
          hWndOtherInstance = FindWindow( "#32770", "Data Copy 2" )
        End If

        '' Ensure that the other instance is running.
        ''
        If hWndOtherInstance Then

          '' The length must be incremented so the null
          '' terminator will be sent with the data
          '' (necessary only because the WM_COPYDATA
          '' handler expects a null-terminated string).
          ''
          cds.cbData = GetWindowTextLength( hWndEdit ) + 1

          If cds.cbData > 1 And cds.cbData <= BUFFSIZE Then

            GetWindowText( hWndEdit, pBuff, BUFFSIZE )

            cds.dwData = TESTDATA
            cds.lpData = pBuff

            SendMessage( hWndOtherInstance, _
                         WM_COPYDATA, _
                         cast(wParam,hDlg), _
                         cast(lParam,@cds) )

            SetWindowText( hWndStatic, ">> " & Str(cds.cbData) )

          End If

        Else

          SetWindowText( hWndStatic, Space(21) )

        End If
      End If

    Case WM_COPYDATA

      pcds = cast(COPYDATASTRUCT Ptr,lParam)
      If pcds->dwData = TESTDATA Then
        SetWindowText( hWndEdit, pcds->lpData )
        SetWindowText( hWndStatic, "<< " & Str(pcds->cbData) )
      End If

    Case WM_CLOSE

      deallocate( pBuff )

      EndDialog( hDlg, null )

  End Select

  Return 0

End Function

'====================================================================

Dim As LPDLGTEMPLATE lpdt

'' The first and second instances need to have different names.
'' #32770 is the system-defined class for dialog boxes.

If FindWindow( "#32770", "Data Copy 2" ) = 0 Then
  If FindWindow( "#32770", "Data Copy 1" ) Then

    instance2 = true

    Dialog( 3, 180, 10, 160, 120, "Data Copy 2", lpdt, _
            WS_OVERLAPPED Or WS_SYSMENU )

  Else

    Dialog( 3, 10, 10, 160, 120, "Data Copy 1", lpdt, _
            WS_OVERLAPPED Or WS_SYSMENU )

  End If

  EditText( 101, 4, 4, 150, 80, "", WS_BORDER Or _
            ES_MULTILINE Or ES_WANTRETURN Or _
            WS_VSCROLL Or WS_HSCROLL )

  PushButton( 102, -1, 90, 30, 12, "Send" )

  Ltext( 103, 120, 92, 40, 12, "" )

  CreateModalDialog( 0, @DialogProc, 0, lpdt )

End If

'====================================================================
 