⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 common.cpp

📁 早期的WINDOWS编程,适合刚接触WINDOWS编程的初学者
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	CASE_SCODE(STG_E_UNKNOWN)
	CASE_SCODE(STG_E_UNIMPLEMENTEDFUNCTION)
	CASE_SCODE(STG_E_INVALIDFLAG)
	CASE_SCODE(STG_E_INUSE)
	CASE_SCODE(STG_E_NOTCURRENT)
	CASE_SCODE(STG_E_REVERTED)
	CASE_SCODE(STG_E_CANTSAVE)
	CASE_SCODE(STG_E_OLDFORMAT)
	CASE_SCODE(STG_E_OLDDLL)
	CASE_SCODE(STG_E_SHAREREQUIRED)
	CASE_SCODE(STG_E_NOTFILEBASEDSTORAGE)
	CASE_SCODE(STG_E_EXTANTMARSHALLINGS)
	CASE_SCODE(STG_S_CONVERTED)

	CASE_SCODE(DISP_E_UNKNOWNINTERFACE)
	CASE_SCODE(DISP_E_MEMBERNOTFOUND)
	CASE_SCODE(DISP_E_PARAMNOTFOUND)
	CASE_SCODE(DISP_E_TYPEMISMATCH)
	CASE_SCODE(DISP_E_UNKNOWNNAME)
	CASE_SCODE(DISP_E_NONAMEDARGS)
	CASE_SCODE(DISP_E_BADVARTYPE)
	CASE_SCODE(DISP_E_EXCEPTION)
	CASE_SCODE(DISP_E_OVERFLOW)
	CASE_SCODE(DISP_E_BADINDEX)
	CASE_SCODE(DISP_E_UNKNOWNLCID)
	CASE_SCODE(DISP_E_ARRAYISLOCKED)
	CASE_SCODE(DISP_E_BADPARAMCOUNT)
	CASE_SCODE(DISP_E_PARAMNOTOPTIONAL)
	CASE_SCODE(DISP_E_BADCALLEE)
	CASE_SCODE(DISP_E_NOTACOLLECTION)

	CASE_SCODE(TYPE_E_BUFFERTOOSMALL)
	CASE_SCODE(TYPE_E_INVDATAREAD)
	CASE_SCODE(TYPE_E_UNSUPFORMAT)
	CASE_SCODE(TYPE_E_REGISTRYACCESS)
	CASE_SCODE(TYPE_E_LIBNOTREGISTERED)
	CASE_SCODE(TYPE_E_UNDEFINEDTYPE)
	CASE_SCODE(TYPE_E_QUALIFIEDNAMEDISALLOWED)
	CASE_SCODE(TYPE_E_INVALIDSTATE)
	CASE_SCODE(TYPE_E_WRONGTYPEKIND)
	CASE_SCODE(TYPE_E_ELEMENTNOTFOUND)
	CASE_SCODE(TYPE_E_AMBIGUOUSNAME)
	CASE_SCODE(TYPE_E_NAMECONFLICT)
	CASE_SCODE(TYPE_E_UNKNOWNLCID)
	CASE_SCODE(TYPE_E_DLLFUNCTIONNOTFOUND)
	CASE_SCODE(TYPE_E_BADMODULEKIND)
	CASE_SCODE(TYPE_E_SIZETOOBIG)
	CASE_SCODE(TYPE_E_DUPLICATEID)
	CASE_SCODE(TYPE_E_TYPEMISMATCH)
	CASE_SCODE(TYPE_E_OUTOFBOUNDS)
	CASE_SCODE(TYPE_E_IOERROR)
	CASE_SCODE(TYPE_E_CANTCREATETMPFILE)
	CASE_SCODE(TYPE_E_CANTLOADLIBRARY)
	CASE_SCODE(TYPE_E_INCONSISTENTPROPFUNCS)
	CASE_SCODE(TYPE_E_CIRCULARTYPE)

	default:
          wsprintf((LPSTR)szBuffer,"0x%.8lX",hResult);
          bBufferUsed = TRUE;
	  break;

  } /* switch() */

  //Good place for exception handling if your compiler supports exception handling
  //
  //Copy the info into the output buffer
  if (bBufferUsed == FALSE)
	lstrcpy(szErrorString, lpStr);
  else
    lstrcpy(szErrorString, szBuffer);
}


