Use wrappers for accessing socket errors. Cygwin does not have some errors in errno...
[mono-project.git] / support / supportw.c
blob49ed549c5d2f546ffeffd31a2e3b8289fa32af1a
1 /*
2 * Helper routines for some of the common methods that people P/Invoke
3 * on their applications.
5 * Authors:
6 * Gonzalo Paniagua (gonzalo@ximian.com)
7 * Miguel de Icaza (miguel@novell.com)
9 * (C) 2005 Novell, Inc.
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"
23 typedef struct {
24 const char *fname;
25 void *fnptr;
26 } FnPtr;
28 gpointer FindWindowExW (gpointer hwndParent, gpointer hwndChildAfter,
29 const char *classw, const char *window);
31 gpointer GetProcessHeap (void);
33 static FnPtr functions [] = {
34 { "FindWindowExW", NULL }, /* user32 */
36 #define NFUNCTIONS (sizeof (functions)/sizeof (FnPtr))
38 static int swf_registered;
40 static int
41 compare_names (const void *key, const void *p)
43 FnPtr *ptr = (FnPtr *) p;
44 return strcmp (key, ptr->fname);
47 static gpointer
48 get_function (const char *name)
50 FnPtr *ptr;
52 ptr = bsearch (name, functions, NFUNCTIONS, sizeof (FnPtr),
53 compare_names);
55 if (ptr == NULL) {
56 g_warning ("Function '%s' not not found.", name);
57 return NULL;
60 return ptr->fnptr;
63 gboolean
64 supportw_register_delegate (const char *function_name, void *fnptr)
66 FnPtr *ptr;
68 g_return_val_if_fail (function_name && fnptr, FALSE);
70 ptr = bsearch (function_name, functions, NFUNCTIONS, sizeof (FnPtr),
71 compare_names);
73 if (ptr == NULL) {
74 g_warning ("Function '%s' not supported.", function_name);
75 return FALSE;
78 ptr->fnptr = fnptr;
79 return TRUE;
82 #define M_ATTRS (METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_STATIC)
83 static gboolean
84 register_assembly (const char *name, int *registered)
86 /* we can't use mono or wapi funcions in a support lib */
87 #if 0
88 MonoAssembly *assembly;
89 MonoImageOpenStatus status;
90 MonoImage *image;
91 MonoClass *klass;
92 MonoMethod *method;
93 MonoObject *exc;
95 if (*registered)
96 return TRUE;
98 assembly = mono_assembly_load_with_partial_name (name, &status);
99 if (assembly == NULL) {
100 g_warning ("Cannot load assembly '%s'.", name);
101 return FALSE;
104 image = mono_assembly_get_image (assembly);
105 klass = mono_class_from_name (image, name, "LibSupport");
106 if (klass == NULL) {
107 g_warning ("Cannot load class %s.LibSupport", name);
108 mono_assembly_close (assembly);
109 return FALSE;
112 method = mono_class_get_method_from_name_flags (klass, "Register", 0, M_ATTRS);
113 if (klass == NULL) {
114 g_warning ("Cannot load method Register from klass %s.LibSupport", name);
115 mono_assembly_close (assembly);
116 return FALSE;
119 exc = NULL;
120 mono_runtime_invoke (method, NULL, NULL, &exc);
121 if (exc != NULL) {
122 mono_assembly_close (assembly);
123 mono_print_unhandled_exception (exc);
124 return FALSE;
126 *registered = 1;
127 mono_assembly_close (assembly);
128 return TRUE;
129 #else
130 return FALSE;
131 #endif
134 void
135 supportw_test_all ()
137 int i;
139 register_assembly ("System.Windows.Forms", &swf_registered);
140 for (i = 0; i < NFUNCTIONS; i++) {
141 FnPtr *ptr = &functions [i];
142 if (ptr->fnptr == NULL)
143 g_warning ("%s wasn't registered.", ptr->fname);
147 gpointer
148 FindWindowExW (gpointer hwndParent, gpointer hwndChildAfter, const char *classw, const char *window)
150 typedef gpointer (*func_type) (gpointer hwndParent, gpointer hwndChildAfter,
151 const char *classw, const char *window);
152 static func_type func;
154 g_return_val_if_fail (register_assembly ("System.Windows.Forms", &swf_registered), NULL);
155 if (func == NULL)
156 func = (func_type) get_function ("FindWindowExW");
158 return func (hwndParent, hwndChildAfter, classw, window);
162 SetWindowPos (gpointer hwnd, gpointer hwndInsertAfter, int x, int y, int cx, int cy, unsigned int flags)
164 fprintf (stderr, "SetWindowPos %p %p to [%d,%dx%d,%d] %d\n", hwnd, hwndInsertAfter, x, y, cx, cy, flags);
165 return 1;
169 SendMessageA (gpointer hwnd, unsigned int msg, gpointer wparam, gpointer lparam)
171 fprintf (stderr, "SendMessage (%d, 0x%x, %p, %p)\n", (int) GPOINTER_TO_INT (hwnd), msg, wparam, lparam);
172 return 0;
176 GetWindowLongA (gpointer hwnd, int a)
178 return 0;