f1fa2d16ff7ab1946d5cbb1f0929d391e0ee3b69
[mono.git] / support / supportw.c
1 /*
2  * Helper routines for some of the common methods that people P/Invoke
3  * on their applications.
4  *
5  * Authors:
6  *   Gonzalo Paniagua (gonzalo@ximian.com)
7  *   Miguel de Icaza  (miguel@novell.com)
8  *
9  * (C) 2005 Novell, Inc.
10  *
11  */
12 #include <glib.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include "supportw.h"
17 #include "mono/metadata/assembly.h"
18 #include "mono/metadata/class.h"
19 #include "mono/metadata/object.h"
20 #include "mono/metadata/tabledefs.h"
21 #include "mono/io-layer/wapi.h"
22
23 typedef struct {
24         const char *fname;
25         void *fnptr;
26 } FnPtr;
27
28 gpointer FindWindowExW        (gpointer hwndParent, gpointer hwndChildAfter,
29                                const char *classw, const char *window);
30
31 gpointer HeapAlloc            (gpointer unused1, gint32 unused2, gint32 nbytes);
32 gpointer HeapCreate           (gint32 flags, gint32 initial_size, gint32 max_size);
33 gboolean HeapSetInformation   (gpointer handle, gpointer heap_info_class,
34                                gpointer heap_info, gint32 head_info_length);
35
36 gboolean HeapQueryInformation (gpointer handle, gpointer heap_info_class,
37                                gpointer heap_info, gint32 head_info_length, gint32 *ret_length);
38
39 gpointer HeapAlloc            (gpointer handle, gint32 flags, gint32 nbytes);
40 gpointer HeapReAlloc          (gpointer handle, gint32 flags, gpointer mem, gint32 nbytes);
41 gint32   HeapSize             (gpointer handle, gint32 flags, gpointer mem);
42 gboolean HeapFree             (gpointer handle, gint32 flags, gpointer mem);
43 gboolean HeapValidate         (gpointer handle, gpointer mem);
44 gboolean HeapDestroy          (gpointer handle);
45 gpointer GetProcessHeap       (void);
46
47 static FnPtr functions [] = {
48         { "FindWindowExW", NULL }, /* user32 */
49 };
50 #define NFUNCTIONS      (sizeof (functions)/sizeof (FnPtr))
51
52 static int swf_registered;
53
54 static int
55 compare_names (const void *key, const void *p)
56 {
57         FnPtr *ptr = (FnPtr *) p;
58         return strcmp (key, ptr->fname);
59 }
60
61 static gpointer
62 get_function (const char *name)
63 {
64         FnPtr *ptr;
65
66         ptr = bsearch (name, functions, NFUNCTIONS, sizeof (FnPtr),
67                         compare_names);
68
69         if (ptr == NULL) {
70                 g_warning ("Function '%s' not not found.", name);
71                 return NULL;
72         }
73
74         return ptr->fnptr;
75 }
76
77 gboolean
78 supportw_register_delegate (const char *function_name, void *fnptr)
79 {
80         FnPtr *ptr;
81
82         g_return_val_if_fail (function_name && fnptr, FALSE);
83
84         ptr = bsearch (function_name, functions, NFUNCTIONS, sizeof (FnPtr),
85                         compare_names);
86
87         if (ptr == NULL) {
88                 g_warning ("Function '%s' not supported.", function_name);
89                 return FALSE;
90         }
91
92         ptr->fnptr = fnptr;
93         return TRUE;
94 }
95
96 #define M_ATTRS (METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_STATIC)
97 static gboolean
98 register_assembly (const char *name, int *registered)
99 {
100 /* we can't use mono or wapi funcions in a support lib */
101 #if 0
102         MonoAssembly *assembly;
103         MonoImageOpenStatus status;
104         MonoImage *image;
105         MonoClass *klass;
106         MonoMethod *method;
107         MonoObject *exc;
108
109         if (*registered)
110                 return TRUE;
111
112         assembly = mono_assembly_load_with_partial_name (name, &status);
113         if (assembly == NULL) {
114                 g_warning ("Cannot load assembly '%s'.", name);
115                 return FALSE;
116         }
117
118         image = mono_assembly_get_image (assembly);
119         klass = mono_class_from_name (image, name, "LibSupport");
120         if (klass == NULL) {
121                 g_warning ("Cannot load class %s.LibSupport", name);
122                 mono_assembly_close (assembly);
123                 return FALSE;
124         }
125
126         method = mono_class_get_method_from_name_flags (klass, "Register", 0, M_ATTRS);
127         if (klass == NULL) {
128                 g_warning ("Cannot load method Register from klass %s.LibSupport", name);
129                 mono_assembly_close (assembly);
130                 return FALSE;
131         }
132
133         exc = NULL;
134         mono_runtime_invoke (method, NULL, NULL, &exc);
135         if (exc != NULL) {
136                 mono_assembly_close (assembly);
137                 mono_print_unhandled_exception (exc);
138                 return FALSE;
139         }
140         *registered = 1;
141         mono_assembly_close (assembly);
142         return TRUE;
143 #else
144         return FALSE;
145 #endif
146 }
147
148 void
149 supportw_test_all ()
150 {
151         int i;
152
153         register_assembly ("System.Windows.Forms", &swf_registered);
154         for (i = 0; i < NFUNCTIONS; i++) {
155                 FnPtr *ptr = &functions [i];
156                 if (ptr->fnptr == NULL)
157                         g_warning ("%s wasn't registered.", ptr->fname);
158         }
159 }
160
161 gpointer
162 FindWindowExW (gpointer hwndParent, gpointer hwndChildAfter, const char *classw, const char *window)
163 {
164         typedef gpointer (*func_type) (gpointer hwndParent, gpointer hwndChildAfter,
165                                         const char *classw, const char *window);
166         static func_type func;
167
168         g_return_val_if_fail (register_assembly ("System.Windows.Forms", &swf_registered), NULL);
169         if (func == NULL)
170                 func = (func_type) get_function ("FindWindowExW");
171
172         return func (hwndParent, hwndChildAfter, classw, window);
173 }
174
175