home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / primcuts.zip / DebugTerminal / sample / TestApplication.c < prev    next >
Text File  |  2000-08-26  |  6KB  |  253 lines

  1. #pragma strings(readonly)
  2.  
  3. #define INCL_WINWINDOWMGR
  4. #define INCL_WINFRAMEMGR
  5. #define INCL_WINSYS
  6.  
  7. #ifdef DEBUG_TERM
  8. #define INCL_DOSPROCESS
  9. #define INCL_DOSQUEUES
  10. #define INCL_DOSSESMGR
  11. #define INCL_DOSERRORS
  12. #endif
  13.  
  14. #include <os2.h>
  15.  
  16.  
  17. #ifdef DEBUG_TERM
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <memory.h>
  21. static void launchDebugTerminal(void);
  22. #endif
  23.  
  24.  
  25. #include <memory.h>
  26.  
  27. #include "resource.h"
  28.  
  29.  
  30.  
  31. extern BOOL _Optlink registerClientClass(HAB hAB);
  32.  
  33. /*
  34.  * Function prototypes - local functions
  35.  */
  36. static HWND _Optlink createFrame(HAB hAB);
  37. static HWND _Optlink createClient(HAB hAB, HWND hwndFrame);
  38.  
  39.  
  40.  
  41. int main(void)
  42. {
  43.    HAB hAB = NULLHANDLE;
  44.    HMQ hMQ = NULLHANDLE;
  45.  
  46. #ifdef DEBUG_TERM
  47.    launchDebugTerminal();
  48. #endif
  49.  
  50. #ifdef DEBUG_TERM
  51.    puts("i Initialize PM");
  52. #endif
  53.  
  54.    hAB = WinInitialize(0);
  55.    if(hAB)
  56.    {
  57.       hMQ = WinCreateMsgQueue(hAB, 0L);
  58.    }
  59.  
  60.    registerClientClass(hAB);
  61.  
  62.    if(hMQ)
  63.    {
  64.       HWND hwndFrame = NULLHANDLE;
  65.       HWND hwndClient = NULLHANDLE;
  66.  
  67. #ifdef DEBUG_TERM
  68.       printf("i AnchorBlock handle: %08x\ni MessageQueue handle: %08x\n", hAB, hMQ);
  69. #endif
  70.  
  71.       if(WinLoadString(hAB, NULLHANDLE, 0xdeadface, 0, NULL) == FALSE)
  72.       {
  73. #ifdef DEBUG_TERM
  74.       printf("w Couldn't load resource string 0x%08x\n", 0xdeadface);
  75. #endif
  76.       }
  77.  
  78.       if(WinLoadString(hAB, NULLHANDLE, 0xdeadbeef, 0, NULL) == FALSE)
  79.       {
  80. #ifdef DEBUG_TERM
  81.       printf("e Couldn't load resource string 0x%08x\n", 0xdeadbeef);
  82. #endif
  83.       }
  84.  
  85.       hwndFrame = createFrame(hAB);
  86.       if(hwndFrame)
  87.       {
  88.          hwndClient = createClient(hAB, hwndFrame);
  89.       }
  90.       else
  91.       {
  92. #ifdef DEBUG_TERM
  93.          puts("e createFrame() failed");
  94. #endif
  95.       }
  96.  
  97.       if(hwndClient)
  98.       {
  99.          QMSG qmsg = { 0 };
  100.  
  101. #ifdef DEBUG_TERM
  102.          puts("i Frame and Client windows created successfully");
  103. #endif
  104.  
  105.          WinSetWindowPos(hwndFrame, HWND_TOP, 42L, 100L, 290L, 160L, SWP_SIZE | SWP_MOVE | SWP_ZORDER | SWP_ACTIVATE | SWP_SHOW);
  106.  
  107.          while(WinGetMsg(hAB, &qmsg, (HWND)NULLHANDLE, 0UL, 0UL))
  108.             WinDispatchMsg(hAB, &qmsg);
  109.  
  110.          WinDestroyWindow(hwndFrame);
  111.       }
  112.    }
  113.    else
  114.    {
  115. #ifdef DEBUG_TERM
  116.       puts("e Couldn't create messagequeue");
  117. #endif
  118.    }
  119.  
  120. #ifdef DEBUG_TERM
  121.    puts("- end");
  122. #endif
  123.  
  124.    if(hAB)
  125.    {
  126.       if(!WinTerminate(hAB))
  127.       {
  128. #ifdef DEBUG_TERM
  129.          printf("e WinTerminate(%08x) failed in %s -> %s\n", hAB, __FILE__, __FUNCTION__);
  130. #endif
  131.       }
  132.    }
  133.  
  134.    /*
  135.     * Give application some time to empty the pipe
  136.     */
  137. #ifdef DEBUG_TERM
  138.    DosSleep(100);
  139. #endif
  140.  
  141.    return 0;
  142. }
  143.  
  144.  
  145. static HWND _Optlink createFrame(HAB hAB)
  146. {
  147.    LONG lLength = 0;
  148.    char pszTitle[64] = "";
  149.    HWND hWnd = NULLHANDLE;
  150.  
  151.    lLength = WinLoadString(hAB, (HMODULE)NULLHANDLE, IDS_APPTITLE, sizeof(pszTitle), pszTitle);
  152.    if(lLength != 0L)
  153.    {
  154.       FRAMECDATA fcd = { 0 };
  155.  
  156. #ifdef DEBUG_TERM
  157.       printf("i Resource string '%s' loaded successfully in %s -> %s\n", pszTitle, __FILE__, __FUNCTION__);
  158. #endif
  159.  
  160.       fcd.cb = sizeof(fcd);
  161.       fcd.flCreateFlags = FCF_SYSMENU | FCF_TITLEBAR | FCF_TASKLIST | FCF_DLGBORDER | FCF_MINMAX | FCF_ICON;
  162.       fcd.hmodResources = NULLHANDLE;
  163.       fcd.idResources = WIN_MAIN;
  164.  
  165.       hWnd = WinCreateWindow(HWND_DESKTOP, WC_FRAME, pszTitle, 0UL, 0L, 0L, 0L, 0L, (HWND)HWND_DESKTOP, HWND_TOP, fcd.idResources, &fcd, NULL);
  166.    }
  167.    return hWnd;
  168. }
  169.  
  170.  
  171. static HWND _Optlink createClient(HAB hAB, HWND hwndFrame)
  172. {
  173.    HWND hWnd = NULLHANDLE;
  174.    LONG lLength = 0;
  175.    char pszClientClass[64] = "";
  176.  
  177.    lLength = WinLoadString(hAB, (HMODULE)NULLHANDLE, IDS_CLIENTCLASS, sizeof(pszClientClass), pszClientClass);
  178.    if(lLength != 0L)
  179.    {
  180.       BYTE buf[512] = "";
  181.       PRESPARAMS *pp = buf;
  182.  
  183.       pp->cb = 4+4+10;
  184.       pp->aparam[0].id = PP_FONTNAMESIZE;
  185.       pp->aparam[0].cb = 10;
  186.       memcpy(pp->aparam->ab, "9.WarpSans", 10);
  187.  
  188.       hWnd = WinCreateWindow(hwndFrame, pszClientClass, (PSZ)NULL, WS_VISIBLE, 0L, 0L, 0L, 0L, (HWND)hwndFrame, HWND_TOP, FID_CLIENT, NULL, pp);
  189.    }
  190.    else
  191.    {
  192. #ifdef DEBUG_TERM
  193.       puts("e Couldn't load client classname string from resource");
  194. #endif
  195.    }
  196.  
  197.    return hWnd;
  198. }
  199.  
  200.  
  201. #ifdef DEBUG_TERM
  202. static void launchDebugTerminal(void)
  203. {
  204.    APIRET rc = NO_ERROR;
  205.    HFILE pipeDebugRead = 0;
  206.    HFILE pipeDebugWrite = 0;
  207.    STARTDATA sd = { 0 };
  208.    ULONG SessID = 0;
  209.    PID pid = 0;
  210.    HFILE hfNew = 1;                      /* stdout */
  211.    char PgmTitle[30] = "";
  212.    char PgmName[100] = "";
  213.    char szCommandLine[60] = "";
  214.    char ObjBuf[200] = "";
  215.  
  216.    rc = DosCreatePipe(&pipeDebugRead, &pipeDebugWrite, 4096);
  217.    if(rc != NO_ERROR)
  218.    {
  219.       DosBeep(1000, 100);
  220.       exit(42);
  221.    }
  222.  
  223.    _ultoa(pipeDebugRead, szCommandLine, 10);
  224.  
  225.    memset(&sd, 0, sizeof(sd));
  226.  
  227.    sd.Length = sizeof(STARTDATA);
  228.    sd.Related = SSF_RELATED_CHILD;
  229.    sd.FgBg = SSF_FGBG_BACK;
  230.    sd.TraceOpt = SSF_TRACEOPT_NONE;
  231.    memcpy((char*) PgmTitle, "Debug terminal\0", 15);
  232.    sd.PgmTitle = PgmTitle;
  233.    memcpy((char*) PgmName, "DebugTerminal.exe\0", 18);
  234.  
  235.    sd.PgmName = PgmName;
  236.    sd.PgmInputs = szCommandLine;
  237.    sd.InheritOpt = SSF_INHERTOPT_PARENT;
  238.    sd.SessionType = SSF_TYPE_PM;
  239.  
  240.    sd.PgmControl = SSF_CONTROL_VISIBLE | SSF_CONTROL_SETPOS;
  241.    sd.ObjectBuffer = ObjBuf;
  242.    sd.ObjectBuffLen = sizeof(ObjBuf);
  243.  
  244.    rc = DosStartSession(&sd, &SessID, &pid);
  245.    if(rc != NO_ERROR)
  246.    {
  247.       DosBeep(1000, 100);
  248.       exit(43);
  249.    }
  250.    DosDupHandle(pipeDebugWrite, &hfNew);
  251. }
  252. #endif
  253.