1 // MIDLWrapper.cpp : Just calls the built-in midl.exe with the given arguments.
2
3 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
4 #include <process.h>
5 #include <stdio.h>
6 #include <string>
7 #include <windows.h>
8
9 using namespace std;
10
wmain(int argc,wchar_t * argv[],wchar_t * envp[])11 int wmain(int argc, wchar_t* argv[], wchar_t* envp[])
12 {
13 #ifndef NDEBUG
14 fwprintf(stderr, L"######### im in ur IDE, compiling ur c0des ########\n");
15 #endif
16
17 int pathIndex = -1;
18 for (int i = 0; envp[i]; ++i)
19 if (!wcsncmp(envp[i], L"PATH=", 5)) {
20 pathIndex = i;
21 break;
22 }
23
24 if (pathIndex == -1) {
25 fwprintf(stderr, L"Couldn't find PATH environment variable!\n");
26 return -1;
27 }
28
29 wchar_t* vcbin = wcsstr(envp[pathIndex], L"Tools\\vcbin");
30 if (!vcbin) {
31 fwprintf(stderr, L"Couldn't find Tools\\vcbin in PATH!\n");
32 return -1;
33 }
34
35 wchar_t saved = *vcbin;
36 *vcbin = 0;
37
38 wchar_t* afterLeadingSemiColon = wcsrchr(envp[pathIndex], ';');
39 if (!afterLeadingSemiColon)
40 afterLeadingSemiColon = envp[pathIndex] + 5; // +5 for the length of "PATH="
41 else
42 afterLeadingSemiColon++;
43
44 *vcbin = saved;
45
46 size_t pathLength = wcslen(envp[pathIndex]);
47
48 wchar_t* trailingSemiColon = wcschr(vcbin, ';');
49 if (!trailingSemiColon)
50 trailingSemiColon = envp[pathIndex] + pathLength;
51
52 int vcbinLength = trailingSemiColon - afterLeadingSemiColon;
53
54 size_t newPathLength = pathLength - vcbinLength;
55
56 wchar_t* newPath = new wchar_t[newPathLength + 1];
57
58 // Copy everything before the vcbin path...
59 wchar_t* d = newPath;
60 wchar_t* s = envp[pathIndex];
61 while (s < afterLeadingSemiColon)
62 *d++ = *s++;
63
64 // Copy everything after the vcbin path...
65 s = trailingSemiColon;
66 while (*d++ = *s++);
67
68 envp[pathIndex] = newPath;
69
70 #ifndef NDEBUG
71 fwprintf(stderr, L"New path: %s\n", envp[pathIndex]);
72 #endif
73
74 wchar_t** newArgv = new wchar_t*[argc + 1];
75 for (int i = 0; i < argc; ++i) {
76 size_t length = wcslen(argv[i]);
77 newArgv[i] = new wchar_t[length + 3];
78 *newArgv[i] = '\"';
79 wcscpy_s(newArgv[i] + 1, length + 2, argv[i]);
80 *(newArgv[i] + 1 + length) = '\"';
81 *(newArgv[i] + 2 + length) = 0;
82 }
83 newArgv[argc] = 0;
84
85 return _wspawnvpe(_P_WAIT, L"midl", newArgv, envp);
86 }
87