062d1360ac823b94cfcf18a94b7b3fb5cc90ad87
[sfrench/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 "../lib/util/dlinklist.h"
23 #include "lib/com/com.h"
24 #include "lib/events/events.h"
25 #include "librpc/gen_ndr/com_dcom.h"
26
27 WERROR com_init_ctx(struct com_context **ctx, struct tevent_context *event_ctx)
28 {
29         *ctx = talloc(NULL, struct com_context);
30         if (event_ctx == NULL) {
31                 event_ctx = event_context_init(*ctx);
32         }
33         (*ctx)->event_ctx = event_ctx;
34         return WERR_OK;
35 }
36
37 WERROR com_create_object(struct com_context *ctx, struct GUID *clsid, int num_ifaces, struct GUID *iid, struct IUnknown **ip, WERROR *results)
38 {
39         struct IUnknown *iunk = NULL;
40         struct IClassFactory *factory;
41         WERROR error;
42         int i;
43         struct GUID classfact_iid;
44
45         GUID_from_string(NDR_ICLASSFACTORY_UUID, &classfact_iid);
46
47         /* Obtain class object */
48         error = com_get_class_object(ctx, clsid, &classfact_iid, (struct IUnknown **)&factory);
49         if (!W_ERROR_IS_OK(error)) {
50                 DEBUG(3, ("Unable to obtain class object for %s\n", GUID_string(NULL, clsid)));
51                 return error;
52         }
53
54         /* Run IClassFactory::CreateInstance() */
55         error = IClassFactory_CreateInstance(factory, ctx, NULL, &classfact_iid, &iunk);
56         if (!W_ERROR_IS_OK(error)) {
57                 DEBUG(3, ("Error while calling IClassFactory::CreateInstance : %s\n", win_errstr(error)));
58                 return error;
59         }
60
61         if (!iunk) {
62                 DEBUG(0, ("IClassFactory_CreateInstance returned success but result pointer is still NULL!\n"));
63                 return WERR_GENERAL_FAILURE;
64         }
65         
66         /* Release class object */
67         IUnknown_Release(factory, ctx);
68         
69         error = WERR_OK;
70         
71         /* Do one or more QueryInterface calls */
72         for (i = 0; i < num_ifaces; i++) {
73                 results[i] = IUnknown_QueryInterface(iunk, ctx, &iid[i], &ip[i]);
74                 if (!W_ERROR_IS_OK(results[i])) error = results[i];
75         }
76
77         return error;
78 }
79
80 WERROR com_get_class_object(struct com_context *ctx, struct GUID *clsid, struct GUID *iid, struct IUnknown **ip)
81 {
82         struct IUnknown *iu;
83         
84         iu = com_class_by_clsid(ctx, clsid);
85         if (!iu) {
86                 return WERR_CLASS_NOT_REGISTERED;
87         }
88         
89         return IUnknown_QueryInterface(iu, ctx, iid, ip);
90 }