home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
- File apigid32.cpp
-
- APIGid32 main program file for Visual Basic Programmer's Guide to Win32 API
-
- Written By Daniel Appleman
- Copyright (c) 1996 by Desaware - All rights reserved
-
- You have a royalty-free right to use, modify, reproduce and distribute
- this file (and/or any modified versions) in any way you find useful,
- provided that you agree that Desaware and Ziff-Davis Press has no warranty,
- obligation or liability for its contents, and that you
- include an acknowledgement identifying those portions of
- the code that were authored by Desaware. You are also required
- to change the name of the file before distributing modified
- versions of these files.
-
- This file is provided "As-Is". No support is provided for the contents
- or use of this file by Ziff-Davis Press, Daniel Appleman or Desaware.
-
- Refer to the Ziff-Davis Visual Basic Programmer's Guide to the
- Win32 API for further information.
-
-
-
- ************************************************************************/
-
- #include "stdafx.h"
- #include "apigid32.h"
-
- #ifdef _DEBUG
- #undef THIS_FILE
- static char BASED_CODE THIS_FILE[] = __FILE__;
- #endif
-
- /************** Global Variables *****************/
- HMODULE LibHandle; // Contains module handle for library
-
- const GUID CDECL BASED_CODE _tlid =
- { 0x59BACB70, 0x7EA1, 0x11CE, { 0x86, 0x32, 0x52, 0x41, 0x53, 0x48, 0x0, 0x0 } };
-
-
- /////////////////////////////////////////////////////////////////////////////
- // The one and only CApigid32App object
-
- CApigid32App theApp;
-
- /////////////////////////////////////////////////////////////////////////////
- // CApigid32App
-
- BEGIN_MESSAGE_MAP(CApigid32App, CWinApp)
- //{{AFX_MSG_MAP(CApigid32App)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- // DO NOT EDIT what you see in these blocks of generated code!
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CApigid32App construction
-
- CApigid32App::CApigid32App()
- {
- LibHandle = m_hInstance;
-
- }
-
-
-
- // Return the instance handle for this application
-
- STDAPI_(HANDLE) agGetInstance()
- {
- return(GetModuleHandle(NULL));
- }
-
- STDAPI_(HANDLE) agGetWndInstance(HWND hwnd)
- {
- return((HANDLE)GetWindowLong(hwnd, GWL_HINSTANCE));
- }
-
-
- // Get a pointer to the string data area for a VB string
- STDAPI_(LONG) agGetAddressForVBString(BSTR *pstr)
- {
- return((LONG)*pstr);
- }
-
- // Get a VB string from a text pointer
- STDAPI_(BSTR) agGetStringFromLPSTR(LPSTR tptr)
- {
- BSTR rptr;
- char tbuf = '\0';
- if(!tptr) {
- rptr = SysAllocString((OLECHAR *)&tbuf);
- }
- else {
- // Why do it by length? Because SysAllocString looks for a double
- // null - which may not be present
- rptr = SysAllocStringByteLen(tptr, lstrlen(tptr));
- }
- return(rptr);
- }
-
-
- // Get the address of an object
- STDAPI_(LONG) agGetAddressForObject(LPVOID lpobject)
- {
- return((LONG)lpobject);
- }
-
-
- // Swap the order of bytes in a word
- STDAPI_(WORD) agSwapBytes(WORD sb)
- {
- return(((sb << 8) & 0xff00) | ((sb >> 8) & 0x0ff) );
- }
-
- STDAPI_(DWORD) agSwapWords(DWORD sb)
- {
- return( ((agSwapBytes(LOWORD(sb)) << 16) & 0xffff0000) |
- (agSwapBytes(HIWORD(sb)) & 0x0ffff) );
- }
-
-
- // Copy a block of data from source to destination
- STDAPI_(VOID) agCopyData(LPVOID source, LPVOID dest, DWORD size)
- {
- memmove(dest, source, size);
- }
-
-
- STDAPI_(VOID) agDWORDto2Integers(LONG l, LPWORD x, LPWORD y)
- {
- *x = (WORD)(l & 0xffff);
- *y = (WORD)(l >> 16);
- return;
- }
-
- STDAPI_(DWORD) agPOINTStoLong(POINTS *pt)
- {
- return(MAKELONG(pt->x, pt->y));
- }
-
- STDAPI_(BSTR) agGetStringFrom2NullBuffer(LPSTR tptr)
- {
- BSTR rptr;
- LPSTR nptr;
- char tbuf = '\0';
- if(!tptr) {
- rptr = SysAllocString((OLECHAR *)&tbuf);
- return(rptr);
- }
-
- nptr = tptr;
- while(*nptr || *(nptr+1)) nptr++;
-
- rptr = SysAllocStringByteLen(tptr, (nptr-tptr));
- return(rptr);
- }
-
-
- STDAPI_(DWORD) agMakeROP4(DWORD foreground, DWORD background)
- {
- return(MAKEROP4(foreground, background));
- }
-
-
- STDAPI_(BOOL) agIsValidName(LPUNKNOWN lpv, LPSTR lpname)
- {
- HRESULT hr;
- LPDISPATCH lpd;
- BOOL isvalid = FALSE;
- OLECHAR *tptr;
- DISPID dispid;
- if(!lpname || !lpv) return(FALSE);
- WORD tptrlen = lstrlen(lpname) * 2;
- tptr = new OLECHAR[tptrlen];
- MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpname, -1, tptr, tptrlen);
- hr = lpv->QueryInterface(IID_IDispatch, (LPVOID FAR *)&lpd);
- if(SUCCEEDED(hr) && lpd) {
- hr = lpd->GetIDsOfNames(IID_NULL, &tptr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
- if(SUCCEEDED(hr) && dispid!=DISPID_UNKNOWN) {
- isvalid = -1;
- }
- lpd->Release();
- }
- return(isvalid);
- }
-
- STDAPI_(VOID) agNegateFileTime(FILETIME *f1)
- {
- if(!f1) return;
- FILETIME addone;
- FILETIME tempft;
- addone.dwLowDateTime = 1;
- addone.dwHighDateTime = 0;
- tempft = *f1;
- f1->dwLowDateTime = ~tempft.dwLowDateTime;
- f1->dwHighDateTime = ~tempft.dwHighDateTime;
- // This does the negation
- agAddFileTimes(f1, &addone, &tempft);
- *f1 = tempft;
- }
-
- STDAPI_(VOID) agSubtractFileTimes(FILETIME *f1, FILETIME *f2, FILETIME *f3)
- {
- FILETIME tempft;
- if(!f1 || !f2 || !f3) return;
- // Change f2 to -f2 (we substract by adding the negative)
- tempft = *f2;
- agNegateFileTime(&tempft);
- // This does the addition
- agAddFileTimes(f1, &tempft, f3);
- }
-
-
- STDAPI_(VOID) agAddFileTimes(FILETIME *f1, FILETIME *f2, FILETIME *f3)
- {
- if(!f1 || !f2 || !f3) return;
- DWORD carry =0;
- f3->dwLowDateTime = f1->dwLowDateTime + f2->dwLowDateTime;
- if( (f1->dwLowDateTime & 0x80000000) || (f2->dwLowDateTime & 0x80000000)) {
- if( !(f3->dwLowDateTime & 0x80000000)) carry = 1;
- }
- f3->dwHighDateTime = f1->dwHighDateTime + f2->dwHighDateTime + carry;
- }
-
- STDAPI_(double) agConvertFileTimeToDouble(FILETIME *f1)
- {
- double dbl;
- double factor;
- BOOL negate = FALSE;
- FILETIME tempft;
-
- if(!f1) return(0);
-
- factor = 0x40000000;
- factor = factor * 4;
-
- tempft = *f1;
-
- if(f1->dwHighDateTime & 0x80000000) {
- negate = TRUE;
- agNegateFileTime(&tempft);
- }
- // We're dealing only with positive values now
- dbl = tempft.dwHighDateTime;
- dbl *= factor;
- dbl+= tempft.dwLowDateTime;
- if(negate) dbl = -dbl;
- return(dbl);
- }
-
- STDAPI_(VOID) agConvertDoubleToFileTime(double dbl, FILETIME *f1)
- {
- double factor;
- double intpart, fracpart;
- double tval;
- BOOL negate = FALSE;
- if(!f1) return;
- factor = 0x40000000;
- factor = factor * 4;
- tval = dbl;
- if (tval<0) {
- tval = -tval;
- negate = TRUE;
- }
-
- intpart = tval/factor;
- f1->dwHighDateTime = (long)intpart;
- fracpart = tval/factor - (double)f1->dwHighDateTime;
- fracpart = fracpart * factor;
- f1->dwLowDateTime = (long)fracpart;
- if(negate) agNegateFileTime(f1);
-
- }
-
-
- STDAPI_(short) agInp(WORD portid)
- {
- short res;
- __asm {
- push dx
- mov dx, portid
- in al,dx
- mov res,ax
- pop dx
- }
- return(res & 0x0ff);
- }
-
- STDAPI_(short) agInpw(WORD portid)
- {
- short res;
- __asm {
- push dx
- mov dx, portid
- in ax,dx
- mov res,ax
- pop dx
- }
- return(res);
-
- }
-
- STDAPI_(DWORD) agInpd(WORD portid)
- {
- DWORD res;
- __asm {
- push dx
- mov dx, portid
- in eax,dx
- mov res,eax
- pop dx
- }
- return(res);
- }
-
- STDAPI_(VOID) agOutp(WORD portid, WORD val)
- {
- BYTE v;
- v = val & 0x0ff;
- __asm {
- push dx
- mov dx, portid
- mov al, v
- out dx,al
- pop dx
- }
- }
-
- STDAPI_(VOID) agOutpw(WORD portid, WORD val)
- {
- __asm {
- push dx
- mov dx, portid
- mov ax, val
- out dx,ax
- pop dx
- }
- }
-
- STDAPI_(VOID) agOutpd(WORD portid, DWORD val)
- {
- __asm {
- push dx
- mov dx, portid
- mov eax, val
- out dx,eax
- pop dx
- }
- }
-
-
-
-
-