home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
dde.zip
/
PM
/
PMDDE.C
< prev
next >
Wrap
Text File
|
1994-05-19
|
13KB
|
440 lines
/*----------------------------------------------------------------------------*/
/* PM DDE side */
/* (c) Larry Morley 1993, 1994 */
/* */
/* Intended for use with "WinDDE" - Demonstrates passing data using a private */
/* (binary) format between a PM application and a Win-OS/2 application. */
/* */
/* This code was constructed from a variety of sources. Trial and error, */
/* code written for OS/2 1.2 and 1.3, and some bits and pieces that I can't */
/* honestly remember. It took me a LONG time to get it to work correctly */
/* for transferring binary data blocks unaltered. I honestly don't remember */
/* where some pieces of this came from. They work, though, and are available */
/* for use. */
/*----------------------------------------------------------------------------*/
#define INCL_WIN
#define INCL_GPI
#define INCL_DOS
#include <os2.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "pmdde.h"
#include "dialog.h"
/*----------------------------------------------------------------------------*/
int main(void);
#define CF_LARRYS_PRIVATE_FMT 20
MRESULT EXPENTRY ClientWndProc(HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2);
MRESULT EXPENTRY dlgAbout (HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2);
MRESULT EXPENTRY dlgDDE (HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2);
PDDESTRUCT EXPENTRY MakeDDEDataSeg( /* Make A Memory Block For DDE */
USHORT usSegSize, /* Requested segment size */
USHORT usFormat, /* Format of data in the segment */
PSZ pszItemName, /* Size of DDE item name */
PVOID pvData, /* Pointer to data to encapsulate */
USHORT cbData, /* Size of above data */
USHORT fsStatus); /* Status flags */
VOID PrintToWindow(PSZ s);
/*----------------------------------------------------------------------------*/
CHAR szClientClass[] = "ClientWindow";
ULONG ulFrameFlags = FCF_TITLEBAR | FCF_SYSMENU |
FCF_SIZEBORDER | FCF_MINMAX |
FCF_SHELLPOSITION | FCF_TASKLIST |
FCF_MENU | FCF_ICON;
HAB hab;
FONTMETRICS fm;
int cyChar = 0;
int cyDesc = 0;
int cxChar = 0;
int cxCaps = 0;
int cxClient;
int cyClient;
HWND hwndFrame;
HWND hwndClient;
HWND hwndDDE;
HWND hwndMLE;
PSZ pDataBuffer;
/*----------------------------------------------------------------------------*/
/* Ordinary startup code */
/*----------------------------------------------------------------------------*/
int main(void)
{
HMQ hmq;
QMSG qmsg;
pDataBuffer = (PSZ)malloc(1024);
if (!pDataBuffer)
return 1;
hab = WinInitialize((USHORT)0);
hmq = WinCreateMsgQueue(hab,(SHORT)0);
WinRegisterClass(
hab,szClientClass,
(PFNWP)ClientWndProc,
CS_SIZEREDRAW,
(USHORT)0);
hwndFrame = WinCreateStdWindow(
HWND_DESKTOP,
WS_VISIBLE,
&ulFrameFlags,
szClientClass,
(PSZ)0,
(ULONG)0L,
(HMODULE)0,
(USHORT)ID_RESOURCE,
&hwndClient);
WinSetWindowText(hwndFrame,"DDE Sample: PM <--> Win-OS/2");
while (WinGetMsg(hab,&qmsg,(HWND)0,(USHORT)0,(USHORT)0))
WinDispatchMsg(hab,&qmsg);
WinDestroyWindow(hwndFrame);
WinDestroyMsgQueue(hmq);
WinTerminate(hab);
return 0;
}
/*----------------------------------------------------------------------------*/
/* Display info in the MLE on the DDE dialog. */
/*----------------------------------------------------------------------------*/
void PrintToWindow(PSZ s)
{
int len;
PSZ p;
if (hwndMLE)
{
len = WinQueryWindowTextLength(hwndMLE) + 1;
p = (PSZ)malloc(len+strlen(s)+16);
if(p)
{
WinQueryWindowText(hwndMLE,(LONG)(len+strlen(s)),p);
strcat(p,"\n");
strcat(p,s);
WinSetWindowText(hwndMLE,p);
free(p);
}
}
return;
}
/*----------------------------------------------------------------------------*/
/* Client area window proc */
/*----------------------------------------------------------------------------*/
MRESULT EXPENTRY ClientWndProc(HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2)
{
HPS hPS;
RECTL rcl;
PSZ s;
PDDESTRUCT pDdes;
switch (msg)
{
case WM_CREATE:
hPS = WinGetPS(hwnd);
GpiQueryFontMetrics(hPS,(LONG)sizeof(fm),&fm);
cyChar = (int)fm.lMaxBaselineExt;
cyDesc = (int)fm.lMaxDescender;
cxChar = (int)fm.lEmInc;
cxCaps = (int)fm.lAveCharWidth;
WinReleasePS(hPS);
break;
case WM_SIZE:
cxClient = SHORT1FROMMP(mp2);
cyClient = SHORT2FROMMP(mp2);
break;
/*-------------------------------------------*/
/* Another window is making a DDE request. */
/*-------------------------------------------*/
case WM_DDE_INITIATE:
/*-------------------------------------------*/
/* mp1 is hwnd of window making DDE request. */
/* */
/* mp2 points to a PDDEINIT structure */
/* USHORT cb */
/* PSZ pszAppName */
/* PSZ pszTopic */
/*-------------------------------------------*/
if (!(strcmpi(((PDDEINIT)mp2)->pszAppName,"LARRYS_APP")))
{
char szMsg[128];
sprintf(szMsg,"Received INITIATE hwnd=%8.8X",(HWND)mp1);
PrintToWindow(szMsg);
hwndDDE = (HWND)mp1;
WinDdeRespond(
hwndDDE,
hwnd,
"LARRYS_APP",
"ANY_TOPIC",
(PCONVCONTEXT)0);
}
break;
/*-------------------------------------------*/
/* Send some data */
/*-------------------------------------------*/
case WM_DDE_REQUEST:
{
s = "DDE response";
pDdes = MakeDDEDataSeg(
1024,
CF_TEXT,
"ANY_ITEM",
s,
strlen(s)+1,
0);
WinDdePostMsg((HWND)mp1,hwnd,WM_DDE_DATA,pDdes,TRUE);
PrintToWindow("Got request, sent response");
break;
}
/*-------------------------------------------*/
case WM_DDE_DATA:
case WM_DDE_POKE:
/*--------------------------------------------------*/
/* We're being sent data. These two cases could be */
/* handled separately; POKE is unsolicited, DATA is */
/* solicited. */
/* */
/* In both cases, the actual data starts at: */
/* DDES_PABDATA[0], of PDDESTRUCT mp2. */
/*--------------------------------------------------*/
PrintToWindow(DDES_PABDATA((PDDESTRUCT)mp2));
break;
/*-------------------------------------------*/
case WM_PAINT:
hPS = WinBeginPaint(hwnd,(HPS)0,&rcl);
GpiErase(hPS);
WinEndPaint(hPS);
break;
case WM_COMMAND:
switch (SHORT1FROMMP(mp1))
{
case IDM_FILE_ABOUT:
WinDlgBox(
HWND_DESKTOP,
hwnd,
(PFNWP)dlgAbout,
(HMODULE)0,
DLG_ABOUT,
(PVOID)0);
return (MRESULT) 0;
case IDM_FILE_EXIT:
WinPostMsg(hwnd,WM_CLOSE,(MPARAM)0,(MPARAM)0);
return (MRESULT) 0;
case IDM_FILE_DDE:
WinDlgBox(
HWND_DESKTOP,
hwnd,
(PFNWP)dlgDDE,
(HMODULE)0,
DLG_DDE,
(PVOID)0);
return (MRESULT) 0;
}
break;
default:
return WinDefWindowProc(hwnd,msg,mp1,mp2);
}
return (MRESULT) FALSE;
}
/*----------------------------------------------------------------------------*/
MRESULT EXPENTRY dlgAbout(HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2)
{
switch (msg)
{
case WM_COMMAND:
switch (SHORT1FROMMP(mp1))
{
case DID_OK:
WinDismissDlg(hwnd,TRUE);
return (MRESULT) 0;
}
}
return WinDefDlgProc(hwnd,msg,mp1,mp2);
}
/*----------------------------------------------------------------------------*/
MRESULT EXPENTRY dlgDDE(HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2)
{
PDDESTRUCT pDDE;
switch (msg)
{
case WM_INITDLG:
hwndMLE = WinWindowFromID(hwnd,EF_REPLY);
break;
case WM_COMMAND:
switch (SHORT1FROMMP(mp1))
{
case PB_TRANSACT:
{
WinQueryDlgItemText(hwnd,EF_SEND,128,pDataBuffer);
if (pDataBuffer[0])
{
pDDE = MakeDDEDataSeg(
sizeof(pDataBuffer),
CF_LARRYS_PRIVATE_FMT,
"ANY_ITEM",
(PVOID)pDataBuffer,
sizeof(pDataBuffer),
0);
WinDdePostMsg(hwndDDE,hwndClient,WM_DDE_DATA,pDDE,TRUE);
PrintToWindow("Message sent...");
}
return (MRESULT)0;
}
case DID_CANCEL:
hwndMLE = (HWND)0;
WinDismissDlg(hwnd,TRUE);
return (MRESULT) 0;
}
}
return WinDefDlgProc(hwnd,msg,mp1,mp2);
}
/*----------------------------------------------------------------------------*/
PDDESTRUCT EXPENTRY MakeDDEDataSeg(USHORT usSegSize,
USHORT usFormat,
PSZ pszItemName,
PVOID pvData,
USHORT cbData,
USHORT fsStatus)
{
PDDESTRUCT pdde = NULL; // ptr to dde data structure
ULONG rc = 0; // api return code holder
PULONG pulSharedObj = (PULONG)0; // pointer to shared object
PCHAR psz = NULL; // string ptr for text format
USHORT cbObjSize = 0; // count of bytes to request
/*------------------------------------------------------------------------*/
/* Allocate a givable memory block */
/*------------------------------------------------------------------------*/
cbObjSize = (USHORT)(strlen(pszItemName) + 1);
rc = DosAllocSharedMem(
((PPVOID)&pulSharedObj),
(PSZ)0,
cbObjSize,
PAG_COMMIT | PAG_READ | PAG_WRITE | OBJ_GIVEABLE);
if (rc && pulSharedObj) // check api return code and ptr
return (PDDESTRUCT)0;
/*------------------------------------------------------------------------*/
/* Fill in the new DDE structure */
/*------------------------------------------------------------------------*/
pdde = (PDDESTRUCT) pulSharedObj;
pdde->usFormat = usFormat;
pdde->offszItemName = (USHORT)sizeof(DDESTRUCT);
strcpy(DDES_PSZITEMNAME(pdde), pszItemName);
pdde->cbData = cbData;
pdde->offabData = (USHORT) pdde->offszItemName +
(USHORT)strlen(pszItemName) + (USHORT)1;
pdde->fsStatus = fsStatus;
if (usFormat == CF_TEXT)
{
if (pvData == NULL || cbData == 0)
psz = '\0';
else
psz = pvData;
strcpy(DDES_PABDATA(pdde), psz);
}
else
{
if (pvData != (VOID *)0 || cbData != 0)
memcpy(DDES_PABDATA(pdde), pvData, (size_t)cbData);
}
return pdde;
}
/*----------------------------------------------------------------------------*/