home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / som / somd / cpp / dii / diitest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-24  |  11.8 KB  |  347 lines

  1. //
  2. //   COMPONENT_NAME: somx
  3. //
  4. //   ORIGINS: 27
  5. //
  6. //
  7. //   10H9767, 10H9769  (C) COPYRIGHT International Business Machines Corp. 1992,1994
  8. //   All Rights Reserved
  9. //   Licensed Materials - Property of IBM
  10. //   US Government Users Restricted Rights - Use, duplication or
  11. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  12. //
  13.  
  14. /* %Z% %I% %W% %G% %U% [%H% %T%] */
  15. /*
  16.  *
  17.  * DISCLAIMER OF WARRANTIES.
  18.  * The following [enclosed] code is sample code created by IBM
  19.  * Corporation. This sample code is not part of any standard or IBM
  20.  * product and is provided to you solely for the purpose of assisting
  21.  * you in the development of your applications.  The code is provided
  22.  * "AS IS". IBM MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT
  23.  * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24.  * FOR A PARTICULAR PURPOSE, REGARDING THE FUNCTION OR PERFORMANCE OF
  25.  * THIS CODE.  IBM shall not be liable for any damages arising out of
  26.  * your use of the sample code, even if they have been advised of the
  27.  * possibility of such damages.
  28.  *
  29.  * DISTRIBUTION.
  30.  * This sample code can be freely distributed, copied, altered, and
  31.  * incorporated into other software, provided that it bears the above
  32.  * Copyright notice and DISCLAIMER intact.
  33.  */
  34.  
  35. /*----------------------------------------
  36.    DIITEST.C -- DSOM Sample Program
  37.   ----------------------------------------*/
  38. /*
  39.  *  "diitest" is a sample program demonstrating the DSOM Dynamic
  40.  *  Invocation Interface.
  41.  */
  42.  
  43. #include <windows.h>
  44. #include <somd.xh>
  45. #include <request.xh>
  46. #include <nvlist.xh>
  47. #include <repostry.xh>
  48. #include <operatdf.xh>
  49. #include "foo.xh"
  50. #include "diitest.h"
  51. #include "nlsutil.h"
  52.  
  53. /***************************************************
  54. Messages for NLS support
  55. ****************************************************/
  56. static char *nlsmsgs[ENDNLSID-STARTNLSID+1];
  57. #define GetNlsMessage(id) nlsmsgs[id-STARTNLSID]
  58. #define SetNlsMessage(id, str) nlsmsgs[id-STARTNLSID] = (str);
  59.  
  60. /**********************************************************
  61. 18953: NLS support
  62. This procedure must be called first to initialize all the messages
  63. to be used by by this sample. These messages are obtained from the 
  64. resource file.
  65. ****************************************************************/
  66. static void InitNlsMsgs(){
  67.  
  68. SetNlsMessage(InvokeMethodId, NlsMsgAlloc(InvokeMethodId));
  69. SetNlsMessage(ResultId ,NlsMsgAlloc(ResultId));
  70. SetNlsMessage(Argument1Id, NlsMsgAlloc(Argument1Id));
  71. SetNlsMessage(Argument2Id, NlsMsgAlloc(Argument2Id));
  72. SetNlsMessage(ErrorId, NlsMsgAlloc(ErrorId));
  73. SetNlsMessage(ErrorMinorCodeId, NlsMsgAlloc(ErrorMinorCodeId));
  74. SetNlsMessage(ErrorCompletionId, NlsMsgAlloc(ErrorCompletionId));
  75. SetNlsMessage(YesId, NlsMsgAlloc(YesId));
  76. SetNlsMessage(NoId, NlsMsgAlloc(NoId));
  77. SetNlsMessage(MaybeId, NlsMsgAlloc(MaybeId));
  78. SetNlsMessage(ExceptionId, NlsMsgAlloc(ExceptionId));
  79. }
  80.  
  81. #define checkEv(ev) ((ev)->_major != NO_EXCEPTION)
  82.  
  83. void printEv(Environment *ev);
  84. void makeNVList(Environment *, char *, NVList **, NamedValue *);
  85. void setNVListValue(Environment *, NVList *, long, void *, long);
  86.  
  87. long APIENTRY WndProc (HWND, UINT, UINT, LONG) ;
  88.  
  89. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  90.                     LPSTR lpszCmdLine, int nCmdShow)
  91.      {
  92.      static char szAppName[] = "Diitest" ;
  93.      HWND        hwnd ;
  94.      MSG         msg ;
  95.      WNDCLASS    wndclass ;
  96.  
  97.      InitNlsMsgs();
  98.      somEnvironmentNew(); 
  99.  
  100.      if (!hPrevInstance)
  101.           {
  102.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  103.           wndclass.lpfnWndProc   = WndProc ;
  104.           wndclass.cbClsExtra    = 0 ;
  105.           wndclass.cbWndExtra    = 0 ;
  106.           wndclass.hInstance     = hInstance ;
  107.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  108.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  109.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  110.           wndclass.lpszMenuName  = NULL ;
  111.           wndclass.lpszClassName = szAppName ;
  112.  
  113.           RegisterClass (&wndclass) ;
  114.           }
  115.  
  116.      hwnd = CreateWindow (szAppName, "Dynamic Invocation Interface Sample",
  117.                           WS_OVERLAPPEDWINDOW,
  118.                           CW_USEDEFAULT, CW_USEDEFAULT,
  119.                           CW_USEDEFAULT, CW_USEDEFAULT,
  120.                           NULL, NULL, hInstance, NULL) ;
  121.  
  122.      ShowWindow (hwnd, nCmdShow) ;
  123.      UpdateWindow (hwnd) ;
  124.  
  125.      while (GetMessage (&msg, NULL, 0, 0))
  126.           {
  127.           TranslateMessage (&msg) ;
  128.           DispatchMessage (&msg) ;
  129.           }
  130.      return msg.wParam ;
  131.      }
  132.  
  133. long APIENTRY WndProc (HWND hwnd, UINT message, UINT wParam,
  134.                        LONG lParam)
  135.      {
  136.      static int   cxChar, cyChar ;
  137.      HDC          hdc ;
  138.      PAINTSTRUCT  ps ;
  139.      TEXTMETRIC   tm ;
  140.      char buffer[100];
  141.      short startChar;
  142.      static short winHeight, winWidth;
  143.  
  144.      static SOMDObject *fooObj;
  145.      static Environment *ev;
  146.      NVList *arglist;
  147.      static NamedValue result;
  148.      Request *reqObj;
  149.  
  150.      static string   arg1 = NULL;       /* ARG_OUT */
  151.      static long     arg2 = 999;        /* ARG_INOUT */
  152.      ReferenceData   arg3;              /* ARG_IN */
  153.  
  154.      switch (message)
  155.           {
  156.           case WM_CREATE:
  157.  
  158.                 /* local and DSOM initialization */
  159.                 ev = SOM_CreateLocalEnvironment();
  160.                 SOMD_Init(ev);
  161.  
  162.                 /* make sure classes are registered with SOMClassMgr */
  163.                 FooNewClass(0,0);
  164.  
  165.                 /* create a remote object */
  166.                 fooObj = (SOMDObject *)
  167.                             (SOMD_ObjectMgr->somdNewObject(ev, "Foo", NULL));
  168.                 if (checkEv(ev)) goto error;
  169.  
  170.                 /* create an NVList for the operation */
  171.                 makeNVList(ev, "Foo::method1", &arglist, &result);
  172.                 if (checkEv(ev)) goto error;
  173.  
  174.                 /* add parameters to the NVList */
  175.                 setNVListValue(ev, arglist, 0, (void *)&arg1, 0);
  176.                 if (checkEv(ev)) goto error;
  177.                 setNVListValue(ev, arglist, 1, (void *)&arg2, sizeof(arg2));
  178.                 if (checkEv(ev)) goto error;
  179.                 arg3._maximum = 1024;
  180.         arg3._length  = 0;
  181.         arg3._buffer  = NULL;
  182.                 setNVListValue(ev, arglist, 2, (void *)&arg3, 0);
  183.                 if (checkEv(ev)) goto error;
  184.  
  185.                 /* create a request for the operation */
  186.                 (void) fooObj->create_request(ev,
  187.                         (Context *)NULL, "method1", arglist,
  188.                         &result, &reqObj, (Flags)0);
  189.                 if (checkEv(ev)) goto error;
  190.  
  191.                 /* invoke request on object */
  192.                 (void) reqObj->invoke(ev, (Flags)0);
  193.                 if (checkEv(ev)) goto error;
  194.  
  195.                 return 0 ;
  196.  
  197.         error:
  198.                 printEv(ev);
  199.                 SendMessage(hwnd, WM_CLOSE, 0, 0L);
  200.                 return 0;
  201.  
  202.           case WM_SIZE:
  203.                 winHeight = HIWORD (lParam);
  204.                 winWidth = LOWORD (lParam);
  205.                 return 0;
  206.  
  207.           case WM_PAINT:
  208.                 hdc = BeginPaint (hwnd, &ps) ;
  209.                 SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  210.                 GetTextMetrics (hdc, &tm) ;
  211.                 cxChar = tm.tmAveCharWidth;
  212.                 cyChar = tm.tmHeight + tm.tmExternalLeading;
  213.  
  214.                 /* print result and out parameters */
  215.                 wsprintf(buffer, GetNlsMessage(InvokeMethodId));
  216.                 startChar = ((winWidth/cxChar)-strlen(buffer))/2*cxChar;
  217.                 TextOut (hdc, startChar, (winHeight/2)-(6*cyChar),
  218.                         (LPSTR)buffer, strlen(buffer));
  219.                 wsprintf(buffer, GetNlsMessage(ResultId),
  220.                         (LPSTR) *(string*) (result.argument._value));
  221.                 startChar = ((winWidth/cxChar)-strlen(buffer))/2*cxChar;
  222.                 TextOut (hdc, startChar, (winHeight/2)-(4*cyChar),
  223.                         (LPSTR)buffer, strlen(buffer));
  224.                 wsprintf(buffer, GetNlsMessage(Argument1Id),
  225.                         (LPSTR) arg1);
  226.                 TextOut (hdc, startChar, (winHeight/2)-(2*cyChar),
  227.                                         (LPSTR)buffer, strlen(buffer));
  228.                 wsprintf(buffer, GetNlsMessage(Argument2Id), arg2);
  229.                 TextOut (hdc, startChar, winHeight/2,
  230.                                         (LPSTR)buffer, strlen(buffer));
  231.                 EndPaint (hwnd, &ps) ;
  232.                 return 0 ;
  233.  
  234.           case WM_DESTROY:
  235.  
  236.                 /* destroy local proxy object and remote object */
  237.                 if (fooObj)
  238.                     SOMD_ObjectMgr->somdDestroyObject(ev, fooObj);
  239.  
  240.                 /* DSOM uninitialization: */
  241.                 SOMD_Uninit(ev);
  242.                 SOM_DestroyLocalEnvironment(ev);
  243.  
  244.                 PostQuitMessage (0) ;
  245.                 return 0 ;
  246.           }
  247.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  248.      }
  249.  
  250. /*
  251.  *  Prints exception information.
  252.  */
  253.  
  254. void printEv(Environment *ev)
  255. {
  256.   char *exId, *bp;
  257.   char buffer[200];
  258.   StExcep *params;
  259.  
  260.   exId = somExceptionId(ev);
  261.   if (!exId) exId = "None";
  262.   params = (StExcep *) somExceptionValue(ev);
  263.   bp = buffer;
  264.  
  265.   wsprintf(bp, GetNlsMessage(ErrorId), (LPSTR) exId);
  266.   bp += strlen(bp);
  267.   wsprintf(bp, GetNlsMessage(ErrorMinorCodeId), (params ? params->minor : 0));
  268.   bp += strlen(bp);
  269.   wsprintf(bp, GetNlsMessage(ErrorCompletionId),
  270.                 (LPSTR) (params ? (params->completed == YES ?
  271.                GetNlsMessage(YesId) :
  272.                 params->completed == NO ? GetNlsMessage(NoId): GetNlsMessage(MaybeId)) : GetNlsMessage(YesId)));
  273.   MessageBox ((HWND)NULL, buffer, GetNlsMessage(ExceptionId),
  274.                 MB_ICONEXCLAMATION | MB_OK);
  275.   somdExceptionFree(ev);
  276.   return;
  277. }
  278.  
  279. /*
  280.  *  Sets the result type and allocates an NVList for the operation.
  281.  */
  282.  
  283. void makeNVList(Environment *ev, char *methodid, NVList **arglist,
  284.                 NamedValue *result)
  285. {
  286.  
  287.     Repository *repos;
  288.     OperationDef *opdef;
  289.     Description desc;
  290.     OperationDescription *opdesc;
  291.  
  292.     /* get the OperationDef object from the Interface Repository */
  293.     repos = new Repository;
  294.     opdef = (OperationDef *) repos->lookup_id(ev, methodid);
  295.     if (ev->_major == NO_EXCEPTION) {
  296.  
  297.       /* get the operation description structure */
  298.  
  299.       desc = opdef->describe(ev);
  300.       /* desc = _SOM1ARGSTRUCTMETHOD (opdef,describe,ev);*/
  301.  
  302.       if (ev->_major == NO_EXCEPTION) {
  303.         opdesc = (OperationDescription *) desc.value._value;
  304.  
  305.         /* fill in the TypeCode field for result */
  306.         result->argument._type = opdesc->result;
  307.  
  308.         /* create a NamedValue list for the operation */
  309.         (void) SOMD_ORBObject->create_operation_list(ev, opdef, arglist);
  310.       }
  311.     }
  312.     return;
  313. }
  314.  
  315. /*
  316.  *  Adds parameters to the NVList.
  317.  */
  318.  
  319. void setNVListValue(Environment *ev, NVList *arglist, long pos, void *value,
  320.                     long valsz)
  321. {
  322.     Identifier name;
  323.     TypeCode tc;
  324.     void *dummy;
  325.     long dummylen;
  326.     Flags flags;
  327.  
  328.     /* get initialized fields of item first */
  329.     (void) arglist->get_item(ev, pos, &name, &tc, &dummy, &dummylen, &flags);
  330.     if (ev->_major == NO_EXCEPTION) {
  331.  
  332.       /*
  333.        * IMPORTANT NOTE:
  334.        * For scalar types, use the pointer to the variable.
  335.        * For arguments represented by structures (including "union", "sequence",
  336.        *     and "any"), use a pointer to the structure.
  337.        * For arguments represented by pointers (incl. strings, arrays, and
  338.        *     objrefs), use a pointer to the pointer.
  339.        */
  340.  
  341.       /* reset the value and length fields of the item */
  342.       (void) arglist->set_item(ev, pos, name, tc, value, valsz, flags);
  343.     }
  344.     return;
  345. }
  346.  
  347.