svn path=/branches/mono-1-1-9/mcs/; revision=51212
[mono.git] / support / supportw.c
1 #include <glib.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include "supportw.h"
6 #include "mono/metadata/assembly.h"
7 #include "mono/metadata/class.h"
8 #include "mono/metadata/object.h"
9 #include "mono/metadata/tabledefs.h"
10 #include "mono/io-layer/wapi.h"
11
12 typedef struct {
13         const char *fname;
14         void *fnptr;
15 } FnPtr;
16
17 gpointer FindWindowExW (gpointer hwndParent, gpointer hwndChildAfter,
18                         const char *classw, const char *window);
19
20 gpointer HeapAlloc (gpointer unused1, gint32 unused2, gint32 nbytes);
21 gpointer HeapCreate (gint32 flags, gint32 initial_size, gint32 max_size);
22 gboolean HeapSetInformation (gpointer handle, gpointer heap_info_class,
23                                 gpointer heap_info, gint32 head_info_length);
24
25 gboolean HeapQueryInformation (gpointer handle, gpointer heap_info_class,
26                         gpointer heap_info, gint32 head_info_length, gint32 *ret_length);
27
28 gpointer HeapAlloc (gpointer handle, gint32 flags, gint32 nbytes);
29 gpointer HeapReAlloc (gpointer handle, gint32 flags, gpointer mem, gint32 nbytes);
30 gint32 HeapSize (gpointer handle, gint32 flags, gpointer mem);
31 gboolean HeapFree (gpointer handle, gint32 flags, gpointer mem);
32 gboolean HeapValidate (gpointer handle, gpointer mem);
33 gboolean HeapDestroy (gpointer handle);
34 gpointer GetProcessHeap (void);
35
36 static FnPtr functions [] = {
37         { "FindWindowExW", NULL }, /* user32 */
38 };
39 #define NFUNCTIONS      (sizeof (functions)/sizeof (FnPtr))
40
41 static int swf_registered;
42
43 static int
44 compare_names (const void *key, const void *p)
45 {
46         FnPtr *ptr = (FnPtr *) p;
47         return strcmp (key, ptr->fname);
48 }
49
50 static gpointer
51 get_function (const char *name)
52 {
53         FnPtr *ptr;
54
55         ptr = bsearch (name, functions, NFUNCTIONS, sizeof (FnPtr),
56                         compare_names);
57
58         if (ptr == NULL) {
59                 g_warning ("Function '%s' not not found.", name);
60                 return NULL;
61         }
62
63         return ptr->fnptr;
64 }
65
66 gboolean
67 supportw_register_delegate (const char *function_name, void *fnptr)
68 {
69         FnPtr *ptr;
70
71         g_return_val_if_fail (function_name && fnptr, FALSE);
72
73         ptr = bsearch (function_name, functions, NFUNCTIONS, sizeof (FnPtr),
74                         compare_names);
75
76         if (ptr == NULL) {
77                 g_warning ("Function '%s' not supported.", function_name);
78                 return FALSE;
79         }
80
81         ptr->fnptr = fnptr;
82         return TRUE;
83 }
84
85 #define M_ATTRS (METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_STATIC)
86 static gboolean
87 register_assembly (const char *name, int *registered)
88 {
89 /* we can't use mono or wapi funcions in a support lib */
90 #if 0
91         MonoAssembly *assembly;
92         MonoImageOpenStatus status;
93         MonoImage *image;
94         MonoClass *klass;
95         MonoMethod *method;
96         MonoObject *exc;
97
98         if (*registered)
99                 return TRUE;
100
101         assembly = mono_assembly_load_with_partial_name (name, &status);
102         if (assembly == NULL) {
103                 g_warning ("Cannot load assembly '%s'.", name);
104                 return FALSE;
105         }
106
107         image = mono_assembly_get_image (assembly);
108         klass = mono_class_from_name (image, name, "LibSupport");
109         if (klass == NULL) {
110                 g_warning ("Cannot load class %s.LibSupport", name);
111                 mono_assembly_close (assembly);
112                 return FALSE;
113         }
114
115         method = mono_class_get_method_from_name_flags (klass, "Register", 0, M_ATTRS);
116         if (klass == NULL) {
117                 g_warning ("Cannot load method Register from klass %s.LibSupport", name);
118                 mono_assembly_close (assembly);
119                 return FALSE;
120         }
121
122         exc = NULL;
123         mono_runtime_invoke (method, NULL, NULL, &exc);
124         if (exc != NULL) {
125                 mono_assembly_close (assembly);
126                 mono_print_unhandled_exception (exc);
127                 return FALSE;
128         }
129         *registered = 1;
130         mono_assembly_close (assembly);
131         return TRUE;
132 #else
133         return FALSE;
134 #endif
135 }
136
137 void
138 supportw_test_all ()
139 {
140         int i;
141
142         register_assembly ("System.Windows.Forms", &swf_registered);
143         for (i = 0; i < NFUNCTIONS; i++) {
144                 FnPtr *ptr = &functions [i];
145                 if (ptr->fnptr == NULL)
146                         g_warning ("%s wasn't registered.", ptr->fname);
147         }
148 }
149
150 gpointer
151 FindWindowExW (gpointer hwndParent, gpointer hwndChildAfter, const char *classw, const char *window)
152 {
153         typedef gpointer (*func_type) (gpointer hwndParent, gpointer hwndChildAfter,
154                                         const char *classw, const char *window);
155         static func_type func;
156
157         g_return_val_if_fail (register_assembly ("System.Windows.Forms", &swf_registered), NULL);
158         if (func == NULL)
159                 func = (func_type) get_function ("FindWindowExW");
160
161         return func (hwndParent, hwndChildAfter, classw, window);
162 }
163
164 /* begin Heap* functions */
165 gpointer
166 HeapCreate (gint32 flags, gint32 initial_size, gint32 max_size)
167 {
168         return (gpointer) 0xDEADBEEF;
169 }
170
171 gboolean
172 HeapSetInformation (gpointer handle, gpointer heap_info_class, gpointer heap_info,
173                         gint32 head_info_length)
174 {
175         return TRUE;
176 }
177
178 gboolean
179 HeapQueryInformation (gpointer handle, gpointer heap_info_class, gpointer heap_info,
180                         gint32 head_info_length, gint32 *ret_length)
181 {
182         *ret_length = 0;
183         return TRUE;
184 }
185
186 gpointer
187 HeapAlloc (gpointer handle, gint32 flags, gint32 nbytes)
188 {
189         return g_malloc0 (nbytes);
190 }
191
192 gpointer
193 HeapReAlloc (gpointer handle, gint32 flags, gpointer mem, gint32 nbytes)
194 {
195         return g_realloc (mem, nbytes);
196 }
197
198 gint32
199 HeapSize (gpointer handle, gint32 flags, gpointer mem)
200 {
201         return 0;
202 }
203
204 gboolean
205 HeapFree (gpointer handle, gint32 flags, gpointer mem)
206 {
207         g_free (mem);
208         return TRUE;
209 }
210
211 gboolean
212 HeapValidate (gpointer handle, gpointer mem)
213 {
214         return TRUE;
215 }
216
217 gboolean
218 HeapDestroy (gpointer handle)
219 {
220         return TRUE;
221 }
222
223
224 gpointer 
225 GetProcessHeap ()
226 {
227         return (gpointer) 0xDEADBEEF;
228 }
229 /* end Heap* functions */
230