Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygend3d9_helpers.c
Go to the documentation of this file.
00001 /* 00002 * COPYRIGHT: See COPYING in the top level directory 00003 * PROJECT: ReactOS ReactX 00004 * FILE: dll/directx/d3d9/d3d9_helpers.c 00005 * PURPOSE: d3d9.dll helper functions 00006 * PROGRAMERS: Gregor Brunmar <gregor (dot) brunmar (at) home (dot) se> 00007 */ 00008 00009 #include <d3d9.h> 00010 #include "d3d9_helpers.h" 00011 #include <stdio.h> 00012 #include <ddraw.h> 00013 #include <debug.h> 00014 00015 #define MEM_ALIGNMENT 0x20 00016 00017 static LPCSTR D3D9_DebugRegPath = "Software\\Microsoft\\Direct3D"; 00018 00019 BOOL ReadRegistryValue(IN DWORD ValueType, IN LPCSTR ValueName, OUT LPBYTE DataBuffer, IN OUT LPDWORD DataBufferSize) 00020 { 00021 HKEY hKey; 00022 DWORD Type; 00023 LONG Ret; 00024 00025 if (ERROR_SUCCESS != RegOpenKeyExA(HKEY_LOCAL_MACHINE, D3D9_DebugRegPath, 0, KEY_QUERY_VALUE, &hKey)) 00026 return FALSE; 00027 00028 Ret = RegQueryValueExA(hKey, ValueName, 0, &Type, DataBuffer, DataBufferSize); 00029 00030 RegCloseKey(hKey); 00031 00032 if (ERROR_SUCCESS != Ret) 00033 return FALSE; 00034 00035 if (Type != ValueType) 00036 return FALSE; 00037 00038 return TRUE; 00039 } 00040 00041 HRESULT SafeFormatString(OUT LPSTR Buffer, IN DWORD BufferSize, IN LPCSTR FormatString, ... ) 00042 { 00043 DWORD BytesWritten; 00044 va_list vargs; 00045 00046 if (BufferSize == 0) 00047 return DDERR_INVALIDPARAMS; 00048 00049 va_start(vargs, FormatString); 00050 BytesWritten = _vsnprintf(Buffer, BufferSize-1, FormatString, vargs); 00051 00052 if (BytesWritten < BufferSize) 00053 return DDERR_GENERIC; 00054 00055 Buffer[BufferSize-1] = '\0'; 00056 00057 return ERROR_SUCCESS; 00058 } 00059 00060 HRESULT SafeCopyString(OUT LPSTR Dst, IN DWORD DstSize, IN LPCSTR Src) 00061 { 00062 HRESULT hr = ERROR_SUCCESS; 00063 00064 if (Dst == NULL || DstSize == 0 || Src == NULL) 00065 return DDERR_INVALIDPARAMS; 00066 00067 while (*Src != '\0' && DstSize > 0) 00068 { 00069 *Dst++ = *Src++; 00070 --DstSize; 00071 } 00072 00073 if (DstSize == 0) 00074 { 00075 --Dst; 00076 hr = DDERR_GENERIC; 00077 } 00078 00079 return hr; 00080 } 00081 00082 HRESULT SafeAppendString(IN OUT LPSTR Dst, IN DWORD DstSize, IN LPCSTR Src) 00083 { 00084 size_t CurrentDstLength; 00085 00086 if (Dst == NULL || DstSize == 0) 00087 return DDERR_INVALIDPARAMS; 00088 00089 CurrentDstLength = strlen(Dst); 00090 00091 return SafeCopyString(Dst + CurrentDstLength, DstSize - CurrentDstLength, Src); 00092 } 00093 00094 HRESULT AlignedAlloc(IN OUT LPVOID *ppObject, IN SIZE_T dwSize) 00095 { 00096 ULONG_PTR AddressOffset; 00097 ULONG AlignedMask = MEM_ALIGNMENT - 1; 00098 CHAR *AlignedPtr; 00099 ULONG_PTR *AlignedOffsetPtr; 00100 00101 if (ppObject == 0) 00102 return DDERR_INVALIDPARAMS; 00103 00104 if (dwSize == 0) 00105 { 00106 *ppObject = NULL; 00107 return S_OK; 00108 } 00109 00110 dwSize += MEM_ALIGNMENT; 00111 00112 AlignedPtr = (CHAR *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize); 00113 00114 if (AlignedPtr == 0) 00115 return DDERR_OUTOFMEMORY; 00116 00117 AddressOffset = MEM_ALIGNMENT - ((ULONG_PTR)AlignedPtr & AlignedMask); 00118 00119 AlignedPtr += AddressOffset; 00120 00121 AlignedOffsetPtr = (ULONG_PTR *)(AlignedPtr - sizeof(ULONG)); 00122 *AlignedOffsetPtr = AddressOffset; 00123 00124 *ppObject = (ULONG_PTR *)AlignedPtr; 00125 00126 return S_OK; 00127 } 00128 00129 VOID AlignedFree(IN OUT LPVOID pObject) 00130 { 00131 CHAR *NonAlignedPtr = pObject; 00132 ULONG_PTR *AlignedPtr = pObject; 00133 00134 if (pObject == 0) 00135 return; 00136 00137 NonAlignedPtr -= *(AlignedPtr - 1); 00138 00139 HeapFree(GetProcessHeap(), 0, NonAlignedPtr); 00140 } Generated on Sat May 26 2012 04:19:52 for ReactOS by
1.7.6.1
|