Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // IntBankDataFetcher.cpp: определяет экспортированные функции для приложения DLL.
- //
- #include "stdafx.h"
- #include "IntBankDataFetcher.h"
- #include "OleAuto.h"
- #ifdef _WINDOWS
- static HINSTANCE loadJVMLibrary(void);
- typedef jint(JNICALL *CreateJavaVM_t)(JavaVM **, void **, JavaVMInitArgs *);
- #endif
- using namespace std;
- static const wchar_t *g_MethodNames[] = { L"FetchBankData" };
- static const wchar_t *g_MethodNamesRu[] = { L"ПолучитьДанныеБанкКлиентов" };
- static const wchar_t g_kClassNames[] = L"CIntBankDataFetcher";
- static IAddInDefBase *pAsyncEvent = NULL;
- uint32_t convToShortWchar(WCHAR_T** Dest, const wchar_t* Source, uint32_t len = 0);
- uint32_t convFromShortWchar(wchar_t** Dest, const WCHAR_T* Source, uint32_t len = 0);
- uint32_t getLenShortWcharStr(const WCHAR_T* Source);
- static AppCapabilities g_capabilities = eAppCapabilitiesInvalid;
- static WcharWrapper s_names(g_kClassNames);
- wchar_t* getJVMBankFetcherStringResult();
- //---------------------------------------------------------------------------//
- long GetClassObject(const WCHAR_T* wsName, IComponentBase** pInterface)
- {
- if (!*pInterface)
- {
- *pInterface = new CIntBankDataFetcher();
- return (long)*pInterface;
- }
- return 0;
- }
- //---------------------------------------------------------------------------//
- AppCapabilities SetPlatformCapabilities(const AppCapabilities capabilities)
- {
- g_capabilities = capabilities;
- return eAppCapabilitiesLast;
- }
- //---------------------------------------------------------------------------//
- long DestroyObject(IComponentBase** pIntf)
- {
- if (!*pIntf)
- return -1;
- delete *pIntf;
- *pIntf = 0;
- return 0;
- }
- //---------------------------------------------------------------------------//
- const WCHAR_T* GetClassNames()
- {
- return s_names;
- }
- //---------------------------------------------------------------------------//
- CIntBankDataFetcher::CIntBankDataFetcher()
- {
- m_iMemory = 0;
- m_iConnect = 0;
- }
- //---------------------------------------------------------------------------//
- CIntBankDataFetcher::~CIntBankDataFetcher()
- {
- }
- //---------------------------------------------------------------------------//
- bool CIntBankDataFetcher::Init(void* pConnection)
- {
- m_iConnect = (IAddInDefBase*)pConnection;
- return m_iConnect != NULL;
- }
- //---------------------------------------------------------------------------//
- long CIntBankDataFetcher::GetInfo()
- {
- // Component should put supported component technology version
- // This component supports 2.0 version
- return 2000;
- }
- //---------------------------------------------------------------------------//
- void CIntBankDataFetcher::Done()
- {
- }
- /////////////////////////////////////////////////////////////////////////////
- // ILanguageExtenderBase
- //---------------------------------------------------------------------------//
- bool CIntBankDataFetcher::RegisterExtensionAs(WCHAR_T** wsExtensionName)
- {
- const wchar_t *wsExtension = L"IntBankDataFetcherExtension";
- size_t iActualSize = ::wcslen(wsExtension) + 1;
- WCHAR_T* dest = 0;
- if (m_iMemory)
- {
- if (m_iMemory->AllocMemory((void**)wsExtensionName, iActualSize * sizeof(WCHAR_T)))
- ::convToShortWchar(wsExtensionName, wsExtension, iActualSize);
- return true;
- }
- return false;
- }
- //---------------------------------------------------------------------------//
- long CIntBankDataFetcher::GetNProps()
- {
- return ePropLast;
- }
- //---------------------------------------------------------------------------//
- long CIntBankDataFetcher::FindProp(const WCHAR_T* wsPropName)
- {
- return -1;
- }
- //---------------------------------------------------------------------------//
- const WCHAR_T* CIntBankDataFetcher::GetPropName(long lPropNum, long lPropAlias)
- {
- return 0;
- }
- //---------------------------------------------------------------------------//
- bool CIntBankDataFetcher::GetPropVal(const long lPropNum, tVariant* pvarPropVal)
- {
- return false;
- }
- //---------------------------------------------------------------------------//
- bool CIntBankDataFetcher::SetPropVal(const long lPropNum, tVariant* varPropVal)
- {
- return false;
- }
- //---------------------------------------------------------------------------//
- bool CIntBankDataFetcher::IsPropReadable(const long lPropNum)
- {
- return false;
- }
- //---------------------------------------------------------------------------//
- bool CIntBankDataFetcher::IsPropWritable(const long lPropNum)
- {
- return false;
- }
- //---------------------------------------------------------------------------//
- long CIntBankDataFetcher::GetNMethods()
- {
- return eMethLast;
- }
- //---------------------------------------------------------------------------//
- long CIntBankDataFetcher::FindMethod(const WCHAR_T* wsMethodName)
- {
- long plMethodNum = -1;
- wchar_t* name = 0;
- ::convFromShortWchar(&name, wsMethodName);
- plMethodNum = findName(g_MethodNames, name, eMethLast);
- if (plMethodNum == -1)
- plMethodNum = findName(g_MethodNamesRu, name, eMethLast);
- delete[] name;
- return plMethodNum;
- }
- //---------------------------------------------------------------------------//
- const WCHAR_T* CIntBankDataFetcher::GetMethodName(const long lMethodNum, const long lMethodAlias)
- {
- if (lMethodNum >= eMethLast)
- return NULL;
- wchar_t *wsCurrentName = NULL;
- WCHAR_T *wsMethodName = NULL;
- int iActualSize = 0;
- switch (lMethodAlias)
- {
- case 0:
- wsCurrentName = (wchar_t*)g_MethodNames[lMethodNum];
- break;
- case 1:
- wsCurrentName = (wchar_t*)g_MethodNamesRu[lMethodNum];
- break;
- default:
- return 0;
- }
- iActualSize = wcslen(wsCurrentName) + 1;
- if (m_iMemory && wsCurrentName)
- {
- if (m_iMemory->AllocMemory((void**)&wsMethodName, iActualSize * sizeof(WCHAR_T)))
- ::convToShortWchar(&wsMethodName, wsCurrentName, iActualSize);
- }
- return wsMethodName;
- }
- //---------------------------------------------------------------------------//
- long CIntBankDataFetcher::GetNParams(const long lMethodNum)
- {
- switch (lMethodNum)
- {
- case eFetchBankData:
- return 0;
- default:
- return 0;
- }
- return 0;
- }
- //---------------------------------------------------------------------------//
- bool CIntBankDataFetcher::GetParamDefValue(const long lMethodNum, const long lParamNum,
- tVariant *pvarParamDefValue)
- {
- TV_VT(pvarParamDefValue) = VTYPE_EMPTY;
- switch (lMethodNum)
- {
- case eFetchBankData:
- break;
- default:
- return false;
- }
- return false;
- }
- //---------------------------------------------------------------------------//
- bool CIntBankDataFetcher::HasRetVal(const long lMethodNum)
- {
- switch (lMethodNum)
- {
- case eFetchBankData:
- return true;
- default:
- return false;
- }
- return false;
- }
- //---------------------------------------------------------------------------//
- bool CIntBankDataFetcher::CallAsProc(const long lMethodNum, tVariant* paParams,
- const long lSizeArray)
- {
- switch (lMethodNum)
- {
- case eFetchBankData:
- break;
- default:
- return false;
- }
- return false;
- }
- //---------------------------------------------------------------------------//
- bool CIntBankDataFetcher::CallAsFunc(const long lMethodNum, tVariant* pvarRetValue,
- tVariant* paParams, const long lSizeArray)
- {
- switch (lMethodNum)
- {
- case eFetchBankData:
- {
- //Тестовая строка
- //wchar_t *testString = L"TestString";
- wchar_t *xmlPathString = getJVMBankFetcherStringResult();
- int iActualSize = ::wcslen(xmlPathString) + 1;
- TV_VT(pvarRetValue) = VTYPE_PWSTR;
- pvarRetValue->pwstrVal = xmlPathString;
- pvarRetValue->strLen = ::wcslen(pvarRetValue->pwstrVal);
- if (m_iMemory)
- {
- //m_iMemory->AllocMemory((void**)&pvarRetValue->pwstrVal, iActualSize * sizeof(WCHAR_T));
- if (m_iMemory->AllocMemory((void**)&pvarRetValue->pwstrVal, iActualSize * sizeof(WCHAR_T)))
- ::convToShortWchar(&pvarRetValue->pwstrVal, xmlPathString, iActualSize);
- }
- return true;
- }
- break;
- }
- return true;
- }
- //---------------------------------------------------------------------------//
- void CIntBankDataFetcher::SetLocale(const WCHAR_T* loc)
- {
- _wsetlocale(LC_ALL, loc);
- }
- /////////////////////////////////////////////////////////////////////////////
- // LocaleBase
- //---------------------------------------------------------------------------//
- bool CIntBankDataFetcher::setMemManager(void* mem)
- {
- m_iMemory = (IMemoryManager*)mem;
- return m_iMemory != 0;
- }
- //---------------------------------------------------------------------------//
- void CIntBankDataFetcher::addError(uint32_t wcode, const wchar_t* source,
- const wchar_t* descriptor, long code)
- {
- if (m_iConnect)
- {
- WCHAR_T *err = 0;
- WCHAR_T *descr = 0;
- ::convToShortWchar(&err, source);
- ::convToShortWchar(&descr, descriptor);
- m_iConnect->AddError(wcode, err, descr, code);
- delete[] err;
- delete[] descr;
- }
- }
- //---------------------------------------------------------------------------//
- long CIntBankDataFetcher::findName(const wchar_t* names[], const wchar_t* name,
- const uint32_t size) const
- {
- long ret = -1;
- for (uint32_t i = 0; i < size; i++)
- {
- if (!wcscmp(names[i], name))
- {
- ret = i;
- break;
- }
- }
- return ret;
- }
- //---------------------------------------------------------------------------//
- uint32_t convToShortWchar(WCHAR_T** Dest, const wchar_t* Source, uint32_t len)
- {
- if (!len)
- len = ::wcslen(Source) + 1;
- if (!*Dest)
- *Dest = new WCHAR_T[len];
- WCHAR_T* tmpShort = *Dest;
- wchar_t* tmpWChar = (wchar_t*)Source;
- uint32_t res = 0;
- ::memset(*Dest, 0, len * sizeof(WCHAR_T));
- for (; len; --len, ++res, ++tmpWChar, ++tmpShort)
- {
- *tmpShort = (WCHAR_T)*tmpWChar;
- }
- return res;
- }
- //---------------------------------------------------------------------------//
- uint32_t convFromShortWchar(wchar_t** Dest, const WCHAR_T* Source, uint32_t len)
- {
- if (!len)
- len = getLenShortWcharStr(Source) + 1;
- if (!*Dest)
- *Dest = new wchar_t[len];
- wchar_t* tmpWChar = *Dest;
- WCHAR_T* tmpShort = (WCHAR_T*)Source;
- uint32_t res = 0;
- ::memset(*Dest, 0, len * sizeof(wchar_t));
- for (; len; --len, ++res, ++tmpWChar, ++tmpShort)
- {
- *tmpWChar = (wchar_t)* tmpShort;
- }
- return res;
- }
- //---------------------------------------------------------------------------//
- uint32_t getLenShortWcharStr(const WCHAR_T* Source)
- {
- uint32_t res = 0;
- WCHAR_T *tmpShort = (WCHAR_T*)Source;
- while (*tmpShort++)
- ++res;
- return res;
- }
- //---------------------------------------------------------------------------//
- WcharWrapper::WcharWrapper(const wchar_t* str) : m_str_wchar(NULL)
- {
- if (str)
- {
- int len = wcslen(str);
- m_str_wchar = new wchar_t[len + 1];
- memset(m_str_wchar, 0, sizeof(wchar_t) * (len + 1));
- memcpy(m_str_wchar, str, sizeof(wchar_t) * len);
- }
- }
- //---------------------------------------------------------------------------//
- WcharWrapper::~WcharWrapper()
- {
- if (m_str_wchar)
- {
- delete[] m_str_wchar;
- m_str_wchar = NULL;
- }
- }
- wchar_t* getJVMBankFetcherStringResult()
- {
- wchar_t* xmlStr;
- JavaVMOption options[2];
- JavaVMInitArgs vm_args;
- JavaVM *jvm;
- JNIEnv *env;
- long status;
- jclass class_BankDataFetcher;
- jclass class_String;
- jobjectArray args;
- jmethodID id_getData;
- #ifdef _WINDOWS
- HINSTANCE hjvmlib;
- CreateJavaVM_t createJavaVM;
- #endif
- char* scriptcat;
- scriptcat = getenv("SCRIPT_CAT");
- const char jarlib[] = "\\AccountDataFetcher\\BankDataFetcher-1.0-jar-with-dependencies.jar";
- char *fpath = new char[strlen(scriptcat) + strlen(jarlib) + 1];
- fpath[0] = '\0';
- strcat(fpath, scriptcat);
- strcat(fpath, jarlib);
- const char *fOptionString = "-Djava.class.path=";
- char fopt[1200];
- strcpy(fopt, fOptionString);
- strcat(fopt, fpath);
- size_t size = strlen(fopt);
- size++;
- fopt[size] = '\0';
- options[0].optionString = fopt;
- options[1].optionString = "-Xrs";
- memset(&vm_args, 0, sizeof(vm_args));
- vm_args.version = JNI_VERSION_1_8;
- vm_args.nOptions = 2;
- vm_args.options = options;
- vm_args.ignoreUnrecognized = JNI_FALSE;
- #ifdef _WINDOWS
- hjvmlib = loadJVMLibrary();
- createJavaVM = (CreateJavaVM_t)GetProcAddress(hjvmlib, "JNI_CreateJavaVM");
- __try {
- status = (*createJavaVM)(&jvm, (void **)&env, &vm_args);
- }
- __except (GetExceptionCode() == EXCEPTION_BREAKPOINT ?
- EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
- {
- }
- #else
- __try {
- status = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);
- }
- __except (GetExceptionCode() == EXCEPTION_BREAKPOINT ?
- EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
- {
- }
- #endif
- if (status == JNI_ERR)
- {
- fprintf(stderr, "Error creating VM\n");
- return xmlStr;
- }
- class_BankDataFetcher = env->FindClass("app/***/bankdatafetcher/BankDataFetcher");
- id_getData = env->GetStaticMethodID(class_BankDataFetcher, "getData", "([Ljava/lang/String;)Ljava/lang/String;");
- class_String = env->FindClass("java/lang/String");
- args = env->NewObjectArray(0, class_String, NULL);
- jobject obj_ret = env->CallStaticObjectMethod(class_BankDataFetcher, id_getData, args);
- //env->CallStaticVoidMethod(class_BankDataFetcher, id_getData, args);
- jstring str_ret = (jstring)obj_ret;
- LPCSTR nativeString = env->GetStringUTFChars(str_ret, 0);
- int lenA = lstrlenA(nativeString);
- int lenW;
- BSTR unicodestr;
- lenW = ::MultiByteToWideChar(CP_ACP, 0, nativeString, lenA, 0, 0);
- if (lenW > 0)
- {
- // Check whether conversion was successful
- unicodestr = ::SysAllocStringLen(0, lenW);
- ::MultiByteToWideChar(CP_ACP, 0, nativeString, lenA, unicodestr, lenW);
- }
- else
- {
- // handle the error
- }
- jvm->DestroyJavaVM();
- return unicodestr;
- }
- #ifdef _WINDOWS
- static int GetStringFromRegistry(HKEY key, const wchar_t *name, wchar_t *buf, jint bufsize)
- {
- DWORD type, size;
- int returnVal;
- return RegQueryValueEx(key, name, 0, &type, 0, &size) == 0
- && type == REG_SZ
- && size < (unsigned int)bufsize
- && RegQueryValueEx(key, name, 0, 0, (LPBYTE)buf, &size) == 0;
- }
- static void GetPublicJREHome(wchar_t *buf, jint bufsize)
- {
- HKEY key, subkey;
- wchar_t version[MAX_PATH];
- /* Find the current version of the JRE */
- wchar_t *JRE_KEY = L"Software\\JavaSoft\\Java Runtime Environment";
- if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_READ, &key) != 0)
- {
- fprintf(stderr, "Error opening registry key '%s'\n", JRE_KEY);
- exit(1);
- }
- if (!GetStringFromRegistry(key, L"CurrentVersion", version, sizeof(version)))
- {
- fprintf(stderr, "Failed reading value of registry key:\n\t%s\\CurrentVersion\n", JRE_KEY);
- RegCloseKey(key);
- exit(1);
- }
- /* Find directory where the current version is installed. */
- if (RegOpenKeyEx(key, version, 0, KEY_READ, &subkey) != 0)
- {
- fprintf(stderr, "Error opening registry key '%s\\%s'\n", JRE_KEY, version);
- RegCloseKey(key);
- exit(1);
- }
- if (!GetStringFromRegistry(subkey, L"JavaHome", buf, bufsize))
- {
- fprintf(stderr, "Failed reading value of registry key:\n\t%s\\%s\\JavaHome\n",
- JRE_KEY, version);
- RegCloseKey(key);
- RegCloseKey(subkey);
- exit(1);
- }
- RegCloseKey(key);
- RegCloseKey(subkey);
- }
- static HINSTANCE loadJVMLibrary(void)
- {
- HINSTANCE h1, h2;
- int len;
- wchar_t msvcdll[MAX_PATH];
- wchar_t javadll[MAX_PATH];
- GetPublicJREHome(msvcdll, MAX_PATH);
- wcscpy_s(javadll, msvcdll);
- wcsncat_s(msvcdll, L"\\bin\\msvcr120.dll", MAX_PATH - wcslen(msvcdll));
- msvcdll[MAX_PATH - 1] = '\0';
- wcsncat_s(javadll, L"\\bin\\server\\jvm.dll", MAX_PATH - wcslen(javadll));
- javadll[MAX_PATH - 1] = '\0';
- h1 = LoadLibrary(msvcdll);
- if (h1 == NULL)
- {
- fprintf(stderr, "Can't load library msvcr120.dll\n");
- exit(1);
- }
- h2 = LoadLibrary(javadll);
- if (h2 == NULL)
- {
- fprintf(stderr, "Can't load library jvm.dll\n");
- exit(1);
- }
- return h2;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment