cdlg32.c 5.56 KB
Newer Older
1 2 3 4 5
/*
 *  Common Dialog Boxes interface (32 bit)
 *  Find/Replace
 *
 * Copyright 1999 Bertho A. Stultiens
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 21
 */

22 23 24
#include <stdarg.h>

#include "windef.h"
25
#include "winbase.h"
26 27
#include "wingdi.h"
#include "winuser.h"
28 29
#include "commdlg.h"
#include "cderr.h"
30
#include "wine/debug.h"
31

32
WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
33 34 35 36 37 38

#include "cdlg.h"


HINSTANCE	COMDLG32_hInstance = 0;

39
static DWORD COMDLG32_TlsIndex = TLS_OUT_OF_INDEXES;
40

41
HINSTANCE	SHELL32_hInstance = 0;
42
HINSTANCE	SHFOLDER_hInstance = 0;
43

44
/* ITEMIDLIST */
45 46 47 48 49
LPITEMIDLIST (WINAPI *COMDLG32_PIDL_ILClone) (LPCITEMIDLIST);
LPITEMIDLIST (WINAPI *COMDLG32_PIDL_ILCombine)(LPCITEMIDLIST,LPCITEMIDLIST);
LPITEMIDLIST (WINAPI *COMDLG32_PIDL_ILGetNext)(LPITEMIDLIST);
BOOL (WINAPI *COMDLG32_PIDL_ILRemoveLastID)(LPCITEMIDLIST);
BOOL (WINAPI *COMDLG32_PIDL_ILIsEqual)(LPCITEMIDLIST, LPCITEMIDLIST);
50 51

/* SHELL */
52 53 54
LPVOID (WINAPI *COMDLG32_SHAlloc)(DWORD);
DWORD (WINAPI *COMDLG32_SHFree)(LPVOID);
BOOL (WINAPI *COMDLG32_SHGetFolderPathA)(HWND,int,HANDLE,DWORD,LPSTR);
55
BOOL (WINAPI *COMDLG32_SHGetFolderPathW)(HWND,int,HANDLE,DWORD,LPWSTR);
56

57
/***********************************************************************
58
 *	DllMain  (COMDLG32.init)
59 60 61 62 63 64 65
 *
 *    Initialization code for the COMDLG32 DLL
 *
 * RETURNS:
 *	FALSE if sibling could not be loaded or instantiated twice, TRUE
 *	otherwise.
 */
66
static const char * GPA_string = "Failed to get entry point %s for hinst = 0x%08x\n";
67 68 69
#define GPA(dest, hinst, name) \
	if(!(dest = (void*)GetProcAddress(hinst,name)))\
	{ \
70
	  ERR(GPA_string, debugstr_a(name), hinst); \
71 72 73
	  return FALSE; \
	}

74
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD Reason, LPVOID Reserved)
75
{
76
	TRACE("(%p, %08lx, %p)\n", hInstance, Reason, Reserved);
77 78 79 80 81 82 83

	switch(Reason)
	{
	case DLL_PROCESS_ATTACH:
		COMDLG32_hInstance = hInstance;
		DisableThreadLibraryCalls(hInstance);

84
		SHELL32_hInstance = GetModuleHandleA("SHELL32.DLL");
85

86
		if (!SHELL32_hInstance)
87
		{
88
			ERR("loading of shell32 failed\n");
89 90
			return FALSE;
		}
91

92 93 94 95 96 97
		/* ITEMIDLIST */
		GPA(COMDLG32_PIDL_ILIsEqual, SHELL32_hInstance, (LPCSTR)21L);
		GPA(COMDLG32_PIDL_ILCombine, SHELL32_hInstance, (LPCSTR)25L);
		GPA(COMDLG32_PIDL_ILGetNext, SHELL32_hInstance, (LPCSTR)153L);
		GPA(COMDLG32_PIDL_ILClone, SHELL32_hInstance, (LPCSTR)18L);
		GPA(COMDLG32_PIDL_ILRemoveLastID, SHELL32_hInstance, (LPCSTR)17L);
98

99
		/* SHELL */
100

101 102 103 104 105 106 107 108 109
		GPA(COMDLG32_SHAlloc, SHELL32_hInstance, (LPCSTR)196L);
		GPA(COMDLG32_SHFree, SHELL32_hInstance, (LPCSTR)195L);
		/* for the first versions of shell32 SHGetFolderPathA is in SHFOLDER.DLL */
		COMDLG32_SHGetFolderPathA = (void*)GetProcAddress(SHELL32_hInstance,"SHGetFolderPathA");
		if (!COMDLG32_SHGetFolderPathA)
		{
		  SHFOLDER_hInstance = LoadLibraryA("SHFOLDER.DLL");
		  GPA(COMDLG32_SHGetFolderPathA, SHFOLDER_hInstance,"SHGetFolderPathA");
		}
110

111 112 113 114 115 116 117 118
		/* for the first versions of shell32 SHGetFolderPathW is in SHFOLDER.DLL */
		COMDLG32_SHGetFolderPathW = (void*)GetProcAddress(SHELL32_hInstance,"SHGetFolderPathW");
		if (!COMDLG32_SHGetFolderPathW)
		{
		  SHFOLDER_hInstance = LoadLibraryA("SHFOLDER.DLL");
		  GPA(COMDLG32_SHGetFolderPathW, SHFOLDER_hInstance,"SHGetFolderPathW");
		}

119 120 121
		break;

	case DLL_PROCESS_DETACH:
122
            if (COMDLG32_TlsIndex != TLS_OUT_OF_INDEXES) TlsFree(COMDLG32_TlsIndex);
123 124
            if(SHFOLDER_hInstance) FreeLibrary(SHFOLDER_hInstance);
            break;
125 126 127
	}
	return TRUE;
}
128
#undef GPA
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

/***********************************************************************
 *	COMDLG32_AllocMem 			(internal)
 * Get memory for internal datastructure plus stringspace etc.
 *	RETURNS
 *		Pointer to a heap block: Succes
 *		NULL: Failure
 */
LPVOID COMDLG32_AllocMem(
	int size	/* [in] Block size to allocate */
) {
        LPVOID ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
        if(!ptr)
        {
        	COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
                return NULL;
        }
        return ptr;
}


/***********************************************************************
 *	COMDLG32_SetCommDlgExtendedError	(internal)
 *
 * Used to set the thread's local error value if a comdlg32 function fails.
 */
void COMDLG32_SetCommDlgExtendedError(DWORD err)
{
157
	TRACE("(%08lx)\n", err);
158
        if (COMDLG32_TlsIndex == TLS_OUT_OF_INDEXES)
159
	  COMDLG32_TlsIndex = TlsAlloc();
160
	if (COMDLG32_TlsIndex != TLS_OUT_OF_INDEXES)
161 162 163
	  TlsSetValue(COMDLG32_TlsIndex, (void *)err);
	else
	  FIXME("No Tls Space\n");
164 165 166 167
}


/***********************************************************************
168 169
 *	CommDlgExtendedError			(COMMDLG.26)
 *	CommDlgExtendedError			(COMDLG32.@)
170 171 172 173 174 175 176 177
 *
 * Get the thread's local error value if a comdlg32 function fails.
 *	RETURNS
 *		Current error value which might not be valid
 *		if a previous call succeeded.
 */
DWORD WINAPI CommDlgExtendedError(void)
{
178
        if (COMDLG32_TlsIndex != TLS_OUT_OF_INDEXES)
179 180
	  return (DWORD)TlsGetValue(COMDLG32_TlsIndex);
	else
181
	  return 0; /* we never set an error, so there isn't one */
182
}