home *** CD-ROM | disk | FTP | other *** search
/ Dan Appleman's Visual Bas…s Guide to the Win32 API / Dan.Applmans.Visual.Basic.5.0.Programmers.Guide.To.The.Win32.API.1997.Ziff-Davis.Press.CD / VB5PG32.mdf / vbpg32 / apigid32 / apigid32.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-28  |  7.7 KB  |  357 lines

  1. /***********************************************************************
  2.    File apigid32.cpp
  3.  
  4.    APIGid32 main program file for Visual Basic Programmer's Guide to Win32 API
  5.  
  6.    Written By Daniel Appleman
  7.    Copyright (c) 1996 by Desaware - All rights reserved
  8.  
  9.     You have a royalty-free right to use, modify, reproduce and distribute
  10.     this file (and/or any modified versions) in any way you find useful,
  11.     provided that you agree that Desaware and Ziff-Davis Press has no warranty, 
  12.     obligation or liability for its contents, and that you 
  13.     include an acknowledgement identifying those portions of
  14.     the code that were authored by Desaware. You are also required
  15.     to change the name of the file before distributing modified
  16.     versions of these files. 
  17.   
  18.    This file is provided "As-Is". No support is provided for the contents
  19.    or use of this file by Ziff-Davis Press, Daniel Appleman or Desaware.
  20.  
  21.    Refer to the Ziff-Davis Visual Basic Programmer's Guide to the
  22.    Win32 API for further information.
  23.  
  24.  
  25.  
  26. ************************************************************************/
  27.  
  28. #include "stdafx.h"
  29. #include "apigid32.h"
  30.  
  31. #ifdef _DEBUG
  32. #undef THIS_FILE
  33. static char BASED_CODE THIS_FILE[] = __FILE__;
  34. #endif
  35.  
  36. /**************  Global Variables  *****************/
  37. HMODULE LibHandle;    // Contains module handle for library
  38.  
  39. const GUID CDECL BASED_CODE _tlid =
  40.         { 0x59BACB70, 0x7EA1, 0x11CE, { 0x86, 0x32, 0x52, 0x41, 0x53, 0x48, 0x0, 0x0 } };
  41.         
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // The one and only CApigid32App object
  45.  
  46. CApigid32App theApp;
  47.  
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CApigid32App
  50.  
  51. BEGIN_MESSAGE_MAP(CApigid32App, CWinApp)
  52.     //{{AFX_MSG_MAP(CApigid32App)
  53.         // NOTE - the ClassWizard will add and remove mapping macros here.
  54.         //    DO NOT EDIT what you see in these blocks of generated code!
  55.     //}}AFX_MSG_MAP
  56. END_MESSAGE_MAP()
  57.  
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CApigid32App construction
  60.  
  61. CApigid32App::CApigid32App()
  62. {
  63.     LibHandle = m_hInstance;
  64.  
  65. }
  66.  
  67.  
  68.  
  69. // Return the instance handle for this application
  70.  
  71. STDAPI_(HANDLE) agGetInstance()
  72. {
  73.    return(GetModuleHandle(NULL));
  74. }
  75.  
  76. STDAPI_(HANDLE) agGetWndInstance(HWND hwnd)
  77. {
  78.    return((HANDLE)GetWindowLong(hwnd, GWL_HINSTANCE));
  79. }
  80.  
  81.  
  82. // Get a pointer to the string data area for a VB string
  83. STDAPI_(LONG) agGetAddressForVBString(BSTR *pstr)
  84. {
  85.    return((LONG)*pstr);
  86. }
  87.  
  88. // Get a VB string from a text pointer
  89. STDAPI_(BSTR) agGetStringFromLPSTR(LPSTR tptr)
  90. {
  91.     BSTR rptr;
  92.    char tbuf = '\0';
  93.    if(!tptr) {
  94.       rptr = SysAllocString((OLECHAR *)&tbuf);
  95.       }
  96.    else {
  97.         // Why do it by length? Because SysAllocString looks for a double
  98.         // null - which may not be present
  99.       rptr = SysAllocStringByteLen(tptr, lstrlen(tptr));
  100.       }
  101.    return(rptr);
  102. }
  103.  
  104.  
  105. // Get the address of an object
  106. STDAPI_(LONG) agGetAddressForObject(LPVOID lpobject)
  107. {
  108.    return((LONG)lpobject);
  109. }
  110.  
  111.  
  112. // Swap the order of bytes in a word
  113. STDAPI_(WORD) agSwapBytes(WORD sb)
  114. {
  115.     return(((sb << 8) & 0xff00) | ((sb >> 8)  & 0x0ff) );
  116. }
  117.  
  118. STDAPI_(DWORD) agSwapWords(DWORD sb)
  119. {
  120.     return( ((agSwapBytes(LOWORD(sb)) << 16) & 0xffff0000) |
  121.             (agSwapBytes(HIWORD(sb)) & 0x0ffff) );
  122. }
  123.  
  124.  
  125. // Copy a block of data from source to destination
  126. STDAPI_(VOID) agCopyData(LPVOID source, LPVOID dest, DWORD size)
  127. {
  128.    memmove(dest, source, size);
  129. }
  130.  
  131.  
  132. STDAPI_(VOID) agDWORDto2Integers(LONG l, LPWORD x, LPWORD y)
  133. {
  134.    *x = (WORD)(l & 0xffff);
  135.    *y = (WORD)(l >> 16);
  136.    return;
  137. }
  138.  
  139. STDAPI_(DWORD) agPOINTStoLong(POINTS *pt)
  140. {
  141.      return(MAKELONG(pt->x, pt->y));
  142. }
  143.  
  144. STDAPI_(BSTR) agGetStringFrom2NullBuffer(LPSTR tptr)
  145. {
  146.     BSTR rptr;
  147.     LPSTR nptr;
  148.    char tbuf = '\0';
  149.    if(!tptr) {
  150.       rptr = SysAllocString((OLECHAR *)&tbuf);
  151.         return(rptr);
  152.       }
  153.  
  154.     nptr = tptr;
  155.     while(*nptr || *(nptr+1)) nptr++;
  156.  
  157.    rptr = SysAllocStringByteLen(tptr, (nptr-tptr));
  158.    return(rptr);
  159. }    
  160.  
  161.  
  162. STDAPI_(DWORD) agMakeROP4(DWORD foreground, DWORD background)
  163. {
  164.     return(MAKEROP4(foreground, background));
  165. }
  166.  
  167.  
  168. STDAPI_(BOOL) agIsValidName(LPUNKNOWN lpv, LPSTR lpname)
  169. {
  170.     HRESULT hr;
  171.     LPDISPATCH lpd;
  172.     BOOL isvalid = FALSE;
  173.     OLECHAR *tptr;
  174.     DISPID dispid;
  175.     if(!lpname || !lpv) return(FALSE);
  176.     WORD tptrlen = lstrlen(lpname) * 2;
  177.     tptr = new OLECHAR[tptrlen];
  178.     MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpname, -1, tptr, tptrlen);
  179.     hr = lpv->QueryInterface(IID_IDispatch, (LPVOID FAR *)&lpd);
  180.     if(SUCCEEDED(hr) && lpd) {
  181.         hr = lpd->GetIDsOfNames(IID_NULL, &tptr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
  182.         if(SUCCEEDED(hr) && dispid!=DISPID_UNKNOWN) {
  183.             isvalid = -1;
  184.             }
  185.         lpd->Release();
  186.         }
  187.     return(isvalid);
  188. }
  189.  
  190. STDAPI_(VOID) agNegateFileTime(FILETIME *f1)
  191. {
  192.     if(!f1) return;
  193.     FILETIME addone;
  194.     FILETIME tempft;
  195.     addone.dwLowDateTime = 1;
  196.     addone.dwHighDateTime = 0;
  197.     tempft = *f1;
  198.     f1->dwLowDateTime = ~tempft.dwLowDateTime;
  199.     f1->dwHighDateTime = ~tempft.dwHighDateTime;
  200.     // This does the negation
  201.     agAddFileTimes(f1, &addone, &tempft);
  202.     *f1 = tempft;
  203. }
  204.  
  205. STDAPI_(VOID) agSubtractFileTimes(FILETIME *f1, FILETIME *f2, FILETIME *f3)
  206. {
  207.     FILETIME tempft;
  208.     if(!f1 || !f2 || !f3) return;
  209.     // Change f2 to -f2 (we substract by adding the negative)
  210.     tempft = *f2;
  211.     agNegateFileTime(&tempft);
  212.     // This does the addition
  213.     agAddFileTimes(f1, &tempft, f3);
  214. }
  215.  
  216.  
  217. STDAPI_(VOID) agAddFileTimes(FILETIME *f1, FILETIME *f2, FILETIME *f3)
  218. {
  219.     if(!f1 || !f2 || !f3) return;
  220.     DWORD carry =0;
  221.     f3->dwLowDateTime = f1->dwLowDateTime + f2->dwLowDateTime;
  222.     if( (f1->dwLowDateTime    & 0x80000000) || (f2->dwLowDateTime & 0x80000000)) {
  223.         if( !(f3->dwLowDateTime & 0x80000000)) carry = 1;
  224.         }
  225.     f3->dwHighDateTime = f1->dwHighDateTime + f2->dwHighDateTime + carry;
  226. }
  227.  
  228. STDAPI_(double) agConvertFileTimeToDouble(FILETIME *f1)
  229. {
  230.     double dbl;
  231.     double factor;
  232.     BOOL negate = FALSE;
  233.     FILETIME tempft;
  234.  
  235.     if(!f1) return(0);
  236.  
  237.     factor = 0x40000000;
  238.     factor = factor * 4;
  239.  
  240.     tempft = *f1;
  241.  
  242.     if(f1->dwHighDateTime & 0x80000000) {
  243.         negate = TRUE;
  244.         agNegateFileTime(&tempft);
  245.         }
  246.     // We're dealing only with positive values now
  247.     dbl = tempft.dwHighDateTime;
  248.     dbl *= factor;
  249.     dbl+= tempft.dwLowDateTime;
  250.     if(negate) dbl = -dbl;
  251.     return(dbl);
  252. }
  253.  
  254. STDAPI_(VOID) agConvertDoubleToFileTime(double dbl, FILETIME *f1)
  255. {
  256.     double factor;
  257.     double intpart, fracpart;
  258.     double tval;
  259.     BOOL negate = FALSE;
  260.     if(!f1) return;
  261.     factor = 0x40000000;
  262.     factor = factor * 4;
  263.     tval = dbl;
  264.     if (tval<0) {
  265.         tval = -tval;
  266.         negate = TRUE;
  267.         }
  268.  
  269.     intpart = tval/factor;
  270.     f1->dwHighDateTime = (long)intpart;
  271.     fracpart = tval/factor - (double)f1->dwHighDateTime;
  272.     fracpart = fracpart * factor;
  273.     f1->dwLowDateTime = (long)fracpart;
  274.     if(negate) agNegateFileTime(f1);        
  275.     
  276. }
  277.  
  278.  
  279. STDAPI_(short) agInp(WORD portid)
  280. {
  281.     short res;
  282.        __asm {
  283.                push dx
  284.             mov dx, portid
  285.             in al,dx
  286.             mov res,ax
  287.             pop dx
  288.         }
  289.     return(res & 0x0ff);
  290. }
  291.  
  292. STDAPI_(short) agInpw(WORD portid)
  293. {
  294.     short res;
  295.        __asm {
  296.                push dx
  297.             mov dx, portid
  298.             in ax,dx
  299.             mov res,ax
  300.             pop dx
  301.         }
  302.     return(res);
  303.  
  304. }
  305.  
  306. STDAPI_(DWORD) agInpd(WORD portid)
  307. {
  308.     DWORD res;
  309.        __asm {
  310.                push dx
  311.             mov dx, portid
  312.             in eax,dx
  313.             mov res,eax
  314.             pop dx
  315.         }
  316.     return(res);
  317. }
  318.  
  319. STDAPI_(VOID) agOutp(WORD portid, WORD val)
  320. {
  321.        BYTE v;
  322.     v = val & 0x0ff;
  323.        __asm {
  324.                push dx
  325.             mov dx, portid
  326.             mov al, v
  327.             out dx,al
  328.             pop dx
  329.         }
  330. }
  331.  
  332. STDAPI_(VOID) agOutpw(WORD portid, WORD val)
  333. {
  334.        __asm {
  335.                push dx
  336.             mov dx, portid
  337.             mov ax, val
  338.             out dx,ax
  339.             pop dx
  340.         }
  341. }
  342.  
  343. STDAPI_(VOID) agOutpd(WORD portid, DWORD val)
  344. {
  345.        __asm {
  346.                push dx
  347.             mov dx, portid
  348.             mov eax, val
  349.             out dx,eax
  350.             pop dx
  351.         }
  352. }
  353.  
  354.  
  355.  
  356.  
  357.