home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / sdk / win32s / ut / samples / utsample / utsamp32.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-11  |  13.3 KB  |  316 lines

  1. /********************************************************************\
  2. *  Module: UTSamp32.c                                                *
  3. *                                                                    *
  4. *  Comments: Source for utsamp32.dll                                 *
  5. *                                                                    *                                                                    *
  6. *  Functions:                                                        *
  7. *                                                                    *
  8. *  DllMain()          - DLL entry point                              *
  9. *  MyGetFreeSpace()   - Get the amount of free physical memory       *
  10. *  MyWNetGetUser()    - Get the default user name                    *
  11. *  My32CallBack()     - Dispatch function for callbacks              *
  12. *                                                                    *
  13. *                                                                    *
  14. \********************************************************************/
  15.  
  16. /*********************  Header Files  *********************/
  17.  
  18. #define W32SUT_32
  19.  
  20. #include <windows.h>
  21. #include "w32sut.h"
  22. #include "utsamp.h"
  23.  
  24. /*********************   Prototypes   *********************/
  25.  
  26. DWORD APIENTRY My32Callback (LPVOID, DWORD );
  27. void APIENTRY MyFunc1( void );
  28. void APIENTRY MyFunc2( void );
  29.  
  30. /*********************   Type Defs    *********************/
  31.  
  32. typedef BOOL (WINAPI * PUTREGISTER) ( HANDLE     hModule,
  33.                       LPCSTR     lpsz16BitDLL,
  34.                       LPCSTR     lpszInitName,
  35.                       LPCSTR     lpszProcName,
  36.                       UT32PROC * ppfn32Thunk,
  37.                       FARPROC    pfnUT32Callback,
  38.                       LPVOID     lpBuff
  39.                     );
  40.  
  41.  
  42. typedef VOID (WINAPI * PUTUNREGISTER) (HANDLE hModule);
  43.  
  44. typedef DWORD (APIENTRY * PUT32CBPROC) (LPVOID lpBuff, DWORD dwUserDefined );
  45.  
  46. /*********************     Globals    *********************/
  47.  
  48. UT32PROC      pfnUTProc = NULL;
  49. PUTREGISTER   pUTRegister = NULL;
  50. PUTUNREGISTER pUTUnRegister = NULL;
  51. PUT32CBPROC   pfnUT32CBProc = &My32Callback;
  52. int           cProcessesAttached = 0;
  53. BOOL          fWin32s = FALSE;
  54. HANDLE        hKernel32 = 0;
  55.  
  56. /********************************************************************\
  57. * Function: BOOL APIENTRY DllMain(HANDLE, DWORD, LPVOID)             *
  58. *                                                                    *
  59. *  Purpose: DLL entry point                                          *
  60. *                                                                    *
  61. * Comments: Detect whether or not we are running under Win32s. If    *
  62. *           we are running under Win32s, get the address of          *
  63. *           UTRegister() and UTUnRegister(). Call UTRegister() to    *
  64. *           set up the thunk when this DLL is loaded. Call           *
  65. *           UTUnRegister() to destroy the thunk when this DLL is     *
  66. *           unloaded.                                                *
  67. *                                                                    *
  68. *                                                                    *
  69. \********************************************************************/
  70.  
  71. BOOL APIENTRY DllMain(HANDLE hInst, DWORD fdwReason, LPVOID lpReserved)
  72. {
  73.    DWORD dwVersion;
  74.  
  75.    if ( fdwReason == DLL_PROCESS_ATTACH )
  76.    {
  77.  
  78.     /*
  79.      * Registration of UT need to be done only once for first
  80.      * attaching process.  At that time set the fWin32s flag
  81.      * to indicate if the DLL is executing under Win32s or not.
  82.      */
  83.  
  84.       if( cProcessesAttached++ )
  85.       {
  86.          return(TRUE);         // Not the first initialization.
  87.       }
  88.  
  89.       // Find out if we're running on Win32s
  90.       dwVersion = GetVersion();
  91.       fWin32s = (BOOL) (!(dwVersion < 0x80000000))
  92.                         && (LOBYTE(LOWORD(dwVersion)) < 4);
  93.  
  94.       if( !fWin32s )
  95.          return(TRUE);         // Not on Win32s - no further initialization needed
  96.  
  97.       hKernel32 = LoadLibrary( "Kernel32.Dll" ); // Get Handle to Kernel32.Dll
  98.  
  99.       pUTRegister = (PUTREGISTER) GetProcAddress( hKernel32, "UTRegister" );
  100.  
  101.       if( !pUTRegister )
  102.          return(FALSE);        // Error - On Win32s, but can't find UTRegister
  103.  
  104.       pUTUnRegister = (PUTUNREGISTER) GetProcAddress( hKernel32, "UTUnRegister" );
  105.  
  106.       if( !pUTUnRegister )
  107.          return(FALSE);        // Error - On Win32s, but can't find UTUnRegister
  108.  
  109.       return (*pUTRegister)( hInst,           // UTSamp32.DLL module handle
  110.                    "UTSAMP16.DLL",  // name of 16-bit thunk dll
  111.                  "UTInit",          // name of init routine
  112.                  "UTProc",        // name of 16-bit dispatch routine (in utsamp16)
  113.                  &pfnUTProc,      // Global variable to receive thunk address
  114.                  pfnUT32CBProc,   // callback function
  115.                  NULL );          // no shared memroy
  116.  
  117.    }
  118.    if( (fdwReason == DLL_PROCESS_DETACH) && (0 == --cProcessesAttached) && fWin32s )
  119.    {
  120.       (*pUTUnRegister)( hInst );
  121.       FreeLibrary( hKernel32 );
  122.    }
  123. } // DllMain()
  124.  
  125. /********************************************************************\
  126. * Function: DWORD APIENTRY MyGetFreeSpace(void)                      *
  127. *                                                                    *
  128. *  Purpose: Get the amount of free physical memory.                  *
  129. *                                                                    *
  130. * Comments: Call GlobalMemoryStatus(). If this is Win32s, this       *
  131. *           returns ERROR_CALL_NOT_IMPLEMENTED. In this case, use    *
  132. *           the UT entrypoint, passing it MYGETFREESPACE.            *
  133. *                                                                    *
  134. *                                                                    *
  135. \********************************************************************/
  136.  
  137. DWORD APIENTRY MyGetFreeSpace(void)
  138. {
  139.    MEMORYSTATUS MemoryStatus;
  140.  
  141.   /*
  142.    * GlobalMemoryStatus() does not have a return value. Check for failure
  143.    * by calling SetLastError() with NO_ERROR, call GlobalMemoryStatus(),
  144.    * then call GetLastError() to see if ERROR_CALL_NOT_IMPLEMENTED was
  145.    * returned.
  146.    */
  147.  
  148.    memset( &MemoryStatus, sizeof(MEMORYSTATUS), 0);
  149.    SetLastError( NO_ERROR );
  150.    GlobalMemoryStatus( &MemoryStatus );
  151.  
  152.    if( ERROR_CALL_NOT_IMPLEMENTED == GetLastError() && fWin32s )
  153.       return( (* pfnUTProc)( NULL, MYGETFREESPACE, NULL ) );
  154.    else
  155.       return( MemoryStatus.dwAvailPhys );
  156. } // MyGetFreeSpace()
  157.  
  158. /********************************************************************\
  159. * Function: DWORD APIENTRY MyWNetGetUser(LPTSTR, LPTSTR, LPDWORD)    *
  160. *                                                                    *
  161. *  Purpose: Get the default user name.                               *
  162. *                                                                    *
  163. * Comments: Call WNetGetUser(). If this Win32s, this returns         *
  164. *           ERROR_CALL_NOT_IMPLEMENTED. In this case, use the UT     *
  165. *           entrypoint, passing it MYWNETGETUSER.                    *
  166. *                                                                    *
  167. *           Win16 WNetGetUser() does not support lpszLocalName, so   *
  168. *           the 1st parameter is ignored when this function is       *
  169. *           called under Win32s.                                     *
  170. *                                                                    *
  171. *                                                                    *
  172. \********************************************************************/
  173.  
  174. DWORD APIENTRY MyWNetGetUser(LPTSTR lpszLocalName, LPTSTR lpszUserName, LPDWORD lpcchBuffer)
  175. {
  176.    DWORD Args[2];
  177.    PVOID TransList[3];
  178.    DWORD retval;
  179.  
  180.   /*
  181.    * Win16 uses several different return codes to indicate different errors,
  182.    * while Win32 uses only a few return codes to indicate error, one of which
  183.    * indicates that WNetGetLastError() should be called for more detailed error
  184.    * information.
  185.    *
  186.    * This function just maps all Win16 error codes into the Win32 error
  187.    * ERROR_NO_NETWORK.  A more complete solution would be to switch on the
  188.    * various return codes possible from WNetGetUser (both Win16 & Win32) and
  189.    * to set appropriate SetLastError values.
  190.    *
  191.    */
  192.  
  193.  
  194.    if( ERROR_CALL_NOT_IMPLEMENTED ==
  195.        (retval = WNetGetUser( lpszLocalName, lpszUserName, lpcchBuffer )))
  196.    {
  197.  
  198.     /*
  199.      * The parameters being passed include pointers that need to be
  200.      * translated, using the Translation List.
  201.      *
  202.      */
  203.  
  204.       Args[0] = (DWORD)lpszUserName; // Buffer to receive UserName
  205.       Args[1] = (DWORD)lpcchBuffer;  // Pointer to a DWord containing the size
  206.                                     // of this buffer on entry.  On return it will
  207.                      // have been replaced with the size of the
  208.                      // returned text.  Since the Win16 API only
  209.                      // changes the low 16 bits, we need to zero out
  210.                      // the high 16 bits before calling the API.
  211.  
  212.       if ((*lpcchBuffer) > 0xFFFF)   // The buffer could be 64K or larger, but
  213.       {                              // since the API only looks at the lower
  214.          *lpcchBuffer = 0xFFFF;      // 16 bits passed, we need to make sure the
  215.       }                              // buffer is not too large.
  216.  
  217.       TransList[0] = & Args[0];      // Translate the two pointes passed in Args[]
  218.       TransList[1] = & Args[1];      // from 0:32 to 16:16.
  219.       TransList[2] = NULL;           // End of list of pointers to translate
  220.  
  221.       retval = (* pfnUTProc)(Args, MYWNETGETUSER, TransList);
  222.  
  223.       if( retval)                    // WN_SUCCESS == NO_ERROR == 0.  All others
  224.          retval = ERROR_NO_NETWORK;  // return codes are Win16 error codes that
  225.                      // we map to ERROR_NO_NETWORK.
  226.    }
  227.  
  228.    return (retval);
  229. } //MyWNetGetUser()
  230.  
  231. /********************************************************************\
  232. * Function: void APIENTRY GenerateCallback(DWORD)                    *
  233. *                                                                    *
  234. *  Purpose: Dummy routine which causes 16-bit side to make a         *
  235. *           callback.                                                *
  236. *                                                                    *
  237. * Comments: This wouldn't be needed in an application - this is just *
  238. *           a simple demo which was requested by customers after     *
  239. *           using the original UTSample.                             *
  240. *                                                                    *
  241. *                                                                    *
  242. \********************************************************************/
  243.  
  244. void APIENTRY GenerateCallback( DWORD dwFunc )
  245. {
  246.    if( fWin32s ) {
  247.       if( dwFunc == 1 )
  248.          (* pfnUTProc)( &dwFunc, MYCALLBACK1, NULL );
  249.       else
  250.          (* pfnUTProc)( &dwFunc, MYCALLBACK2, NULL );
  251.    }
  252.    else {
  253.       if( dwFunc == 1 )
  254.          MyFunc1();
  255.       else
  256.          MyFunc2();
  257.    }
  258. } // GenerateCallback()
  259.  
  260. /********************************************************************\
  261. * Function: DWORD APIENTRY My32Callback(LPVOID, DWORD)               *
  262. *                                                                    *
  263. *  Purpose: Dispatch function for callbacks                          *
  264. *                                                                    *
  265. * Comments: Simple code which is part of showing how callbacks from  *
  266. *           the 16-bit code to the 32-bit code that loaded it works. *
  267. *                                                                    *
  268. *                                                                    *
  269. \********************************************************************/
  270.  
  271. DWORD APIENTRY My32Callback( LPVOID lpBuff, DWORD dwFunc )
  272. {
  273.    switch( dwFunc )
  274.    {
  275.       case MYFUNC1:
  276.      MyFunc1();
  277.      break;
  278.       case MYFUNC2:
  279.      MyFunc2();
  280.      break;
  281.       default:
  282.      return( 0 );
  283.    }
  284.    return( 1 );
  285. } // My32Callback()
  286.  
  287. /********************************************************************\
  288. * Function: void APIENTRY MyFunc1(void)                              *
  289. *                                                                    *
  290. *  Purpose: To be called from 16-bit side via Reverse UT             *
  291. *                                                                    *
  292. * Comments: Does nothing at this point                               *
  293. *                                                                    *
  294. *                                                                    *
  295. \********************************************************************/
  296.  
  297. void APIENTRY MyFunc1( )
  298. {
  299.    MessageBox( NULL, "Inside MyCallback1", "UTSamp32", MB_OK );
  300. } // MyCallback1()
  301.  
  302. /********************************************************************\
  303. * Function: void APIENTRY MyFunc2(void)                              *
  304. *                                                                    *
  305. *  Purpose: To be called from 16-bit side via Reverse UT             *
  306. *                                                                    *
  307. * Comments: Does nothing at this point                               *
  308. *                                                                    *
  309. *                                                                    *
  310. \********************************************************************/
  311.  
  312. void APIENTRY MyFunc2( )
  313. {
  314.    MessageBox( NULL, "Inside MyCallback2", "UTSamp32", MB_OK );
  315. } // MyCallback2()
  316.