//Given a CLSID, return the CLSID as a descriptive string
//The caller is required to make sure that szErrorString has enough
//room to return the descriptive string
//
void ClsIDToString(IID iid, TCHAR *szErrorString)
{
  LPTSTR lpStr;
  TCHAR szBuffer[50];

#define IF_IID(iidVal)\
   if (iid == iidVal)\
     lpStr = (LPTSTR)#iidVal;

//
  IF_IID(IID_IUnknown)
  else IF_IID(IID_IClassFactory)
  else IF_IID(IID_IMalloc)
  else IF_IID(IID_IMarshal)
  else IF_IID(IID_IRpcChannel)
  else IF_IID(IID_IRpcStub)
  else IF_IID(IID_IStubManager)
  else IF_IID(IID_IRpcProxy)
  else IF_IID(IID_IProxyManager)
  else IF_IID(IID_IPSFactory)
  else IF_IID(IID_ILockBytes)
  else IF_IID(IID_IStorage)
  else IF_IID(IID_IStream)
  else IF_IID(IID_IEnumSTATSTG)
  else IF_IID(IID_IBindCtx)
  else IF_IID(IID_IMoniker)
  else IF_IID(IID_IRunningObjectTable)
  else IF_IID(IID_IInternalMoniker)
  else IF_IID(IID_IRootStorage)
  else IF_IID(IID_IDfReserved1)
  else IF_IID(IID_IDfReserved2)
  else IF_IID(IID_IDfReserved3)
  else IF_IID(IID_IMessageFilter)
  else IF_IID(IID_IStdMarshalInfo)
  else IF_IID(IID_IEnumUnknown)
  else IF_IID(IID_IEnumString)
  else IF_IID(IID_IEnumMoniker)
  else IF_IID(IID_IEnumFORMATETC)
  else IF_IID(IID_IEnumOLEVERB)
  else IF_IID(IID_IEnumSTATDATA)
  else IF_IID(IID_IEnumGeneric)
  else IF_IID(IID_IEnumHolder)
  else IF_IID(IID_IEnumCallback)
  else IF_IID(IID_IPersistStream)
  else IF_IID(IID_IPersistStorage)
  else IF_IID(IID_IPersistFile)
  else IF_IID(IID_IPersist)
  else IF_IID(IID_IViewObject)
  else IF_IID(IID_IDataObject)
  else IF_IID(IID_IAdviseSink)
  else IF_IID(IID_IDataAdviseHolder)
  else IF_IID(IID_IOleObject)
  else IF_IID(IID_IOleInPlaceObject)
  else IF_IID(IID_IOleWindow)
  else IF_IID(IID_IOleInPlaceUIWindow)
  else IF_IID(IID_IOleInPlaceFrame)
  else IF_IID(IID_IOleInPlaceActiveObject)
  else IF_IID(IID_IOleClientSite)
  else IF_IID(IID_IOleInPlaceSite)
  else IF_IID(IID_IParseDisplayName)
  else IF_IID(IID_IOleContainer)
  else IF_IID(IID_IOleItemContainer)
  else IF_IID(IID_IOleLink)
  else IF_IID(IID_IOleCache)
  else IF_IID(IID_IOleManager)
  else IF_IID(IID_IOlePresObj)
  else IF_IID(IID_IDropSource)
  else IF_IID(IID_IDropTarget)
  else IF_IID(IID_IDebug)
  else IF_IID(IID_IDebugStream)
  else IF_IID(IID_IAdviseSink2)
  else IF_IID(IID_IViewObject2)
  else IF_IID(IID_IOleCache2)
  else IF_IID(IID_IRunnableObject)
  else IF_IID(IID_IExternalConnection)
  else IF_IID(IID_IStdMarshalInfo)
  else IF_IID(IID_IOleCacheControl)
  else IF_IID(IID_IDispatch)
  else IF_IID(IID_IEnumVARIANT)
  else{
   StringFromIID(iid, (LPOLESTR FAR*)&lpStr);
   lstrcpy(szBuffer,lpStr);
   PMOLEMemFree(lpStr);
  }

  lstrcpy(szErrorString,lpStr);

}

