r12499: Move smb_build.h out of includes.h
[bbaumbach/samba-autobuild/.git] / source4 / lib / com / main.c
1 /*
2    Unix SMB/CIFS implementation.
3    Main COM functionality
4    Copyright (C) 2004 Jelmer Vernooij <jelmer@samba.org>
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "dlinklist.h"
23 #include "lib/com/com.h"
24 #include "lib/events/events.h"
25 #include "librpc/gen_ndr/com_dcom.h"
26 #include "smb_build.h"
27
28 WERROR com_init_ctx(struct com_context **ctx, struct event_context *event_ctx)
29 {
30         *ctx = talloc(NULL, struct com_context);
31         if (event_ctx == NULL) {
32                 event_ctx = event_context_init(*ctx);
33         }
34         (*ctx)->event_ctx = event_ctx;
35         return WERR_OK;
36 }
37
38 WERROR com_create_object(struct com_context *ctx, struct GUID *clsid, int num_ifaces, struct GUID *iid, struct IUnknown **ip, WERROR *results)
39 {
40         struct IUnknown *iunk = NULL;
41         struct IClassFactory *factory;
42         WERROR error;
43         int i;
44         struct GUID classfact_iid;
45
46         GUID_from_string(DCERPC_ICLASSFACTORY_UUID, &classfact_iid);
47
48         /* Obtain class object */
49         error = com_get_class_object(ctx, clsid, &classfact_iid, (struct IUnknown **)&factory);
50         if (!W_ERROR_IS_OK(error)) {
51                 DEBUG(3, ("Unable to obtain class object for %s\n", GUID_string(NULL, clsid)));
52                 return error;
53         }
54
55         /* Run IClassFactory::CreateInstance() */
56         error = IClassFactory_CreateInstance(factory, ctx, NULL, &classfact_iid, &iunk);
57         if (!W_ERROR_IS_OK(error)) {
58                 DEBUG(3, ("Error while calling IClassFactory::CreateInstance : %s\n", win_errstr(error)));
59                 return error;
60         }
61
62         if (!iunk) {
63                 DEBUG(0, ("IClassFactory_CreateInstance returned success but result pointer is still NULL!\n"));
64                 return WERR_GENERAL_FAILURE;
65         }
66         
67         /* Release class object */
68         IUnknown_Release(factory, ctx);
69         
70         error = WERR_OK;
71         
72         /* Do one or more QueryInterface calls */
73         for (i = 0; i < num_ifaces; i++) {
74                 results[i] = IUnknown_QueryInterface(iunk, ctx, &iid[i], &ip[i]);
75                 if (!W_ERROR_IS_OK(results[i])) error = results[i];
76         }
77
78         return error;
79 }
80
81 WERROR com_get_class_object(struct com_context *ctx, struct GUID *clsid, struct GUID *iid, struct IUnknown **ip)
82 {
83         struct IUnknown *iu;
84         
85         iu = com_class_by_clsid(ctx, clsid);
86         if (!iu) {
87                 return WERR_CLASS_NOT_REGISTERED;
88         }
89         
90         return IUnknown_QueryInterface(iu, ctx, iid, ip);
91 }
92
93 NTSTATUS com_init(void)
94 {
95         init_module_fn static_init[] = STATIC_COM_MODULES;
96         init_module_fn *shared_init = load_samba_modules(NULL, "com");
97
98         run_init_functions(static_init);
99         run_init_functions(shared_init);
100
101         talloc_free(shared_init);
102         
103         return NT_STATUS_OK;    
104 }