Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include <comutil.h>
- #include <windows.h>
- #include <wchar.h>
- #include <algorithm>
- #include <string>
- #include <vector>
- #if _WIN64 // msscript.ocx on Windows 64-bit
- #import "C:\Windows\SysWOW64\msscript.ocx"
- #elif _WIN32 // msscript.ocx on Windows 32-bit
- #import "C:\Windows\System32\msscript.ocx"
- #endif
- using namespace MSScriptControl;
- using namespace std;
- #define DLL extern "C" _declspec(dllexport)
- typedef basic_string<WCHAR> tstring;
- tstring widen(string str)
- {
- const size_t wchar_count = str.size() + 1;
- vector<WCHAR> buf(wchar_count);
- return tstring{ buf.data(), (size_t)MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, buf.data(), wchar_count) };
- }
- string shorten(tstring str)
- {
- int nbytes = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), (int)str.length(), NULL, 0, NULL, NULL);
- vector<char> buf((size_t)nbytes);
- return string{ buf.data(), (size_t)WideCharToMultiByte(CP_UTF8, 0, str.c_str(), (int)str.length(), buf.data(), nbytes, NULL, NULL) };
- }
- DLL double GetSitehWnd()
- {
- return (long)GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
- }
- DLL double ExecuteStatement(char *Statement, char *Language)
- {
- HRESULT hr = CoInitialize(NULL);
- IScriptControlPtr pScriptControl(__uuidof(ScriptControl));
- // Create a VARIANT array of VARIANTs which hold BSTRs
- LPSAFEARRAY psa;
- SAFEARRAYBOUND rgsabound[] = { 3, 0 }; // 3 elements, 0-based
- int i;
- psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound);
- if (!psa)
- {
- return E_OUTOFMEMORY;
- }
- VARIANT vFlavors[3];
- for (i = 0; i < 3; i++)
- {
- VariantInit(&vFlavors[i]);
- V_VT(&vFlavors[i]) = VT_BSTR;
- }
- V_BSTR(&vFlavors[0]) = SysAllocString(OLESTR("Vanilla"));
- V_BSTR(&vFlavors[1]) = SysAllocString(OLESTR("Chocolate"));
- V_BSTR(&vFlavors[2]) = SysAllocString(OLESTR("Espresso Chip"));
- long lZero = 0;
- long lOne = 1;
- long lTwo = 2;
- // Put Elements to the SafeArray:
- hr = SafeArrayPutElement(psa, &lZero, &vFlavors[0]);
- hr = SafeArrayPutElement(psa, &lOne, &vFlavors[1]);
- hr = SafeArrayPutElement(psa, &lTwo, &vFlavors[2]);
- // Free Elements from the SafeArray:
- for (i = 0; i < 3; i++)
- {
- SysFreeString(vFlavors[i].bstrVal);
- }
- // Set up Script control properties
- string strLanguage = Language;
- tstring tstrLanguage = widen(strLanguage);
- _bstr_t bstrLanguage = tstrLanguage.c_str();
- pScriptControl->Language = bstrLanguage;
- pScriptControl->SitehWnd = (long)GetSitehWnd();
- pScriptControl->AllowUI = TRUE;
- // Execute Statement:
- string strStatement = Statement;
- tstring tstrStatement = widen(strStatement);
- _bstr_t bstrStatement = tstrStatement.c_str();
- pScriptControl->ExecuteStatement(bstrStatement);
- // Clean up:
- SafeArrayDestroy(psa);
- CoUninitialize();
- return 0;
- }
- DLL char *EvaluateExpression(char *Expression, char *Subroutine, char *Language)
- {
- HRESULT hr = CoInitialize(NULL);
- IScriptControlPtr pScriptControl(__uuidof(ScriptControl));
- // Create a VARIANT array of VARIANTs which hold BSTRs
- LPSAFEARRAY psa;
- SAFEARRAYBOUND rgsabound[] = { 3, 0 }; // 3 elements, 0-based
- int i;
- psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound);
- if (!psa)
- {
- return (char *)E_OUTOFMEMORY;
- }
- VARIANT vFlavors[3];
- for (i = 0; i < 3; i++)
- {
- VariantInit(&vFlavors[i]);
- V_VT(&vFlavors[i]) = VT_BSTR;
- }
- V_BSTR(&vFlavors[0]) = SysAllocString(OLESTR("Vanilla"));
- V_BSTR(&vFlavors[1]) = SysAllocString(OLESTR("Chocolate"));
- V_BSTR(&vFlavors[2]) = SysAllocString(OLESTR("Espresso Chip"));
- long lZero = 0;
- long lOne = 1;
- long lTwo = 2;
- // Put Elements to the SafeArray:
- hr = SafeArrayPutElement(psa, &lZero, &vFlavors[0]);
- hr = SafeArrayPutElement(psa, &lOne, &vFlavors[1]);
- hr = SafeArrayPutElement(psa, &lTwo, &vFlavors[2]);
- // Free Elements from the SafeArray:
- for (i = 0; i < 3; i++)
- {
- SysFreeString(vFlavors[i].bstrVal);
- }
- // Set up Script control properties
- string strLanguage = Language;
- tstring tstrLanguage = widen(strLanguage);
- _bstr_t bstrLanguage = tstrLanguage.c_str();
- pScriptControl->Language = bstrLanguage;
- pScriptControl->SitehWnd = (long)GetSitehWnd();
- pScriptControl->AllowUI = TRUE;
- // Add Subroutine Code:
- string strSubroutine = Subroutine;
- tstring tstrSubroutine = widen(strSubroutine);
- _bstr_t bstrSubroutine = tstrSubroutine.c_str();
- pScriptControl->AddCode(bstrSubroutine);
- // Evaluate Expression:
- string strExpression = Expression;
- tstring tstrExpression = widen(strExpression);
- _bstr_t bstrExpression = tstrExpression.c_str();
- _variant_t Evaluation = pScriptControl->Eval(bstrExpression);
- // Convert VARIANT to BSTR:
- static _bstr_t bstrResult;
- bstrResult = (_bstr_t)Evaluation;
- // Clean up:
- SafeArrayDestroy(psa);
- CoUninitialize();
- static string strResult;
- strResult = shorten((wchar_t *)bstrResult);
- return (char *)strResult.c_str();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement