[PATCH] d3dx9: Add utility functions for loading files and resources into memory

Tony Wasserka tony.wasserka at freenet.de
Mon May 18 09:38:31 CDT 2009


---
 dlls/d3dx9_36/Makefile.in        |    3 +-
 dlls/d3dx9_36/d3dx9_36_private.h |    8 ++
 dlls/d3dx9_36/util.c             |  133 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 143 insertions(+), 1 deletions(-)
 create mode 100644 dlls/d3dx9_36/util.c

diff --git a/dlls/d3dx9_36/Makefile.in b/dlls/d3dx9_36/Makefile.in
index 16fd41c..c094071 100644
--- a/dlls/d3dx9_36/Makefile.in
+++ b/dlls/d3dx9_36/Makefile.in
@@ -13,7 +13,8 @@ C_SRCS = \
 	mesh.c \
 	shader.c \
 	sprite.c \
-	surface.c
+	surface.c \
+	util.c
 
 RC_SRCS = version.rc
 
diff --git a/dlls/d3dx9_36/d3dx9_36_private.h b/dlls/d3dx9_36/d3dx9_36_private.h
index cd4122d..14ca1f5 100644
--- a/dlls/d3dx9_36/d3dx9_36_private.h
+++ b/dlls/d3dx9_36/d3dx9_36_private.h
@@ -23,9 +23,17 @@
 #include <stdarg.h>
 
 #define COBJMACROS
+#include "winbase.h"
 #include "wingdi.h"
+#include "winuser.h"
 #include "d3dx9.h"
 
+/* for internal use */
+HRESULT LoadFileIntoMemoryA(LPCSTR filename, LPVOID *buffer, DWORD *length);
+HRESULT LoadFileIntoMemoryW(LPCWSTR filename, LPVOID *buffer, DWORD *length);
+HRESULT LoadResourceIntoMemoryA(HMODULE module, LPCSTR resname, LPVOID *buffer, DWORD *length);
+HRESULT LoadResourceIntoMemoryW(HMODULE module, LPCWSTR resname, LPVOID *buffer, DWORD *length);
+
 
 /* ID3DXFont */
 typedef struct ID3DXFontImpl
diff --git a/dlls/d3dx9_36/util.c b/dlls/d3dx9_36/util.c
new file mode 100644
index 0000000..bb615cd
--- /dev/null
+++ b/dlls/d3dx9_36/util.c
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2009 Tony Wasserka
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ *
+ */
+
+#include "wine/debug.h"
+#include "wine/unicode.h"
+#include "d3dx9_36_private.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
+
+/***************************************
+ * LoadFileIntoMemory                  *
+ *                                     *
+ * [in] filename                       *
+ * [out] buffer                        *
+ * [out] length                        *
+ *                                     *
+ * Loads a file into buffer and stores *
+ * the number of read bytes in length. *
+ * The allocated memory needs to be    *
+ * freed by the caller manually.       *
+ ***************************************/
+HRESULT LoadFileIntoMemoryW(LPCWSTR filename, LPVOID *buffer, DWORD *length)
+{
+    HANDLE handle;
+
+    if( !filename || !buffer || !length ) return D3DERR_INVALIDCALL;
+
+    handle = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
+    if(handle == INVALID_HANDLE_VALUE) return D3DERR_INVALIDCALL;
+
+    *length = GetFileSize(handle, NULL);
+    if(*length == INVALID_FILE_SIZE) return D3DERR_INVALIDCALL;
+
+    *buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *length);
+    if(*buffer == NULL) {
+        CloseHandle(handle);
+        return D3DERR_INVALIDCALL;
+    }
+
+    if(ReadFile(handle, *buffer, *length, NULL, 0) == FALSE) {
+        HeapFree(GetProcessHeap(), 0, *buffer);
+        CloseHandle(handle);
+        return D3DERR_INVALIDCALL;
+    }
+    CloseHandle(handle);
+
+    return D3D_OK;
+}
+
+HRESULT LoadFileIntoMemoryA(LPCSTR filename, LPVOID *buffer, DWORD *length)
+{
+    LPWSTR widename;
+    HRESULT hr;
+    int strlength;
+
+    if( !filename || !buffer || !length ) return D3DERR_INVALIDCALL;
+
+    strlength = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
+    widename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlength * sizeof(WCHAR));
+    MultiByteToWideChar(CP_ACP, 0, filename, -1, widename, strlength);
+
+    hr = LoadFileIntoMemoryW(widename, buffer, length);
+    HeapFree(GetProcessHeap(), 0, widename);
+
+    return hr;
+}
+
+/*******************************************
+ * LoadResourceIntoMemory                  *
+ *                                         *
+ * [in] module                             *
+ * [in] resname                            *
+ * [out] buffer                            *
+ * [out] length                            *
+ *                                         *
+ * Loads a resource into buffer and stores *
+ * the number of read bytes in length.     *
+ * The memory doesn't need to be freed     *
+ * by the caller manually.                 *
+ *******************************************/
+HRESULT LoadResourceIntoMemoryW(HMODULE module, LPCWSTR resname, LPVOID *buffer, DWORD *length)
+{
+    HRSRC resinfo;
+    HGLOBAL resource;
+
+    if( !module || !resname || !buffer || !length ) return D3DERR_INVALIDCALL;
+
+    resinfo = FindResourceW(module, resname, (LPCWSTR)RT_RCDATA);
+    if( !resinfo ) return D3DERR_INVALIDCALL;
+
+    *length = SizeofResource(module, resinfo);
+    resource = LoadResource(module, resinfo);
+    if( !resource ) return D3DERR_INVALIDCALL;
+
+    *buffer = LockResource(resource);
+    if(*buffer == NULL) return D3DERR_INVALIDCALL;
+
+    return D3D_OK;
+}
+
+HRESULT LoadResourceIntoMemoryA(HMODULE module, LPCSTR resname, LPVOID *buffer, DWORD *length)
+{
+    LPWSTR widename;
+    HRESULT hr;
+    int strlength;
+
+    if( !module || !resname || !buffer || !length ) return D3DERR_INVALIDCALL;
+
+    strlength = MultiByteToWideChar(CP_ACP, 0, resname, -1, NULL, 0);
+    widename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlength * sizeof(WCHAR));
+    MultiByteToWideChar(CP_ACP, 0, resname, -1, widename, strlength);
+
+    hr = LoadResourceIntoMemoryW(module, widename, buffer, length);
+    HeapFree(GetProcessHeap(), 0, widename);
+
+    return hr;
+}
-- 
1.6.0.2


--------------000304030204040305090807--



More information about the wine-patches mailing list