//Free's memory which was allocated by OLE
//This routine is required since StringFromIID (which is used ClsIDToString)
//allocates memory which needs to be freed
//
BOOL	PMOLEMemFree(void *p)
{
		IMalloc *pMalloc;

//Win32 allocates memory as MEMCTX_TASK while Win16 uses MEMCTX_SHARED
//
#ifdef WIN32
		DWORD		dwAlloc = MEMCTX_TASK;
#else
		DWORD		dwAlloc = MEMCTX_SHARED;
#endif

//Win16 allows the user to use any IMalloc interface for allocation, while
//Win32 must use its own allocation. To obtain the correct IMalloc interface under
//either platform just call CoGetMalloc.
//
	if (SUCCEEDED(CoGetMalloc(dwAlloc, &pMalloc))) {
		pMalloc->Free(p);
		return TRUE;
	}

return FALSE;
}

//The routine AnsiToUnicode converts an ANSI string into an OLECHAR
//An OLECHAR is a UNICODE string under Win32 and is a char under Win16
//A TCHAR is not a UNICODE string unless UNICODE is defined during the
//compilation of the application
//Also Windows 95 and Win32 do not support Unicode, however the OLE libraries
//for the Win32 platform requires Unicode
//
OLECHAR *AnsiToUnicode(TCHAR *szInput)
{
	//uiInputLength is the number of characters (not the number of bytes) of
	//szInput. 1 is added to the length to include the NUL terminator.
	//
	//If you need the number of bytes use: (uiInputLength * sizeof(TCHAR))
	//
	unsigned int uiInputLength = lstrlen(szInput)+1;

   //Since the follow code performs a 'new' the caller is expected to 'delete'
   //the OLECHAR which is returned
   //
   OLECHAR *pRet = new OLECHAR(uiInputLength);

   //'new' failed so there's no point in going on
   if (pRet == NULL)
   	return pRet;

   //If you want to try to ensure that szInput is not Unicode, you 
   //can call IsTextUnicode (before AnsiToWide)
   //which returns either TRUE (szInput is probably Unicode) or FALSE (szInput is
   //probably not Unicode). IsTextUnicode performs statistical tests on the input
   //string so it may not always return the correct answer
   //
   //AnsiToWide is a macro defined in Common.h which simply calls MultiByteToWideChar
   //with the necessary parameters. MultiByteToWideChar is actually the Window API
   //which performs the conversion.
   //
   AnsiToWide(szInput,pRet,uiInputLength);
   return pRet;
}

//The routine AnsiToUnicode256 converts an ANSI string into an OLECHAR
//An OLECHAR is a UNICODE string under Win32 and is a char under Win16
//This routine differs from AnsiToUnicode in that you don't delete the
//returned string. In addition, this routine has a limit of 256 characters.
//
OLECHAR	AnsiToUnicodeReturn[256];

OLECHAR *AnsiToUnicode256(TCHAR *szInput)
{
	//uiInputLength is the number of characters (not the number of bytes) of
	//szInput. 1 is added to the length to include the NUL terminator.
	//
	//If you need the number of bytes use: (uiInputLength * sizeof(TCHAR))
	//
	unsigned int uiInputLength = lstrlen(szInput)+1;

	//This routine supports a maximum size of 256, if you need a larger
	//translation, use AnsiToUnicode instead
	//
	if (uiInputLength >= 256)
		return NULL;

   OLECHAR *pRet = AnsiToUnicodeReturn;

   //If you want to try to ensure that szInput is not Unicode, you 
   //can call IsTextUnicode (before AnsiToWide)
   //which returns either TRUE (szInput is probably Unicode) or FALSE (szInput is
   //probably not Unicode). IsTextUnicode performs statistical tests on the input
   //string so it may not always return the correct answer
   //
   //AnsiToWide is a macro defined in Common.h which simply calls MultiByteToWideChar
   //with the necessary parameters. MultiByteToWideChar is actually the Window API
   //which performs the conversion.
   //
   AnsiToWide(szInput,pRet,uiInputLength);
   return pRet;
}

#ifdef _DEBUG
    #ifndef _AFX
        #ifdef WIN32
void TraceDebug1(LPCTSTR x, LPCTSTR y)
{
    TCHAR   tstr[256];

    wsprintf(tstr,x,y);
    OutputDebugString(tstr);
}
#endif  //Win32
#endif  //_AFX
#endif  //_Debug

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -