winhttp: Convert connect_t's address type from a struct sockaddr_in to a struct socka...
[wine] / dlls / d3dx9_36 / util.c
1 /*
2  * Copyright (C) 2009 Tony Wasserka
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  *
18  */
19
20 #include "wine/debug.h"
21 #include "d3dx9_36_private.h"
22
23 /************************************************************
24  * map_view_of_file
25  *
26  * Loads a file into buffer and stores the number of read bytes in length.
27  *
28  * PARAMS
29  *   filename [I] name of the file to be loaded
30  *   buffer   [O] pointer to destination buffer
31  *   length   [O] size of the obtained data
32  *
33  * RETURNS
34  *   Success: D3D_OK
35  *   Failure:
36  *     see error codes for CreateFileW, GetFileSize, CreateFileMapping and MapViewOfFile
37  *
38  * NOTES
39  *   The caller must UnmapViewOfFile when it doesn't need the data anymore
40  *
41  */
42 HRESULT map_view_of_file(LPCWSTR filename, LPVOID *buffer, DWORD *length)
43 {
44     HANDLE hfile, hmapping = NULL;
45
46     hfile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
47     if(hfile == INVALID_HANDLE_VALUE) goto error;
48
49     *length = GetFileSize(hfile, NULL);
50     if(*length == INVALID_FILE_SIZE) goto error;
51
52     hmapping = CreateFileMappingW(hfile, NULL, PAGE_READONLY, 0, 0, NULL);
53     if(!hmapping) goto error;
54
55     *buffer = MapViewOfFile(hmapping, FILE_MAP_READ, 0, 0, 0);
56     if(*buffer == NULL) goto error;
57
58     CloseHandle(hmapping);
59     CloseHandle(hfile);
60
61     return S_OK;
62
63 error:
64     CloseHandle(hmapping);
65     CloseHandle(hfile);
66     return HRESULT_FROM_WIN32(GetLastError());
67 }
68
69 /************************************************************
70  * load_resource_into_memory
71  *
72  * Loads a resource into buffer and stores the number of
73  * read bytes in length.
74  *
75  * PARAMS
76  *   module  [I] handle to the module
77  *   resinfo [I] handle to the resource's information block
78  *   buffer  [O] pointer to destination buffer
79  *   length  [O] size of the obtained data
80  *
81  * RETURNS
82  *   Success: D3D_OK
83  *   Failure:
84  *     See error codes for SizeofResource, LoadResource and LockResource
85  *
86  * NOTES
87  *   The memory doesn't need to be freed by the caller manually
88  *
89  */
90 HRESULT load_resource_into_memory(HMODULE module, HRSRC resinfo, LPVOID *buffer, DWORD *length)
91 {
92     HGLOBAL resource;
93
94     *length = SizeofResource(module, resinfo);
95     if(*length == 0) return HRESULT_FROM_WIN32(GetLastError());
96
97     resource = LoadResource(module, resinfo);
98     if( !resource ) return HRESULT_FROM_WIN32(GetLastError());
99
100     *buffer = LockResource(resource);
101     if(*buffer == NULL) return HRESULT_FROM_WIN32(GetLastError());
102
103     return S_OK;
104 }