2 * Helper routines for some of the common methods that people P/Invoke
3 * on their applications.
6 * Gonzalo Paniagua (gonzalo@ximian.com)
7 * Miguel de Icaza (miguel@novell.com)
9 * (C) 2005 Novell, Inc.
18 #include "mono/metadata/assembly.h"
19 #include "mono/metadata/class.h"
20 #include "mono/metadata/object.h"
21 #include "mono/metadata/tabledefs.h"
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
;
41 compare_names (const void *key
, const void *p
)
43 FnPtr
*ptr
= (FnPtr
*) p
;
44 return strcmp (key
, ptr
->fname
);
48 get_function (const char *name
)
52 ptr
= bsearch (name
, functions
, NFUNCTIONS
, sizeof (FnPtr
),
56 g_warning ("Function '%s' not found.", name
);
64 supportw_register_delegate (const char *function_name
, void *fnptr
)
68 g_return_val_if_fail (function_name
&& fnptr
, FALSE
);
70 ptr
= bsearch (function_name
, functions
, NFUNCTIONS
, sizeof (FnPtr
),
74 g_warning ("Function '%s' not supported.", function_name
);
82 #define M_ATTRS (METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_STATIC)
84 register_assembly (const char *name
, int *registered
)
86 /* we can't use mono or wapi funcions in a support lib */
88 MonoAssembly
*assembly
;
89 MonoImageOpenStatus status
;
98 assembly
= mono_assembly_load_with_partial_name (name
, &status
);
99 if (assembly
== NULL
) {
100 g_warning ("Cannot load assembly '%s'.", name
);
104 image
= mono_assembly_get_image (assembly
);
105 klass
= mono_class_from_name (image
, name
, "LibSupport");
107 g_warning ("Cannot load class %s.LibSupport", name
);
108 mono_assembly_close (assembly
);
112 method
= mono_class_get_method_from_name_flags (klass
, "Register", 0, M_ATTRS
);
114 g_warning ("Cannot load method Register from klass %s.LibSupport", name
);
115 mono_assembly_close (assembly
);
120 mono_runtime_invoke (method
, NULL
, NULL
, &exc
);
122 mono_assembly_close (assembly
);
123 mono_print_unhandled_exception (exc
);
127 mono_assembly_close (assembly
);
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
);
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
);
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
);
165 SetWindowPos (gpointer hwnd
, gpointer hwndInsertAfter
, int x
, int y
, int cx
, int cy
, unsigned int flags
)
167 fprintf (stderr
, "SetWindowPos %p %p to [%d,%dx%d,%d] %d\n", hwnd
, hwndInsertAfter
, x
, y
, cx
, cy
, flags
);
172 SendMessageA (gpointer hwnd
, unsigned int msg
, gpointer wparam
, gpointer lparam
);
175 SendMessageA (gpointer hwnd
, unsigned int msg
, gpointer wparam
, gpointer lparam
)
177 fprintf (stderr
, "SendMessage (%d, 0x%x, %p, %p)\n", (int) GPOINTER_TO_INT (hwnd
), msg
, wparam
, lparam
);
182 GetWindowLongA (gpointer hwnd
, int a
);
185 GetWindowLongA (gpointer hwnd
, int a
)