home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / mapi / remote.srv / common.cpp < prev    next >
C/C++ Source or Header  |  1996-04-11  |  12KB  |  453 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  File Name 
  4. //      COMMON.CPP
  5. //
  6. //  Description
  7. //
  8. //  Author
  9. //      Irving De la Cruz
  10. //
  11. //  Revision: 1.7
  12. //
  13. // Written for Microsoft Windows Developer Support
  14. // Copyright (c) 1995-1996 Microsoft Corporation. All rights reserved.
  15. //
  16. #include <WINDOWS.H>
  17. #include "TRACES.H"
  18. #include "WINDSGBL.H"
  19. #include "WDSADM.H"
  20. #include "COMMON.H"
  21.  
  22. // Remark this line to turn verbose tracing OFF
  23. //#define DO_INFO_TRACES
  24. #ifdef DO_INFO_TRACES
  25. #define InfoTrace(a)        TraceInfoMessage(a)
  26. #else
  27. #define InfoTrace(a)        0
  28. #endif // DO_INFO_TRACES
  29.  
  30.  
  31. extern "C"
  32. {
  33.     extern HINSTANCE ghInstance;
  34.     extern TCHAR g_szAppTitle[];
  35. }
  36.  
  37. ///////////////////////////////////////////////////////////////////////////////
  38. //    InsertNewDLMNodeA()
  39. //
  40. //    Parameters
  41. //
  42. //    Purpose
  43. //
  44. //    Return Value
  45. //
  46. void WINAPI InsertNewDLMNodeA (DLM_INFO_A Info, DLM_XMIT_LIST_A * pList)
  47. {
  48.     DLM_LIST_A * pNewNode = (DLM_LIST_A *)midl_user_allocate (sizeof (DLM_LIST_A));
  49.     if (NULL == pNewNode)
  50.     {
  51.         TraceMessage ("InsertNewDLMNodeA: Failed to allocate new node");
  52.         return;
  53.     }
  54.     pNewNode->Info = Info;
  55.     pNewNode->pNext = NULL;
  56.     
  57.     DLM_XMIT_LIST_A * pCurrent = pList;
  58.     while (pCurrent->pNext)
  59.     {
  60.         pCurrent = pCurrent->pNext;
  61.     }
  62.     pCurrent->pNext = pNewNode;
  63. }
  64.  
  65. ///////////////////////////////////////////////////////////////////////////////
  66. //    DLM_XMIT_LIST_A_to_xmit()
  67. //
  68. //    Parameters
  69. //
  70. //    Purpose
  71. //
  72. //    Return Value
  73. //
  74. void __RPC_USER DLM_XMIT_LIST_A_to_xmit (DLM_XMIT_LIST_A __RPC_FAR * pList,
  75.                                          DLM_ARRAY_A __RPC_FAR * __RPC_FAR * ppArray)
  76. {
  77.     InfoTrace ("DLM_XMIT_LIST_A_to_xmit: Function invoked");
  78.  
  79.     DLM_XMIT_LIST_A * pHead = pList;  // save pointer to start
  80.     DLM_ARRAY_A * pArray;
  81.  
  82.     // Count the number of member in the list
  83.     ULONG ulCount = 0;
  84.     for (; pList != NULL; pList = pList->pNext)
  85.     {
  86.         ulCount++;
  87.     }
  88.  
  89.     *ppArray = NULL;
  90.  
  91.     // Allocate the memory for the array using the MIDL memory allocator (which we implement)
  92.     pArray = (DLM_ARRAY_A *)midl_user_allocate (sizeof(DLM_ARRAY_A) + (ulCount * sizeof(DLM_INFO_A)));
  93.     if (NULL == pArray)
  94.     {
  95.         TraceMessage ("DLM_XMIT_LIST_A_to_xmit: Failed to allocate XMIT array");
  96.         return;
  97.     }
  98.     pArray->dwCount = ulCount;
  99.  
  100.     // Copy the linked list of members into the allocated array.
  101.     for (ulCount = 0, pList = pHead; pList != NULL; pList = pList->pNext)
  102.     {
  103.         pArray->pMembers[ulCount++] = pList->Info;
  104.     }
  105.  
  106.     // Return the array allocated to the RPC stubs
  107.     *ppArray = pArray;
  108. }
  109.  
  110. ///////////////////////////////////////////////////////////////////////////////
  111. //    DLM_XMIT_LIST_A_from_xmit()
  112. //
  113. //    Parameters
  114. //
  115. //    Purpose
  116. //
  117. //    Return Value
  118. //
  119. void __RPC_USER DLM_XMIT_LIST_A_from_xmit (DLM_ARRAY_A __RPC_FAR * pArray,
  120.                                            DLM_XMIT_LIST_A __RPC_FAR * pList)
  121. {
  122.     InfoTrace ("DLM_XMIT_LIST_A_from_xmit: Function invoked");
  123.     
  124.     if (pArray->dwCount <= 0)
  125.     {
  126.         pList = NULL;
  127.         return;
  128.     }
  129.  
  130.     if (pList == NULL)
  131.     {
  132.         InsertNewDLMNodeA (pArray->pMembers[0], pList);
  133.     }
  134.     else
  135.     {
  136.         DLM_XMIT_LIST_A_free_inst (pList);
  137.         pList->Info = pArray->pMembers[0];
  138.         pList->pNext = NULL;
  139.     }
  140.  
  141.     for (UINT i=1; i<pArray->dwCount; i++)
  142.     {
  143.         InsertNewDLMNodeA (pArray->pMembers[i], pList);
  144.     }
  145. }
  146.  
  147. ///////////////////////////////////////////////////////////////////////////////
  148. //    DLM_XMIT_LIST_A_free_inst()
  149. //
  150. //    Parameters
  151. //
  152. //    Purpose
  153. //
  154. //    Return Value
  155. //
  156. void __RPC_USER DLM_XMIT_LIST_A_free_inst (DLM_XMIT_LIST_A __RPC_FAR * pList)
  157. {
  158.     InfoTrace ("DLM_XMIT_LIST_A_free_inst: Function invoked");
  159.     // Don't delete the first node.
  160.     DLM_XMIT_LIST_A * pNextNode, *pNode = pList->pNext;
  161.     ZeroMemory (pList, sizeof(DLM_XMIT_LIST_A));
  162.  
  163.     while (pNode)
  164.     {
  165.         pNextNode = pNode->pNext;
  166.         midl_user_free (pNode);
  167.         pNode = pNextNode;
  168.     }
  169. }
  170.  
  171. ///////////////////////////////////////////////////////////////////////////////
  172. //    DLM_XMIT_LIST_A_free_xmit()
  173. //
  174. //    Parameters
  175. //
  176. //    Purpose
  177. //
  178. //    Return Value
  179. //
  180. void __RPC_USER DLM_XMIT_LIST_A_free_xmit (DLM_ARRAY_A __RPC_FAR * pArray)
  181. {
  182.     InfoTrace ("DLM_XMIT_LIST_A_free_xmit: Function invoked");
  183.     midl_user_free (pArray);
  184. }
  185.  
  186. ///////////////////////////////////////////////////////////////////////////////
  187. //    InsertNewDLMNodeW()
  188. //
  189. //    Parameters
  190. //
  191. //    Purpose
  192. //
  193. //    Return Value
  194. //
  195. void WINAPI InsertNewDLMNodeW (DLM_INFO_W Info, DLM_XMIT_LIST_W * pList)
  196. {
  197.     DLM_LIST_W * pNewNode = (DLM_LIST_W *)midl_user_allocate (sizeof (DLM_LIST_W));
  198.     if (NULL == pNewNode)
  199.     {
  200.         TraceMessage ("InsertNewDLMNodeW: Failed to allocate new node");
  201.         return;
  202.     }
  203.     pNewNode->Info = Info;
  204.     pNewNode->pNext = NULL;
  205.     
  206.     DLM_XMIT_LIST_W * pCurrent = pList;
  207.     while (pCurrent->pNext)
  208.     {
  209.         pCurrent = pCurrent->pNext;
  210.     }
  211.     pCurrent->pNext = pNewNode;
  212. }
  213.  
  214. ///////////////////////////////////////////////////////////////////////////////
  215. //    DLM_XMIT_LIST_W_to_xmit()
  216. //
  217. //    Parameters
  218. //
  219. //    Purpose
  220. //
  221. //    Return Value
  222. //
  223. void __RPC_USER DLM_XMIT_LIST_W_to_xmit (DLM_XMIT_LIST_W __RPC_FAR * pList,
  224.                                          DLM_ARRAY_W __RPC_FAR * __RPC_FAR * ppArray)
  225. {
  226.     InfoTrace ("DLM_XMIT_LIST_W_to_xmit: Function invoked");
  227.  
  228.     DLM_XMIT_LIST_W * pHead = pList;  // save pointer to start
  229.     DLM_ARRAY_W * pArray;
  230.  
  231.     // Count the number of member in the list
  232.     ULONG ulCount = 0;
  233.     for (; pList != NULL; pList = pList->pNext)
  234.     {
  235.         ulCount++;
  236.     }
  237.  
  238.     *ppArray = NULL;
  239.  
  240.     // Allocate the memory for the array using the MIDL memory allocator (which we implement)
  241.     pArray = (DLM_ARRAY_W *)midl_user_allocate (sizeof(DLM_ARRAY_W) + (ulCount * sizeof(DLM_INFO_W)));
  242.     if (NULL == pArray)
  243.     {
  244.         TraceMessage ("DLM_XMIT_LIST_W_to_xmit: Failed to allocate XMIT array");
  245.         return;
  246.     }
  247.     pArray->dwCount = ulCount;
  248.  
  249.     // Copy the linked list of members into the allocated array.
  250.     for (ulCount = 0, pList = pHead; pList != NULL; pList = pList->pNext)
  251.     {
  252.         pArray->pMembers[ulCount++] = pList->Info;
  253.     }
  254.  
  255.     // Return the array allocated to the RPC stubs
  256.     *ppArray = pArray;
  257. }
  258.  
  259. ///////////////////////////////////////////////////////////////////////////////
  260. //    DLM_XMIT_LIST_W_from_xmit()
  261. //
  262. //    Parameters
  263. //
  264. //    Purpose
  265. //
  266. //    Return Value
  267. //
  268. void __RPC_USER DLM_XMIT_LIST_W_from_xmit (DLM_ARRAY_W __RPC_FAR * pArray,
  269.                                            DLM_XMIT_LIST_W __RPC_FAR * pList)
  270. {
  271.     InfoTrace ("DLM_XMIT_LIST_W_from_xmit: Function invoked");
  272.     
  273.     if (pArray->dwCount <= 0)
  274.     {
  275.         pList = NULL;
  276.         return;
  277.     }
  278.  
  279.     if (pList == NULL)
  280.     {
  281.         InsertNewDLMNodeW (pArray->pMembers[0], pList);
  282.     }
  283.     else
  284.     {
  285.         DLM_XMIT_LIST_W_free_inst (pList);
  286.         pList->Info = pArray->pMembers[0];
  287.         pList->pNext = NULL;
  288.     }
  289.  
  290.     for (UINT i=1; i<pArray->dwCount; i++)
  291.     {
  292.         InsertNewDLMNodeW (pArray->pMembers[i], pList);
  293.     }
  294. }
  295.  
  296. ///////////////////////////////////////////////////////////////////////////////
  297. //    DLM_XMIT_LIST_W_free_inst()
  298. //
  299. //    Parameters
  300. //
  301. //    Purpose
  302. //
  303. //    Return Value
  304. //
  305. void __RPC_USER DLM_XMIT_LIST_W_free_inst (DLM_XMIT_LIST_W __RPC_FAR * pList)
  306. {
  307.     InfoTrace ("DLM_XMIT_LIST_W_free_inst: Function invoked");
  308.     // Don't delete the first node.
  309.     DLM_XMIT_LIST_W * pNextNode, * pNode = pList->pNext;
  310.     ZeroMemory (pList, sizeof(DLM_XMIT_LIST_W));
  311.  
  312.     while (pNode)
  313.     {
  314.         pNextNode = pNode->pNext;
  315.         midl_user_free (pNode);
  316.         pNode = pNextNode;
  317.     }
  318. }
  319.  
  320. ///////////////////////////////////////////////////////////////////////////////
  321. //    DLM_XMIT_LIST_W_free_xmit()
  322. //
  323. //    Parameters
  324. //
  325. //    Purpose
  326. //
  327. //    Return Value
  328. //
  329. void __RPC_USER DLM_XMIT_LIST_W_free_xmit (DLM_ARRAY_W __RPC_FAR * pArray)
  330. {
  331.     InfoTrace ("DLM_XMIT_LIST_W_free_xmit: Function invoked");
  332.     midl_user_free (pArray);
  333. }
  334.  
  335. ///////////////////////////////////////////////////////////////////////////////
  336. //    PrivateMessageBox()
  337. //
  338. //    Parameters
  339. //      ids             String ID of a string in the resource string table
  340. //      hOwnerWnd       Window who will own the Message Box
  341. //      uFlags          Flags (options) for the message box
  342. //      
  343. //    Purpose
  344. //      This function display a message box with a string loaded from the
  345. //      string table of this app. The ID of the string is passed in the ids
  346. //      parameter. The dialog box is modal with respect to the window
  347. //      identified in hOwnerWnd. The options to display the dialog box are
  348. //      passed in uFlags
  349. //      
  350. //    Return Value
  351. //      ID of the button pressed in the message box dialog box
  352. //      
  353. int WINAPI PrivateMessageBox (UINT ids, HWND hOwnerWnd, UINT uFlags)
  354. {
  355.     // Default response
  356.     int nResponse = -1;
  357.     // Get the string from the string table. The size of the buffer is the
  358.     // maximum number of character allowed in the character buffer, without
  359.     // the accounting for the NULL terminator
  360.     TCHAR szBuffer[256];
  361.     // Set the cursor to an arrow, in case it wasn't.
  362.     HCURSOR hPrevCursor = SetCursor (LoadCursor (NULL, MAKEINTRESOURCE(IDC_ARROW)));
  363.     if (LoadString (ghInstance, ids, szBuffer, 255))
  364.     {
  365.         if (0 == uFlags)
  366.         {
  367.             uFlags = MB_OK | MB_ICONSTOP;
  368.         }
  369.         uFlags |= MB_SETFOREGROUND;
  370.         nResponse = MessageBox (hOwnerWnd, szBuffer, g_szAppTitle, uFlags);
  371.     }
  372.     // Before returning, reset the cursor to the previous state
  373.     SetCursor (hPrevCursor);
  374.     return nResponse;
  375. }
  376.  
  377. ///////////////////////////////////////////////////////////////////////////////
  378. //    CenterDialogBox()
  379. //
  380. //    Parameters
  381. //      hDlg    Handle to the dialog (or window) that we want to center.
  382. //      
  383. //    Purpose
  384. //      This function centers a window, vertically and horizontally,
  385. //      with respect to the desktop (or screen)
  386. //      
  387. //    Return Value
  388. //      None.
  389. //      
  390. void WINAPI CenterDialogBox (HWND hDlg)
  391. {
  392.     POINT   pt;
  393.     RECT    r1, r2;
  394.     int    iWidth, iHeight;
  395.     GetWindowRect (GetDesktopWindow(), &r1);
  396.     GetWindowRect (hDlg, &r2);
  397.     iWidth = r2.right - r2.left;
  398.     iHeight = r2.bottom - r2.top;
  399.     pt.x = (r1.right - r1.left) / 2;
  400.     pt.y = (r1.bottom - r1.top) / 2;
  401.     pt.x = pt.x - (iWidth / 2);
  402.     pt.y = pt.y - (iHeight / 2);
  403.     MoveWindow (hDlg, pt.x, pt.y, iWidth, iHeight, FALSE);
  404. }
  405.  
  406. ///////////////////////////////////////////////////////////////////////////////
  407. //    midl_user_allocate()
  408. //
  409. //    Parameters
  410. //      len     Size (in bytes) of the memory block to allocate for the
  411. //              RPC object
  412. //
  413. //    Purpose
  414. //      Allocates memory as needed by the RPC runtime library.
  415. //      The stubs or runtime libraries may need to allocate memory.
  416. //      By convention, they call a user-specified function named
  417. //      midl_user_allocate.
  418. //
  419. //    Return Value
  420. //      Pointer to a block of memory of len byte in size
  421. //
  422. void __RPC_FAR * __RPC_API midl_user_allocate (size_t len)
  423. {
  424.     LPVOID pVoid = HeapAlloc (GetProcessHeap(), HEAP_ZERO_MEMORY, (DWORD)len);
  425.     if (NULL == pVoid)
  426.     {
  427.         TraceMessage ("midl_user_allocate: Failed to allocated RPC memory block");
  428.     }
  429.     return pVoid;
  430. }
  431.  
  432. ///////////////////////////////////////////////////////////////////////////////
  433. //    midl_user_free()
  434. //
  435. //    Parameters
  436. //      ptr     Pointer to memory block to release
  437. //
  438. //    Purpose
  439. //      Free memory as needed by the RPC runtime library.
  440. //      The stubs or runtime libraries may need to free memory.
  441. //      By convention, they call a user-specified function named
  442. //      midl_user_free.
  443. //
  444. //    Return Value
  445. //      None
  446. //
  447. void __RPC_API midl_user_free (void __RPC_FAR * ptr)
  448. {
  449.     HeapFree (GetProcessHeap(), 0, ptr);
  450. }
  451.  
  452. // End of file for COMMON.CPP
  453.