MIDLWrapper.cpp 2.4 KB

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