xref: /reactos/dll/win32/wbemprox/process.c (revision cdf90707)
1 /*
2  * Win32_Process methods implementation
3  *
4  * Copyright 2013 Hans Leidekker for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #define COBJMACROS
22 
23 #include <stdarg.h>
24 
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wbemcli.h"
28 
29 #include "wine/debug.h"
30 #include "wbemprox_private.h"
31 
32 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);
33 
34 static HRESULT get_owner( VARIANT *user, VARIANT *domain, VARIANT *retval )
35 {
36     DWORD len;
37     UINT error = 8;
38 
39     len = 0;
40     GetUserNameW( NULL, &len );
41     if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) goto done;
42     if (!(V_BSTR( user ) = SysAllocStringLen( NULL, len - 1 ))) goto done;
43     if (!GetUserNameW( V_BSTR( user ), &len )) goto done;
44     V_VT( user ) = VT_BSTR;
45 
46     len = 0;
47     GetComputerNameW( NULL, &len );
48     if (GetLastError() != ERROR_BUFFER_OVERFLOW) goto done;
49     if (!(V_BSTR( domain ) = SysAllocStringLen( NULL, len - 1 ))) goto done;
50     if (!GetComputerNameW( V_BSTR( domain ), &len )) goto done;
51     V_VT( domain ) = VT_BSTR;
52 
53     error = 0;
54 
55 done:
56     if (error)
57     {
58         VariantClear( user );
59         VariantClear( domain );
60     }
61     set_variant( VT_UI4, error, NULL, retval );
62     return S_OK;
63 }
64 
65 HRESULT process_get_owner( IWbemClassObject *obj, IWbemClassObject *in, IWbemClassObject **out )
66 {
67     VARIANT user, domain, retval;
68     IWbemClassObject *sig, *out_params = NULL;
69     HRESULT hr;
70 
71     TRACE("%p, %p, %p\n", obj, in, out);
72 
73     hr = create_signature( class_processW, method_getownerW, PARAM_OUT, &sig );
74     if (hr != S_OK) return hr;
75 
76     if (out)
77     {
78         hr = IWbemClassObject_SpawnInstance( sig, 0, &out_params );
79         if (hr != S_OK)
80         {
81             IWbemClassObject_Release( sig );
82             return hr;
83         }
84     }
85     VariantInit( &user );
86     VariantInit( &domain );
87     hr = get_owner( &user, &domain, &retval );
88     if (hr != S_OK) goto done;
89     if (out_params)
90     {
91         if (!V_UI4( &retval ))
92         {
93             hr = IWbemClassObject_Put( out_params, param_userW, 0, &user, CIM_STRING );
94             if (hr != S_OK) goto done;
95             hr = IWbemClassObject_Put( out_params, param_domainW, 0, &domain, CIM_STRING );
96             if (hr != S_OK) goto done;
97         }
98         hr = IWbemClassObject_Put( out_params, param_returnvalueW, 0, &retval, CIM_UINT32 );
99     }
100 
101 done:
102     VariantClear( &user );
103     VariantClear( &domain );
104     IWbemClassObject_Release( sig );
105     if (hr == S_OK && out)
106     {
107         *out = out_params;
108         IWbemClassObject_AddRef( out_params );
109     }
110     if (out_params) IWbemClassObject_Release( out_params );
111     return hr;
112 }
113