11#include " ..\Project.h"
22#include " expdef.h" // printPeExports
3+ #include " ..\helpers.h"
34#include < filesystem>
45#include < atlconv.h> // CW2A
56#include < algorithm> // transform
@@ -177,30 +178,34 @@ int _wmain(int argc, wchar_t** argv)
177178 return -4 ;
178179 }
179180
180- // Idea copied from vswhere, except whole implementation was re-written from scratch.
181- ISetupConfigurationPtr setupCfg;
182- IEnumSetupInstancesPtr enumInstances;
183-
184181 vector<VisualStudioInfo> instances;
185- auto lcid = GetUserDefaultLCID ();
186182
187- function< void (HRESULT)> hrc = [](HRESULT hr)
183+ // Idea copied from vswhere, except whole implementation was re-written from scratch.
188184 {
189- if (FAILED (hr))
190- throw _com_error (hr);
191- };
185+ ISetupConfigurationPtr setupCfg;
186+ IEnumSetupInstancesPtr enumInstances;
187+
188+ auto lcid = GetUserDefaultLCID ();
189+
190+ function<void (HRESULT)> hrc = [](HRESULT hr)
191+ {
192+ if (FAILED (hr))
193+ {
194+ USES_CONVERSION;
195+ throw exception (CW2A (_com_error (hr).ErrorMessage ()));
196+ }
197+ };
192198
193- try {
194199 hrc (CoInitialize (nullptr ));
195200 hrc (setupCfg.CreateInstance (__uuidof (SetupConfiguration)));
196201 hrc (setupCfg->EnumInstances (&enumInstances));
197202
198- while (true )
203+ while (true )
199204 {
200205 ISetupInstance* p = nullptr ;
201206 unsigned long ul = 0 ;
202207 HRESULT hr = enumInstances->Next (1 , &p, &ul);
203- if (hr != S_OK )
208+ if (hr != S_OK)
204209 break ;
205210
206211 ISetupInstancePtr setupi (p, false );
@@ -220,15 +225,45 @@ int _wmain(int argc, wchar_t** argv)
220225 vsinfo.InstallPath = instpath;
221226 instances.push_back (vsinfo);
222227 }
223-
224- CoUninitialize ();
225- }
226- catch (_com_error ce)
227- {
228- printf (" Error: %S\n " , ce.ErrorMessage ());
229228 }
229+ CoUninitialize ();
230+
231+ auto it = max_element (instances.begin (), instances.end (), [](auto e1 , auto e2 ) { return e1 .version < e2 .version ; });
232+ if (it == instances.end ())
233+ throw exception (" No Visual Studio installation found" );
234+
235+ auto devenv = path (it->InstallPath ).append (" Common7\\ IDE\\ devenv.com" );
236+
237+ auto quoted = [](wstring f) -> wstring
238+ {
239+ return wstring (L" \" " ) + f + L" \" " ;
240+ };
241+
242+ wprintf (L" %s: compile... " , scriptToRun.filename ().c_str ());
243+ wstring cmd = wstring (L" cmd /C " ) + quoted ( quoted (devenv.c_str ()) + L" /build " + quoted (L" Debug^|x64" ) + L" " + quoted (p.GetProjectSaveLocation ()) ) + L" >logBuild.txt" ;
244+ // printf("%S\n", cmd.c_str());
245+ int error = 0 ;
246+ error = _wsystem (cmd.c_str ());
247+ if (error != 0 )
248+ throw exception (" Compilation failured" );
249+
250+ auto dllPath = path (projectDir).append (scriptToRun.stem ().wstring () + L" .dll" );
251+ if ( !exists (dllPath) )
252+ throw exception (sFormat (" Compilation failed: dll does not exists: '%s'" , dllPath.u8string ().c_str ()).c_str ());
253+
254+ HMODULE h = LoadLibraryW (dllPath.c_str ());
255+ if (!h)
256+ ThrowLastError ();
257+
258+ FARPROC proc = GetProcAddress (h, " main" );
259+
260+ if (!proc)
261+ throwFormat (" Dll entry point function 'main' not found." );
230262
231- return 0 ;
263+ printf (" execute:\n\n " );
264+ int r = (int )proc ();
265+ FreeLibrary (h);
266+ return r;
232267}
233268
234269int wmain (int argc, wchar_t ** argv)
0 commit comments