Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.5 KiB

  1. // hosttest.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "objbase.h"
  5. #include "initguid.h"
  6. #include <stdio.h>
  7. #include <activscp.h>
  8. // CLSID for our implementation of IActiveScriptingSite
  9. // {838E2F5E-E20E-11d2-B355-00105A1F473A}
  10. DEFINE_GUID(CLSID_WmiActiveScriptingSite,
  11. 0x838e2f5e, 0xe20e, 0x11d2, 0xb3, 0x55, 0x0, 0x10, 0x5a, 0x1f, 0x47, 0x3a);
  12. WCHAR * ReadScript(char * pFileName);
  13. int main(int argc, char* argv[])
  14. {
  15. if (2 != argc)
  16. {
  17. printf ("Usage: hosttest <scriptfile>\n");
  18. return 1;
  19. }
  20. LPWSTR pScriptText = ReadScript (argv[1]);
  21. HRESULT sc = CoInitialize(0);
  22. // Get the active script site
  23. IActiveScriptSite *pSite = NULL;
  24. HRESULT hr = CoCreateInstance (CLSID_WmiActiveScriptingSite,NULL,
  25. CLSCTX_INPROC_SERVER,IID_IActiveScriptSite, (void**) &pSite);
  26. // Get the scripting engine
  27. CLSID clsid;
  28. hr = CLSIDFromProgID (L"JScript", &clsid);
  29. IActiveScript* pScriptEngine = NULL;
  30. hr =CoCreateInstance (clsid, NULL, CLSCTX_INPROC_SERVER, IID_IActiveScript, (void**) &pScriptEngine);
  31. IActiveScriptParse* pParse;
  32. sc = pScriptEngine->QueryInterface(IID_IActiveScriptParse, (void**)&pParse);
  33. if(FAILED(sc))
  34. return 1;
  35. sc = pParse->InitNew();
  36. // Bind the host to the engine
  37. sc = pScriptEngine->SetScriptSite(pSite);
  38. pSite->Release();
  39. // Register the "this" pointer
  40. sc = pScriptEngine->AddNamedItem(L"instance",
  41. SCRIPTITEM_ISVISIBLE | SCRIPTITEM_NOCODE | SCRIPTITEM_GLOBALMEMBERS);
  42. if(FAILED(sc))
  43. return 1;
  44. EXCEPINFO ei;
  45. sc = pParse->ParseScriptText(
  46. pScriptText,
  47. NULL, NULL, NULL,
  48. 0, 0, 0, NULL, &ei);
  49. if(FAILED(sc))
  50. return 1;
  51. pParse->Release();
  52. sc = pScriptEngine->SetScriptState(SCRIPTSTATE_CONNECTED);
  53. if(FAILED(sc))
  54. return 1;
  55. pScriptEngine->Release();
  56. CoUninitialize();
  57. printf("Terminating normally\n");
  58. return 0;
  59. }
  60. WCHAR * ReadScript(char * pFileName)
  61. {
  62. FILE *fp;
  63. BOOL bUnicode = FALSE;
  64. BOOL bBigEndian = FALSE;
  65. // Make sure the file exists and can be opened
  66. fp = fopen(pFileName, "rb");
  67. if (!fp)
  68. {
  69. printf("\nCant open file %s", pFileName);
  70. return NULL;
  71. }
  72. // Determine the size of the file
  73. // ==============================
  74. fseek(fp, 0, SEEK_END);
  75. long lSize = ftell(fp); // add a bit extra for ending space and null NULL
  76. fseek(fp, 0, SEEK_SET);
  77. // Check for UNICODE source file.
  78. // ==============================
  79. BYTE UnicodeSignature[2];
  80. if (fread(UnicodeSignature, sizeof(BYTE), 2, fp) != 2)
  81. {
  82. printf("\nNothing in file %s", pFileName);
  83. fclose(fp);
  84. return NULL;
  85. }
  86. if (UnicodeSignature[0] == 0xFF && UnicodeSignature[1] == 0xFE)
  87. {
  88. LPWSTR pRet = new WCHAR[lSize/2 +2];
  89. if(pRet == NULL)
  90. return NULL;
  91. fread(pRet, 1, lSize-2, fp);
  92. fclose(fp);
  93. return pRet;
  94. }
  95. else
  96. {
  97. fseek(fp, 0, SEEK_SET);
  98. LPSTR pTemp = new char[lSize+1];
  99. memset (pTemp,0,(lSize+1) * sizeof(char));
  100. LPWSTR pRet = new WCHAR[lSize+1];
  101. memset (pRet, 0, (lSize + 1) * sizeof (WCHAR));
  102. if(pRet == NULL || pTemp == NULL)
  103. return NULL;
  104. fread(pTemp, 1, lSize, fp);
  105. fclose(fp);
  106. mbstowcs(pRet, pTemp, lSize);
  107. delete pTemp;
  108. return pRet;
  109. }
  110. return NULL;
  111. }