434301d6359928b5eae2ee1e2d31c79568e15d9f
[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         MonoAssembly *assembly;
90         MonoImageOpenStatus status;
91         MonoImage *image;
92         MonoClass *klass;
93         MonoMethod *method;
94         MonoObject *exc;
95
96         if (*registered)
97                 return TRUE;
98
99         assembly = mono_assembly_load_with_partial_name (name, &status);
100         if (assembly == NULL) {
101                 g_warning ("Cannot load assembly '%s'.", name);
102                 return FALSE;
103         }
104
105         image = mono_assembly_get_image (assembly);
106         klass = mono_class_from_name (image, name, "LibSupport");
107         if (klass == NULL) {
108                 g_warning ("Cannot load class %s.LibSupport", name);
109                 mono_assembly_close (assembly);
110                 return FALSE;
111         }
112
113         method = mono_class_get_method_from_name_flags (klass, "Register", 0, M_ATTRS);
114         if (klass == NULL) {
115                 g_warning ("Cannot load method Register from klass %s.LibSupport", name);
116                 mono_assembly_close (assembly);
117                 return FALSE;
118         }
119
120         exc = NULL;
121         mono_runtime_invoke (method, NULL, NULL, &exc);
122         if (exc != NULL) {
123                 mono_assembly_close (assembly);
124                 mono_print_unhandled_exception (exc);
125                 return FALSE;
126         }
127         *registered = 1;
128         mono_assembly_close (assembly);
129         return TRUE;
130 }
131
132 void
133 supportw_test_all ()
134 {
135         int i;
136
137         register_assembly ("System.Windows.Forms", &swf_registered);
138         for (i = 0; i < NFUNCTIONS; i++) {
139                 FnPtr *ptr = &functions [i];
140                 if (ptr->fnptr == NULL)
141                         g_warning ("%s wasn't registered.", ptr->fname);
142         }
143 }
144
145 gpointer
146 FindWindowExW (gpointer hwndParent, gpointer hwndChildAfter, const char *classw, const char *window)
147 {
148         typedef gpointer (*func_type) (gpointer hwndParent, gpointer hwndChildAfter,
149                                         const char *classw, const char *window);
150         static func_type func;
151
152         g_return_val_if_fail (register_assembly ("System.Windows.Forms", &swf_registered), NULL);
153         if (func == NULL)
154                 func = (func_type) get_function ("FindWindowExW");
155
156         return func (hwndParent, hwndChildAfter, classw, window);
157 }
158
159 /* begin Heap* functions */
160 gpointer
161 HeapCreate (gint32 flags, gint32 initial_size, gint32 max_size)
162 {
163         return (gpointer) 0xDEADBEEF;
164 }
165
166 gboolean
167 HeapSetInformation (gpointer handle, gpointer heap_info_class, gpointer heap_info,
168                         gint32 head_info_length)
169 {
170         return TRUE;
171 }
172
173 gboolean
174 HeapQueryInformation (gpointer handle, gpointer heap_info_class, gpointer heap_info,
175                         gint32 head_info_length, gint32 *ret_length)
176 {
177         *ret_length = 0;
178         return TRUE;
179 }
180
181 gpointer
182 HeapAlloc (gpointer handle, gint32 flags, gint32 nbytes)
183 {
184         return g_malloc0 (nbytes);
185 }
186
187 gpointer
188 HeapReAlloc (gpointer handle, gint32 flags, gpointer mem, gint32 nbytes)
189 {
190         return g_realloc (mem, nbytes);
191 }
192
193 gint32
194 HeapSize (gpointer handle, gint32 flags, gpointer mem)
195 {
196         return 0;
197 }
198
199 gboolean
200 HeapFree (gpointer handle, gint32 flags, gpointer mem)
201 {
202         g_free (mem);
203         return TRUE;
204 }
205
206 gboolean
207 HeapValidate (gpointer handle, gpointer mem)
208 {
209         return TRUE;
210 }
211
212 gboolean
213 HeapDestroy (gpointer handle)
214 {
215         return TRUE;
216 }
217
218
219 gpointer 
220 GetProcessHeap ()
221 {
222         return (gpointer) 0xDEADBEEF;
223 }
224 /* end Heap* functions */
225