home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 v2.4 Fix / W95-v2.4fix.iso / ACADWIN / ADS / WIN / DDETEST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  9.9 KB  |  296 lines

  1. /*
  2.  
  3.     DdeTest
  4.  
  5.     A DDE Test Program by Autodesk, Inc. (1991).
  6.  
  7.     This is to be used to test sending and receiving data from another
  8.     Windows application via DDE.
  9. */
  10.  
  11. /*
  12. See "Dde.DOC".
  13. */
  14.  
  15.  
  16.  
  17. #include "options.h"
  18.  
  19. #include <stdio.h>
  20. #include <math.h>
  21.  
  22. #include "adslib.h"    
  23.  
  24. #include <process.h>
  25. #include <string.h>
  26. #include <memory.h>
  27. #include <dos.h>
  28. #include <ctype.h>
  29.  
  30. #include "winutil.h"
  31. #include "ddewin.h"
  32. #include "ddeconv.h"
  33. #include "spreadsh.h"
  34. #include "ddetest.h"
  35.  
  36.  
  37.  
  38.  
  39. DDEDATA *pDdeData = NULL;
  40. PDDE pdde = NULL;
  41. static char progname[] = "DdeTest";
  42.  
  43. #ifdef EXCEL
  44. static char ddeapp[] = "Excel";       /* Defaults */
  45. static char ddetopic[] = "Sheet1";
  46. #else
  47. static char ddeapp[] = "123w";       /* Defaults */
  48. static char ddetopic[] = "Untitled";
  49. #endif
  50.  
  51. int row = 1;
  52. char item[25];
  53. char datastr1[256] = 
  54. "Testing DDE Poke strings that are large enough to fill up 80 characters......";
  55. #define COL1  1
  56. #define NCOLS 3
  57. char datastr[101] = 
  58. "1.11111\t2.22222\t3.33333\r\n" \
  59. "4.44444\t5.55555\t6.66666\r\n" \
  60. "7.77777\t8.88888\t9.99999\r\n" \
  61. "11.1111\t22.2222\t33.3333\r\n"; 
  62. #define DATASTRLEN (sizeof(datastr)-1)
  63. char databuf[4096];
  64. int datalen, rows;
  65.  
  66. char execstr[] = 
  67. "[SELECT(\"R1C1\")][FORMULA(\"AutoCAD Graphics Data\")]";
  68.  
  69. HANDLE hInst;                       /* current instance                      */
  70.  
  71.  
  72. /* All Function Prototypes for ddetest.c */
  73.  
  74. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, 
  75.                                         LPSTR lpCmdLine, int nCmdShow);
  76. BOOL InitApplication(HANDLE hInstance);
  77. BOOL InitInstance(HANDLE hInstance, int nCmdShow);
  78. long FAR PASCAL MainWndProc(HWND hWnd, UINT message, WPARAM wParam, 
  79.         LPARAM lParam);
  80. BOOL FAR PASCAL About(HWND hDlg, UINT message, WPARAM wParam, 
  81.         LPARAM lParam);
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, 
  89.                                         LPSTR lpCmdLine, int nCmdShow)
  90. {
  91.     MSG msg;                                 /* message                      */
  92.  
  93.     if (!hPrevInstance)                  /* Other instances of app running? */
  94.         if (!InitApplication(hInstance)) /* Initialize shared things */
  95.             return (FALSE);              /* Exits if unable to initialize     */
  96.  
  97.     /* Perform initializations that apply to a specific instance */
  98.  
  99.     if (!InitInstance(hInstance, nCmdShow))
  100.         return (FALSE);
  101.  
  102.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  103.  
  104.     while (GetMessage(&msg, NULL, NULL, NULL)) {
  105.         TranslateMessage(&msg);    /* Translates virtual key codes           */
  106.         DispatchMessage(&msg);     /* Dispatches message to window           */
  107.     }
  108.     return (msg.wParam);           /* Returns the value from PostQuitMessage */
  109. }
  110.  
  111.  
  112.  
  113. BOOL InitApplication(hInstance)
  114. HANDLE hInstance;                              /* current instance           */
  115. {
  116.     WNDCLASS  wc;
  117.  
  118.     /* Fill in window class structure with parameters that describe the       */
  119.     /* main window.                                                           */
  120.  
  121.     wc.style = NULL;                    /* Class style(s).                    */
  122.     wc.lpfnWndProc = MainWndProc;       /* Function to retrieve messages for  */
  123.                                         /* windows of this class.             */
  124.     wc.cbClsExtra = 0;                  /* No per-class extra data.           */
  125.     wc.cbWndExtra = 0;                  /* No per-window extra data.          */
  126.     wc.hInstance = hInstance;           /* Application that owns the class.   */
  127.     wc.hIcon = LoadIcon(hInstance, "DDEICON");
  128.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  129.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  130.     wc.lpszMenuName =  "DdetestMenu";   /* Name of menu resource in .RC file. */
  131.     wc.lpszClassName = "DdetestWClass"; /* Name used in call to CreateWindow. */
  132.  
  133.     /* Register the window class and return success/failure code. */
  134.  
  135.     return (RegisterClass(&wc));
  136. }
  137.  
  138.  
  139.  
  140. BOOL InitInstance(hInstance, nCmdShow)
  141.     HANDLE          hInstance;          /* Current instance identifier.       */
  142.     int             nCmdShow;           /* Param for first ShowWindow() call. */
  143. {
  144.     HWND            hWnd;               /* Main window handle.                */
  145.  
  146.     /* Save the instance handle in static variable, which will be used in  */
  147.     /* many subsequence calls from this application to Windows.            */
  148.  
  149.     hInst = hInstance;
  150.  
  151.     /* Create a main window for this application instance.  */
  152.  
  153.     hWnd = CreateWindow(
  154.         "DdetestWClass",                /* See RegisterClass() call.          */
  155.         "Ddetest Sample Application",   /* Text for window title bar.         */
  156.         WS_OVERLAPPEDWINDOW,            /* Window style.                      */
  157.         CW_USEDEFAULT,                  /* Default horizontal position.       */
  158.         CW_USEDEFAULT,                  /* Default vertical position.         */
  159.         CW_USEDEFAULT,                  /* Default width.                     */
  160.         CW_USEDEFAULT,                  /* Default height.                    */
  161.         NULL,                           /* Overlapped windows have no parent. */
  162.         NULL,                           /* Use the window class menu.         */
  163.         hInstance,                      /* This instance owns this window.    */
  164.         NULL                            /* Pointer not needed.                */
  165.     );
  166.  
  167.     /* If window could not be created, return "failure" */
  168.  
  169.     if (!hWnd)
  170.         return (FALSE);
  171.  
  172.     /* Make the window visible; update its client area; and return "success" */
  173.  
  174.     ShowWindow(hWnd, nCmdShow);  /* Show the window                        */
  175.     UpdateWindow(hWnd);          /* Sends WM_PAINT message                 */
  176.     return (TRUE);               /* Returns the value from PostQuitMessage */
  177.  
  178. }
  179.  
  180.  
  181. long FAR PASCAL MainWndProc(HWND hWnd, UINT message, WPARAM wParam,
  182.         LPARAM lParam)
  183. {
  184.     FARPROC lpProcAbout;                  /* pointer to the "About" function */
  185.     char buf[80];
  186.     int total;
  187.     char *ptr; 
  188.     char item[25];
  189.  
  190.     switch (message) {
  191.         case WM_CREATE:
  192.             pDdeData = DdeInit(progname, hInst, NULL);
  193.             assert(pDdeData != NULL);
  194.             total = 0;
  195.             ptr = databuf;
  196.             for (rows = 0; ; ++rows) {
  197.                 if (total + DATASTRLEN < sizeof(databuf)) {
  198.                     memcpy(ptr, datastr, DATASTRLEN);
  199.                     ptr += DATASTRLEN;
  200.                     total += DATASTRLEN;
  201.                 } else
  202.                     break;
  203.             } 
  204.             *ptr = EOS;
  205.             datalen = strlen(databuf);
  206.             sprintf(buf, "Test string length: %d", datalen);
  207.             /* MsgBox("DDE Data", buf); */
  208.             break;
  209.  
  210.         case WM_COMMAND:           /* message: command from application menu */
  211.             switch (wParam) {
  212.             int loop;
  213.             case IDM_LINK:
  214.                 pdde = DdeDlgStart(hWnd, FALSE);
  215.                 /* pdde = DdeInitiate(ddeapp, ddetopic); */
  216.                 if (pdde == NULL) {
  217.                     sprintf(buf, "Error connecting to %s|%s", 
  218.                         pDdeData->app, pDdeData->topic);
  219.                     MsgBox("DDE Link", buf);
  220.                 } else {
  221.                     strcpy(ddeapp, pdde->app);
  222.                     strcpy(ddeapp, pdde->topic);
  223.                 }
  224.                 break;
  225.  
  226.             case IDM_POKE:
  227.                 if (pdde == NULL)
  228.                     break;
  229.                 for (loop = 1; loop <= 10; ++loop) {
  230.                     CellRange(item, row, COL1, rows, NCOLS);
  231.                     DdePokeString(pdde, item, databuf);
  232.                     row += rows;
  233.                 }
  234.                 break;
  235.       
  236.             case IDM_REQUEST:
  237.                 if (pdde == NULL)
  238.                     break;
  239.                 DdeReqString(pdde, item);
  240.                 MsgBox("Request Data", pdde->pData);
  241.                 DdeFreeData(pdde);
  242.                 break;
  243.        
  244.             case IDM_EXECUTE:
  245.                 if (pdde == NULL)
  246.                     break;
  247.                 DdeExec(pdde, execstr);
  248.                 break;
  249.                             
  250.             case IDM_ABOUT:
  251.                 lpProcAbout = MakeProcInstance(About, hInst);
  252.                 DialogBox(hInst,                 /* current instance         */
  253.                     "AboutBox",                  /* resource to use          */
  254.                     hWnd,                        /* parent handle            */
  255.                     lpProcAbout);                /* About() instance address */
  256.  
  257.                 FreeProcInstance(lpProcAbout);
  258.                 break;
  259.  
  260.             default:
  261.                                     /* Lets Windows process it       */
  262.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  263.             }
  264.             break;
  265.  
  266.         case WM_DESTROY:                  /* message: window being destroyed */
  267.             DdeQuit();
  268.             PostQuitMessage(0);
  269.             break;
  270.  
  271.         default:                          /* Passes it on if unproccessed    */
  272.             return (DefWindowProc(hWnd, message, wParam, lParam));
  273.     }
  274.     return (NULL);
  275. }
  276.  
  277.  
  278.  
  279. BOOL FAR PASCAL About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  280. {
  281.     switch (message) {
  282.         case WM_INITDIALOG:                /* message: initialize dialog box */
  283.             return (TRUE);
  284.  
  285.         case WM_COMMAND:                      /* message: received a command */
  286.             if (wParam == IDOK                /* "OK" box selected?          */
  287.                 || wParam == IDCANCEL) {      /* System menu close command? */
  288.                 EndDialog(hDlg, TRUE);        /* Exits the dialog box        */
  289.                 return (TRUE);
  290.             }
  291.             break;
  292.     }
  293.     return (FALSE);                           /* Didn't process a message    */
  294. }
  295.  
  296.