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
- #import "C:\Windows\SysWOW64\msscript.ocx" // msscript.ocx
- #elif _WIN32
- #import "C:\Windows\System32\msscript.ocx" // 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
- tstring wstrLanguage = widen(Language);
- _bstr_t bstrLanguage = SysAllocString(wstrLanguage.c_str());
- pScriptControl->Language = bstrLanguage;
- pScriptControl->SitehWnd = (long)GetSitehWnd();
- pScriptControl->AllowUI = TRUE;
- SysFreeString(bstrLanguage);
- // Execute Statement:
- tstring wstrStatement = widen(Statement);
- _bstr_t bstrStatement = SysAllocString(wstrStatement.c_str());
- pScriptControl->ExecuteStatement(bstrStatement);
- SysFreeString(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
- tstring wstrLanguage = widen(Language);
- _bstr_t bstrLanguage = SysAllocString(wstrLanguage.c_str());
- pScriptControl->Language = bstrLanguage;
- pScriptControl->SitehWnd = (long)GetSitehWnd();
- pScriptControl->AllowUI = TRUE;
- SysFreeString(bstrLanguage);
- // Add Subroutine Code:
- tstring wstrSubroutine = widen(Subroutine);
- _bstr_t bstrSubroutine = SysAllocString(wstrSubroutine.c_str());
- pScriptControl->AddCode(bstrSubroutine);
- SysFreeString(bstrSubroutine);
- // Evaluate Expression:
- tstring wstrExpression = widen(Expression);
- _bstr_t bstrExpression = SysAllocString(wstrExpression.c_str());
- _variant_t Evaluation = pScriptControl->Eval(bstrExpression);
- SysFreeString(bstrExpression);
- // Convert VARIANT to BSTR:
- static _bstr_t stcbstrResult;
- stcbstrResult = (_bstr_t)Evaluation;
- tstring wstrResult = widen((char *)stcbstrResult);
- _bstr_t bstrResult = SysAllocString(wstrResult.c_str());
- wstrResult = widen((char *)bstrResult);
- SysFreeString(bstrExpression);
- // Clean up:
- SafeArrayDestroy(psa);
- CoUninitialize();
- static string strResult;
- strResult = shorten(wstrResult.c_str());
- return (char *)strResult.c_str();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement