Advertisement
Guest User

MSScriptControl.cpp

a guest
Mar 26th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <comutil.h>
  4. #include <windows.h>
  5. #include <wchar.h>
  6. #include <algorithm>
  7. #include <string>
  8. #include <vector>
  9.  
  10. #if _WIN64 // msscript.ocx on Windows 64-bit
  11.     #import "C:\Windows\SysWOW64\msscript.ocx"
  12. #elif _WIN32 // msscript.ocx on Windows 32-bit
  13.     #import "C:\Windows\System32\msscript.ocx"
  14. #endif
  15.  
  16. using namespace MSScriptControl;
  17. using namespace std;
  18.  
  19. #define DLL extern "C" _declspec(dllexport)
  20.  
  21. typedef basic_string<WCHAR> tstring;
  22. tstring widen(string str)
  23. {
  24.     const size_t wchar_count = str.size() + 1;
  25.     vector<WCHAR> buf(wchar_count);
  26.     return tstring{ buf.data(), (size_t)MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, buf.data(), wchar_count) };
  27. }
  28.  
  29. string shorten(tstring str)
  30. {
  31.     int nbytes = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), (int)str.length(), NULL, 0, NULL, NULL);
  32.     vector<char> buf((size_t)nbytes);
  33.     return string{ buf.data(), (size_t)WideCharToMultiByte(CP_UTF8, 0, str.c_str(), (int)str.length(), buf.data(), nbytes, NULL, NULL) };
  34. }
  35.  
  36. DLL double GetSitehWnd()
  37. {
  38.     return (long)GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
  39. }
  40.  
  41. DLL double ExecuteStatement(char *Statement, char *Language)
  42. {
  43.     HRESULT hr = CoInitialize(NULL);
  44.  
  45.     IScriptControlPtr pScriptControl(__uuidof(ScriptControl));
  46.  
  47.     // Create a VARIANT array of VARIANTs which hold BSTRs
  48.     LPSAFEARRAY psa;
  49.     SAFEARRAYBOUND rgsabound[] = { 3, 0 }; // 3 elements, 0-based
  50.     int i;
  51.  
  52.     psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound);
  53.     if (!psa)
  54.     {
  55.         return E_OUTOFMEMORY;
  56.     }
  57.  
  58.     VARIANT vFlavors[3];
  59.     for (i = 0; i < 3; i++)
  60.     {
  61.         VariantInit(&vFlavors[i]);
  62.         V_VT(&vFlavors[i]) = VT_BSTR;
  63.     }
  64.  
  65.     V_BSTR(&vFlavors[0]) = SysAllocString(OLESTR("Vanilla"));
  66.     V_BSTR(&vFlavors[1]) = SysAllocString(OLESTR("Chocolate"));
  67.     V_BSTR(&vFlavors[2]) = SysAllocString(OLESTR("Espresso Chip"));
  68.  
  69.     long lZero = 0;
  70.     long lOne = 1;
  71.     long lTwo = 2;
  72.  
  73.     // Put Elements to the SafeArray:
  74.     hr = SafeArrayPutElement(psa, &lZero, &vFlavors[0]);
  75.     hr = SafeArrayPutElement(psa, &lOne, &vFlavors[1]);
  76.     hr = SafeArrayPutElement(psa, &lTwo, &vFlavors[2]);
  77.  
  78.     // Free Elements from the SafeArray:
  79.     for (i = 0; i < 3; i++)
  80.     {
  81.         SysFreeString(vFlavors[i].bstrVal);
  82.     }
  83.  
  84.     // Set up Script control properties
  85.     string strLanguage = Language;
  86.     tstring tstrLanguage = widen(strLanguage);
  87.     _bstr_t bstrLanguage = tstrLanguage.c_str();
  88.     pScriptControl->Language = bstrLanguage;
  89.     pScriptControl->SitehWnd = (long)GetSitehWnd();
  90.     pScriptControl->AllowUI = TRUE;
  91.  
  92.     //  Execute Statement:
  93.     string strStatement = Statement;
  94.     tstring tstrStatement = widen(strStatement);
  95.     _bstr_t bstrStatement = tstrStatement.c_str();
  96.     pScriptControl->ExecuteStatement(bstrStatement);
  97.  
  98.  
  99.     //  Clean up:
  100.     SafeArrayDestroy(psa);
  101.  
  102.     CoUninitialize();
  103.     return 0;
  104. }
  105.  
  106. DLL char *EvaluateExpression(char *Expression, char *Subroutine, char *Language)
  107. {
  108.     HRESULT hr = CoInitialize(NULL);
  109.  
  110.     IScriptControlPtr pScriptControl(__uuidof(ScriptControl));
  111.  
  112.     // Create a VARIANT array of VARIANTs which hold BSTRs
  113.     LPSAFEARRAY psa;
  114.     SAFEARRAYBOUND rgsabound[] = { 3, 0 }; // 3 elements, 0-based
  115.     int i;
  116.  
  117.     psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound);
  118.     if (!psa)
  119.     {
  120.         return (char *)E_OUTOFMEMORY;
  121.     }
  122.  
  123.     VARIANT vFlavors[3];
  124.     for (i = 0; i < 3; i++)
  125.     {
  126.         VariantInit(&vFlavors[i]);
  127.         V_VT(&vFlavors[i]) = VT_BSTR;
  128.     }
  129.  
  130.     V_BSTR(&vFlavors[0]) = SysAllocString(OLESTR("Vanilla"));
  131.     V_BSTR(&vFlavors[1]) = SysAllocString(OLESTR("Chocolate"));
  132.     V_BSTR(&vFlavors[2]) = SysAllocString(OLESTR("Espresso Chip"));
  133.  
  134.     long lZero = 0;
  135.     long lOne = 1;
  136.     long lTwo = 2;
  137.  
  138.     // Put Elements to the SafeArray:
  139.     hr = SafeArrayPutElement(psa, &lZero, &vFlavors[0]);
  140.     hr = SafeArrayPutElement(psa, &lOne, &vFlavors[1]);
  141.     hr = SafeArrayPutElement(psa, &lTwo, &vFlavors[2]);
  142.  
  143.     // Free Elements from the SafeArray:
  144.     for (i = 0; i < 3; i++)
  145.     {
  146.         SysFreeString(vFlavors[i].bstrVal);
  147.     }
  148.  
  149.     // Set up Script control properties
  150.     string strLanguage = Language;
  151.     tstring tstrLanguage = widen(strLanguage);
  152.     _bstr_t bstrLanguage = tstrLanguage.c_str();
  153.     pScriptControl->Language = bstrLanguage;
  154.     pScriptControl->SitehWnd = (long)GetSitehWnd();
  155.     pScriptControl->AllowUI = TRUE;
  156.  
  157.     //  Add Subroutine Code:
  158.     string strSubroutine = Subroutine;
  159.     tstring tstrSubroutine = widen(strSubroutine);
  160.     _bstr_t bstrSubroutine = tstrSubroutine.c_str();
  161.     pScriptControl->AddCode(bstrSubroutine);
  162.  
  163.     //  Evaluate Expression:
  164.     string strExpression = Expression;
  165.     tstring tstrExpression = widen(strExpression);
  166.     _bstr_t bstrExpression = tstrExpression.c_str();
  167.     _variant_t Evaluation = pScriptControl->Eval(bstrExpression);
  168.  
  169.  
  170.     // Convert VARIANT to BSTR:
  171.     static _bstr_t bstrResult;
  172.     bstrResult = (_bstr_t)Evaluation;
  173.  
  174.     //  Clean up:
  175.     SafeArrayDestroy(psa);
  176.  
  177.     CoUninitialize();
  178.  
  179.     static string strResult;
  180.     strResult = shorten((wchar_t *)bstrResult);
  181.     return (char *)strResult.c_str();
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement