include: Update the PEB and TEB structures.
[wine.git] / dlls / wbemprox / process.c
blob0fdd9340d1660c584b38a385cc6f761baff288be
1 /*
2 * Win32_Process methods implementation
4 * Copyright 2013 Hans Leidekker for CodeWeavers
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.
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.
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
21 #define COBJMACROS
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wbemcli.h"
29 #include "wine/debug.h"
30 #include "wbemprox_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);
34 static HRESULT get_owner( VARIANT *user, VARIANT *domain, VARIANT *retval )
36 DWORD len;
37 UINT error = 8;
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;
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;
53 error = 0;
55 done:
56 if (error)
58 VariantClear( user );
59 VariantClear( domain );
61 set_variant( VT_UI4, error, NULL, retval );
62 return S_OK;
65 HRESULT process_get_owner( IWbemClassObject *obj, IWbemContext *context, IWbemClassObject *in, IWbemClassObject **out )
67 VARIANT user, domain, retval;
68 IWbemClassObject *sig, *out_params = NULL;
69 HRESULT hr;
71 TRACE("%p, %p, %p, %p\n", obj, context, in, out);
73 hr = create_signature( WBEMPROX_NAMESPACE_CIMV2, L"Win32_Process", L"GetOwner", PARAM_OUT, &sig );
74 if (hr != S_OK) return hr;
76 if (out)
78 hr = IWbemClassObject_SpawnInstance( sig, 0, &out_params );
79 if (hr != S_OK)
81 IWbemClassObject_Release( sig );
82 return hr;
85 VariantInit( &user );
86 VariantInit( &domain );
87 hr = get_owner( &user, &domain, &retval );
88 if (hr != S_OK) goto done;
89 if (out_params)
91 if (!V_UI4( &retval ))
93 hr = IWbemClassObject_Put( out_params, L"User", 0, &user, CIM_STRING );
94 if (hr != S_OK) goto done;
95 hr = IWbemClassObject_Put( out_params, L"Domain", 0, &domain, CIM_STRING );
96 if (hr != S_OK) goto done;
98 hr = IWbemClassObject_Put( out_params, L"ReturnValue", 0, &retval, CIM_UINT32 );
101 done:
102 VariantClear( &user );
103 VariantClear( &domain );
104 IWbemClassObject_Release( sig );
105 if (hr == S_OK && out)
107 *out = out_params;
108 IWbemClassObject_AddRef( out_params );
110 if (out_params) IWbemClassObject_Release( out_params );
111 return hr;
114 HRESULT process_create( IWbemClassObject *obj, IWbemContext *context, IWbemClassObject *in, IWbemClassObject **out )
116 VARIANT command_line, current_directory, startup_info;
117 HRESULT ret = WBEM_E_INVALID_PARAMETER, hr;
118 IWbemClassObject *sig, *out_params = NULL;
119 PROCESS_INFORMATION pi;
120 STARTUPINFOW si;
121 UINT32 error;
122 VARIANT v;
123 BOOL bret;
124 CIMTYPE type;
126 FIXME("%p, %p, %p, %p stub\n", obj, context, in, out);
128 *out = NULL;
130 if ((hr = create_signature( WBEMPROX_NAMESPACE_CIMV2, L"Win32_Process", L"Create", PARAM_OUT, &sig ))) return hr;
132 VariantInit( &command_line );
133 VariantInit( &current_directory );
134 VariantInit( &startup_info );
136 if (FAILED(hr = IWbemClassObject_Get( in, L"CommandLine", 0, &command_line, &type, NULL ))
137 || V_VT( &command_line ) != VT_BSTR)
138 WARN( "invalid CommandLine, hr %#lx, type %u\n", hr, V_VT( &command_line ));
139 else
140 TRACE( "CommandLine %s.\n", debugstr_w( V_BSTR( &command_line )));
142 if (FAILED(hr = IWbemClassObject_Get( in, L"CurrentDirectory", 0, &current_directory, &type, NULL ))
143 || V_VT( &current_directory ) != VT_BSTR)
144 WARN("invalid CurrentDirectory, hr %#lx, type %u\n", hr, V_VT( &current_directory ));
145 else
146 TRACE( "CurrentDirectory %s.\n", debugstr_w( V_BSTR( &current_directory )));
148 if (SUCCEEDED(IWbemClassObject_Get( in, L"ProcessStartupInformation", 0, &startup_info, &type, NULL ))
149 && V_VT( &startup_info ) == VT_UNKNOWN && V_UNKNOWN( &startup_info ))
150 FIXME( "ProcessStartupInformation is not implemented, vt_type %u, type %lu, val %p\n",
151 V_VT( &startup_info ), type, V_UNKNOWN( &startup_info ));
153 if (out && (hr = IWbemClassObject_SpawnInstance( sig, 0, &out_params )))
155 ret = hr;
156 goto done;
159 memset( &si, 0, sizeof(si) );
160 si.cb = sizeof(si);
162 if (V_VT( &command_line ) == VT_BSTR && V_BSTR( &command_line ))
164 bret = CreateProcessW( NULL, V_BSTR( &command_line ), NULL, NULL, FALSE, 0L,
165 V_VT( &current_directory ) == VT_BSTR ? V_BSTR( &current_directory ) : NULL,
166 NULL, &si, &pi );
167 TRACE( "CreateProcessW ret %d, GetLastError() %lu\n", bret, GetLastError() );
168 if (bret)
170 CloseHandle( pi.hThread );
171 CloseHandle( pi.hProcess );
172 error = 0;
174 else
176 switch (GetLastError())
178 case ERROR_FILE_NOT_FOUND:
179 case ERROR_PATH_NOT_FOUND:
180 error = 9;
181 break;
182 case ERROR_ACCESS_DENIED:
183 error = 2;
184 break;
185 default:
186 error = 8;
187 break;
191 else
193 bret = FALSE;
194 error = 21;
197 if (out)
199 VariantInit( &v );
201 V_VT( &v ) = VT_UI4;
202 V_UI4( &v ) = pi.dwProcessId;
204 if (bret && (ret = IWbemClassObject_Put( out_params, L"ProcessId", 0, &v, 0 ))) goto done;
206 V_UI4( &v ) = error;
207 if ((ret = IWbemClassObject_Put( out_params, L"ReturnValue", 0, &v, 0 ))) goto done;
209 *out = out_params;
210 IWbemClassObject_AddRef( out_params );
212 ret = S_OK;
214 done:
215 IWbemClassObject_Release( sig );
216 if (out_params) IWbemClassObject_Release( out_params );
217 VariantClear( &command_line );
218 VariantClear( &current_directory );
219 VariantClear( &startup_info );
220 TRACE( "ret %#lx\n", ret );
221 return ret;