home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / oleaut / browseh / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-31  |  5.2 KB  |  231 lines

  1. /*************************************************************************
  2. **
  3. **  This is a part of the Microsoft Source Code Samples.
  4. **
  5. **  Copyright (C) 1992-1997 Microsoft Corporation. All rights reserved.
  6. **
  7. **  This source code is only intended as a supplement to Microsoft Development
  8. **  Tools and/or WinHelp documentation.  See these sources for detailed
  9. **  information regarding the Microsoft samples programs.
  10. **
  11. **  OLE Automation TypeLibrary Browse Helper Sample
  12. **
  13. **  main.cpp
  14. **
  15. **  new and delete operator redefinition to use memory manager of calling
  16. **  task.
  17. **  LibMain, WEP, DllGetClassObject, DllCanUnloadNow, DLL initialization.
  18. **  Procedure to create standard dispatch implementation.
  19. **
  20. **  Written by Microsoft Product Support Services, Windows Developer Support
  21. **
  22. *************************************************************************/
  23. #include <windows.h>
  24. #include <windowsx.h>
  25. #ifdef WIN16   
  26.   #include <ole2.h>
  27.   #include <compobj.h>    
  28.   #include <dispatch.h> 
  29.   #include <variant.h>
  30.   #include <olenls.h>  
  31. #endif 
  32. #include <initguid.h>
  33. #include "browseh.h"          
  34.  
  35. // Globals
  36. HINSTANCE   g_hinst;                   // Instance of application
  37.                                        //Count number of objects and number of locks.
  38. ULONG       g_cObj=0;
  39. ULONG       g_cLock=0;
  40.  
  41. // String resource buffers
  42. TCHAR g_szServerName[STR_LEN];
  43.  
  44.  
  45. /*
  46.  * new
  47.  *
  48.  * Purpose:
  49.  *   Since this is an InProcServer object, the memory allocator used by 
  50.  *   the calling task must be used.
  51.  *   This is done by redefining the global new operator and using new 
  52.  *   for all memory allocations.
  53.  */
  54. void FAR* operator new(size_t size)
  55. {
  56.     IMalloc FAR* pMalloc;
  57.     LPVOID lpv;
  58.  
  59.     if (CoGetMalloc(MEMCTX_TASK, &pMalloc) == NOERROR)
  60.     {
  61.         lpv = pMalloc->Alloc(size);
  62.         pMalloc->Release();
  63.         return lpv;
  64.     }
  65.     return NULL;
  66. }
  67.  
  68. /*
  69.  * delete
  70.  *
  71.  * Purpose:
  72.  *   Use the memory manager of the calling task to free memory.
  73.  */
  74. void operator delete(void FAR* lpv)
  75. {
  76.     IMalloc FAR* pMalloc;
  77.  
  78.     if (lpv == NULL) return;
  79.  
  80.     if( CoGetMalloc(MEMCTX_TASK, &pMalloc) == NOERROR) 
  81.     {
  82.         if (pMalloc->DidAlloc(lpv))
  83.             pMalloc->Free(lpv);
  84.         pMalloc->Release();
  85.     }
  86. }
  87.  
  88.  
  89. #ifdef WIN16
  90. /*
  91.  * LibMain
  92.  *
  93.  * Purpose:
  94.  *  Called by Win16 on DLL load. Does any one-time initializations.
  95.  *
  96.  */
  97. int PASCAL LibMain (HINSTANCE hinst, WORD wDataSeg, WORD cbHeapSize, LPSTR lpCmdLine)
  98. {
  99.    if (cbHeapSize != 0)
  100.        UnlockData(0);
  101.    
  102.    g_hinst = hinst;    
  103.  
  104.    if (!InitDLL(hinst))
  105.          return FALSE;
  106.  
  107.    return TRUE;
  108. }
  109.  
  110. /*
  111.  * WEP
  112.  *
  113.  * Purpose:
  114.  *  Called by Windows on DLL unload. 
  115.  *
  116.  */
  117. extern "C" void FAR PASCAL _WEP(int bSystemExit)
  118. {
  119.     return;
  120. }
  121.  
  122. #else //Win 32
  123.  
  124. BOOL WINAPI DllMain (HINSTANCE hinst, DWORD dwReason, LPVOID lpReserved)
  125. {
  126.     switch (dwReason)
  127.     {
  128.         case DLL_PROCESS_ATTACH:
  129.             if (!InitDLL(hinst))
  130.                 return FALSE;
  131.             else return TRUE;
  132.  
  133.         default:
  134.             return TRUE;
  135.     }
  136. }
  137.  
  138. #endif 
  139.  
  140. /*
  141.  * DLLGetClassObject
  142.  *
  143.  * Purpose:
  144.  *  OLE calls this funtion to obtain the class factory. Note that the class 
  145.  *  factory is not registered by the inproc server. 
  146.  *
  147.  */
  148. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid,
  149.                                    LPVOID FAR *ppv)
  150. {    
  151.     LPCLASSFACTORY pcf;
  152.     HRESULT hr;
  153.     
  154.     *ppv =  NULL;
  155.     
  156.     // Check if this CLSID is supported.
  157.     if (rclsid != CLSID_BrowseHelper)
  158.        return E_FAIL;
  159.     
  160.     // Create class factory and return it  
  161.     pcf = new CBrowseHelperCF;  
  162.     if (!pcf)
  163.         return E_OUTOFMEMORY;  
  164.     hr = pcf->QueryInterface(riid, ppv);
  165.     if (FAILED(hr)) 
  166.     {
  167.         delete pcf;
  168.         return hr;
  169.     }          
  170.     return NOERROR;
  171. }
  172.  
  173. /*
  174.  * DLLCanUnloadNow
  175.  *
  176.  * Purpose:
  177.  *  DllCanUnloadNow is called by OLE to determine if the DLL can be unloded. 
  178.  *
  179.  */
  180. STDAPI DllCanUnloadNow(void)
  181. {  
  182.     if (g_cObj==0L && g_cLock==0L)
  183.         return S_OK;
  184.     else return S_FALSE;
  185. }     
  186.  
  187. /*
  188.  * InitDLL
  189.  *
  190.  * Purpose:
  191.  *  Load strings & Registers the window class
  192.  *
  193.  * Parameters:
  194.  *  hinstance       hinstance of application
  195.  *
  196.  */
  197. BOOL InitDLL (HINSTANCE hinst)
  198. {  
  199.    return LoadString(hinst, IDS_SERVERNAME, g_szServerName, sizeof(g_szServerName));
  200. }
  201.  
  202. /*
  203.  * Quick & Dirty ANSI/Unicode conversion routines. These routines use a static
  204.  * buffer of fixed size to hold the converted string. Consequently these
  205.  * routines are limited to strings of size STRCONVERT_MAXLEN. Also the same
  206.  * buffer is reused when the routine is called a second time. So make sure
  207.  * that the converted string is used before the conversion routine is called
  208.  * again
  209.  */
  210. #ifdef WIN32
  211.  
  212. #ifndef UNICODE
  213. char* ConvertToAnsi(OLECHAR FAR* szW)
  214. {
  215.   static char achA[STRCONVERT_MAXLEN]; 
  216.   
  217.   WideCharToMultiByte(CP_ACP, 0, szW, -1, achA, STRCONVERT_MAXLEN, NULL, NULL);  
  218.   return achA; 
  219.  
  220. OLECHAR* ConvertToUnicode(char FAR* szA)
  221. {
  222.   static OLECHAR achW[STRCONVERT_MAXLEN]; 
  223.  
  224.   MultiByteToWideChar(CP_ACP, 0, szA, -1, achW, STRCONVERT_MAXLEN);  
  225.   return achW; 
  226. }
  227. #endif
  228.  
  229. #endif   
  230.