Guest User

Untitled

a guest
Jun 28th, 2017
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.68 KB | None | 0 0
  1. // IntBankDataFetcher.cpp: определяет экспортированные функции для приложения DLL.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "IntBankDataFetcher.h"
  6. #include "OleAuto.h"
  7.  
  8. #ifdef _WINDOWS
  9.  
  10. static HINSTANCE loadJVMLibrary(void);
  11. typedef jint(JNICALL *CreateJavaVM_t)(JavaVM **, void **, JavaVMInitArgs *);
  12.  
  13. #endif
  14.  
  15. using namespace std;
  16.  
  17. static const wchar_t *g_MethodNames[] = { L"FetchBankData" };
  18.  
  19. static const wchar_t *g_MethodNamesRu[] = { L"ПолучитьДанныеБанкКлиентов" };
  20.  
  21. static const wchar_t g_kClassNames[] = L"CIntBankDataFetcher";
  22. static IAddInDefBase *pAsyncEvent = NULL;
  23.  
  24. uint32_t convToShortWchar(WCHAR_T** Dest, const wchar_t* Source, uint32_t len = 0);
  25. uint32_t convFromShortWchar(wchar_t** Dest, const WCHAR_T* Source, uint32_t len = 0);
  26. uint32_t getLenShortWcharStr(const WCHAR_T* Source);
  27. static AppCapabilities g_capabilities = eAppCapabilitiesInvalid;
  28. static WcharWrapper s_names(g_kClassNames);
  29.  
  30. wchar_t* getJVMBankFetcherStringResult();
  31.  
  32. //---------------------------------------------------------------------------//
  33. long GetClassObject(const WCHAR_T* wsName, IComponentBase** pInterface)
  34. {
  35.     if (!*pInterface)
  36.     {
  37.         *pInterface = new CIntBankDataFetcher();
  38.         return (long)*pInterface;
  39.     }
  40.     return 0;
  41. }
  42. //---------------------------------------------------------------------------//
  43. AppCapabilities SetPlatformCapabilities(const AppCapabilities capabilities)
  44. {
  45.     g_capabilities = capabilities;
  46.     return eAppCapabilitiesLast;
  47. }
  48. //---------------------------------------------------------------------------//
  49. long DestroyObject(IComponentBase** pIntf)
  50. {
  51.     if (!*pIntf)
  52.         return -1;
  53.  
  54.     delete *pIntf;
  55.     *pIntf = 0;
  56.     return 0;
  57. }
  58. //---------------------------------------------------------------------------//
  59. const WCHAR_T* GetClassNames()
  60. {
  61.     return s_names;
  62. }
  63. //---------------------------------------------------------------------------//
  64. CIntBankDataFetcher::CIntBankDataFetcher()
  65. {
  66.     m_iMemory = 0;
  67.     m_iConnect = 0;
  68. }
  69. //---------------------------------------------------------------------------//
  70. CIntBankDataFetcher::~CIntBankDataFetcher()
  71. {
  72. }
  73. //---------------------------------------------------------------------------//
  74. bool CIntBankDataFetcher::Init(void* pConnection)
  75. {
  76.     m_iConnect = (IAddInDefBase*)pConnection;
  77.     return m_iConnect != NULL;
  78. }
  79. //---------------------------------------------------------------------------//
  80. long CIntBankDataFetcher::GetInfo()
  81. {
  82.     // Component should put supported component technology version
  83.     // This component supports 2.0 version
  84.     return 2000;
  85. }
  86. //---------------------------------------------------------------------------//
  87. void CIntBankDataFetcher::Done()
  88. {
  89.  
  90. }
  91. /////////////////////////////////////////////////////////////////////////////
  92. // ILanguageExtenderBase
  93. //---------------------------------------------------------------------------//
  94. bool CIntBankDataFetcher::RegisterExtensionAs(WCHAR_T** wsExtensionName)
  95. {
  96.     const wchar_t *wsExtension = L"IntBankDataFetcherExtension";
  97.     size_t iActualSize = ::wcslen(wsExtension) + 1;
  98.     WCHAR_T* dest = 0;
  99.  
  100.     if (m_iMemory)
  101.     {
  102.         if (m_iMemory->AllocMemory((void**)wsExtensionName, iActualSize * sizeof(WCHAR_T)))
  103.             ::convToShortWchar(wsExtensionName, wsExtension, iActualSize);
  104.         return true;
  105.     }
  106.     return false;
  107. }
  108. //---------------------------------------------------------------------------//
  109. long CIntBankDataFetcher::GetNProps()
  110. {
  111.     return ePropLast;
  112. }
  113. //---------------------------------------------------------------------------//
  114. long CIntBankDataFetcher::FindProp(const WCHAR_T* wsPropName)
  115. {
  116.     return -1;
  117. }
  118. //---------------------------------------------------------------------------//
  119. const WCHAR_T* CIntBankDataFetcher::GetPropName(long lPropNum, long lPropAlias)
  120. {
  121.     return 0;
  122. }
  123. //---------------------------------------------------------------------------//
  124. bool CIntBankDataFetcher::GetPropVal(const long lPropNum, tVariant* pvarPropVal)
  125. {
  126.     return false;
  127. }
  128. //---------------------------------------------------------------------------//
  129. bool CIntBankDataFetcher::SetPropVal(const long lPropNum, tVariant* varPropVal)
  130. {
  131.     return false;
  132. }
  133. //---------------------------------------------------------------------------//
  134. bool CIntBankDataFetcher::IsPropReadable(const long lPropNum)
  135. {
  136.     return false;
  137. }
  138. //---------------------------------------------------------------------------//
  139. bool CIntBankDataFetcher::IsPropWritable(const long lPropNum)
  140. {
  141.     return false;
  142. }
  143. //---------------------------------------------------------------------------//
  144. long CIntBankDataFetcher::GetNMethods()
  145. {
  146.     return eMethLast;
  147. }
  148. //---------------------------------------------------------------------------//
  149. long CIntBankDataFetcher::FindMethod(const WCHAR_T* wsMethodName)
  150. {
  151.     long plMethodNum = -1;
  152.     wchar_t* name = 0;
  153.  
  154.     ::convFromShortWchar(&name, wsMethodName);
  155.  
  156.     plMethodNum = findName(g_MethodNames, name, eMethLast);
  157.  
  158.     if (plMethodNum == -1)
  159.         plMethodNum = findName(g_MethodNamesRu, name, eMethLast);
  160.  
  161.     delete[] name;
  162.  
  163.     return plMethodNum;
  164. }
  165. //---------------------------------------------------------------------------//
  166. const WCHAR_T* CIntBankDataFetcher::GetMethodName(const long lMethodNum, const long lMethodAlias)
  167. {
  168.     if (lMethodNum >= eMethLast)
  169.         return NULL;
  170.  
  171.     wchar_t *wsCurrentName = NULL;
  172.     WCHAR_T *wsMethodName = NULL;
  173.     int iActualSize = 0;
  174.  
  175.     switch (lMethodAlias)
  176.     {
  177.     case 0:
  178.         wsCurrentName = (wchar_t*)g_MethodNames[lMethodNum];
  179.         break;
  180.     case 1:
  181.         wsCurrentName = (wchar_t*)g_MethodNamesRu[lMethodNum];
  182.         break;
  183.     default:
  184.         return 0;
  185.     }
  186.  
  187.     iActualSize = wcslen(wsCurrentName) + 1;
  188.  
  189.     if (m_iMemory && wsCurrentName)
  190.     {
  191.         if (m_iMemory->AllocMemory((void**)&wsMethodName, iActualSize * sizeof(WCHAR_T)))
  192.             ::convToShortWchar(&wsMethodName, wsCurrentName, iActualSize);
  193.     }
  194.  
  195.     return wsMethodName;
  196. }
  197. //---------------------------------------------------------------------------//
  198. long CIntBankDataFetcher::GetNParams(const long lMethodNum)
  199. {
  200.     switch (lMethodNum)
  201.     {
  202.     case eFetchBankData:
  203.         return 0;
  204.     default:
  205.         return 0;
  206.     }
  207.  
  208.     return 0;
  209. }
  210. //---------------------------------------------------------------------------//
  211. bool CIntBankDataFetcher::GetParamDefValue(const long lMethodNum, const long lParamNum,
  212.     tVariant *pvarParamDefValue)
  213. {
  214.     TV_VT(pvarParamDefValue) = VTYPE_EMPTY;
  215.  
  216.     switch (lMethodNum)
  217.     {
  218.     case eFetchBankData:
  219.         break;
  220.     default:
  221.         return false;
  222.     }
  223.     return false;
  224. }
  225. //---------------------------------------------------------------------------//
  226. bool CIntBankDataFetcher::HasRetVal(const long lMethodNum)
  227. {
  228.     switch (lMethodNum)
  229.     {
  230.     case eFetchBankData:
  231.         return true;
  232.     default:
  233.         return false;
  234.     }
  235.     return false;
  236. }
  237. //---------------------------------------------------------------------------//
  238. bool CIntBankDataFetcher::CallAsProc(const long lMethodNum, tVariant* paParams,
  239.     const long lSizeArray)
  240. {
  241.     switch (lMethodNum)
  242.     {
  243.     case eFetchBankData:
  244.         break;
  245.     default:
  246.         return false;
  247.     }
  248.  
  249.     return false;
  250. }
  251. //---------------------------------------------------------------------------//
  252. bool CIntBankDataFetcher::CallAsFunc(const long lMethodNum, tVariant* pvarRetValue,
  253.     tVariant* paParams, const long lSizeArray)
  254. {
  255.  
  256.     switch (lMethodNum)
  257.     {
  258.     case eFetchBankData:
  259.     {
  260.         //Тестовая строка
  261.         //wchar_t *testString = L"TestString";
  262.  
  263.         wchar_t *xmlPathString = getJVMBankFetcherStringResult();
  264.        
  265.         int iActualSize = ::wcslen(xmlPathString) + 1;
  266.  
  267.         TV_VT(pvarRetValue) = VTYPE_PWSTR;
  268.         pvarRetValue->pwstrVal = xmlPathString;
  269.         pvarRetValue->strLen = ::wcslen(pvarRetValue->pwstrVal);
  270.  
  271.         if (m_iMemory)
  272.         {
  273.             //m_iMemory->AllocMemory((void**)&pvarRetValue->pwstrVal, iActualSize * sizeof(WCHAR_T));
  274.             if (m_iMemory->AllocMemory((void**)&pvarRetValue->pwstrVal, iActualSize * sizeof(WCHAR_T)))
  275.                 ::convToShortWchar(&pvarRetValue->pwstrVal, xmlPathString, iActualSize);
  276.         }
  277.  
  278.         return true;
  279.     }
  280.     break;
  281.     }
  282.  
  283.     return true;
  284.  
  285. }
  286. //---------------------------------------------------------------------------//
  287. void CIntBankDataFetcher::SetLocale(const WCHAR_T* loc)
  288. {
  289.     _wsetlocale(LC_ALL, loc);
  290. }
  291. /////////////////////////////////////////////////////////////////////////////
  292. // LocaleBase
  293. //---------------------------------------------------------------------------//
  294. bool CIntBankDataFetcher::setMemManager(void* mem)
  295. {
  296.     m_iMemory = (IMemoryManager*)mem;
  297.     return m_iMemory != 0;
  298. }
  299. //---------------------------------------------------------------------------//
  300. void CIntBankDataFetcher::addError(uint32_t wcode, const wchar_t* source,
  301.     const wchar_t* descriptor, long code)
  302. {
  303.     if (m_iConnect)
  304.     {
  305.         WCHAR_T *err = 0;
  306.         WCHAR_T *descr = 0;
  307.  
  308.         ::convToShortWchar(&err, source);
  309.         ::convToShortWchar(&descr, descriptor);
  310.  
  311.         m_iConnect->AddError(wcode, err, descr, code);
  312.         delete[] err;
  313.         delete[] descr;
  314.     }
  315. }
  316. //---------------------------------------------------------------------------//
  317. long CIntBankDataFetcher::findName(const wchar_t* names[], const wchar_t* name,
  318.     const uint32_t size) const
  319. {
  320.     long ret = -1;
  321.     for (uint32_t i = 0; i < size; i++)
  322.     {
  323.         if (!wcscmp(names[i], name))
  324.         {
  325.             ret = i;
  326.             break;
  327.         }
  328.     }
  329.     return ret;
  330. }
  331. //---------------------------------------------------------------------------//
  332. uint32_t convToShortWchar(WCHAR_T** Dest, const wchar_t* Source, uint32_t len)
  333. {
  334.     if (!len)
  335.         len = ::wcslen(Source) + 1;
  336.     if (!*Dest)
  337.         *Dest = new WCHAR_T[len];
  338.  
  339.     WCHAR_T* tmpShort = *Dest;
  340.     wchar_t* tmpWChar = (wchar_t*)Source;
  341.     uint32_t res = 0;
  342.  
  343.     ::memset(*Dest, 0, len * sizeof(WCHAR_T));
  344.  
  345.     for (; len; --len, ++res, ++tmpWChar, ++tmpShort)
  346.     {
  347.         *tmpShort = (WCHAR_T)*tmpWChar;
  348.     }
  349.  
  350.     return res;
  351. }
  352. //---------------------------------------------------------------------------//
  353. uint32_t convFromShortWchar(wchar_t** Dest, const WCHAR_T* Source, uint32_t len)
  354. {
  355.     if (!len)
  356.         len = getLenShortWcharStr(Source) + 1;
  357.     if (!*Dest)
  358.         *Dest = new wchar_t[len];
  359.  
  360.     wchar_t* tmpWChar = *Dest;
  361.     WCHAR_T* tmpShort = (WCHAR_T*)Source;
  362.     uint32_t res = 0;
  363.  
  364.     ::memset(*Dest, 0, len * sizeof(wchar_t));
  365.  
  366.     for (; len; --len, ++res, ++tmpWChar, ++tmpShort)
  367.     {
  368.         *tmpWChar = (wchar_t)* tmpShort;
  369.     }
  370.  
  371.     return res;
  372.  
  373. }
  374. //---------------------------------------------------------------------------//
  375. uint32_t getLenShortWcharStr(const WCHAR_T* Source)
  376. {
  377.     uint32_t res = 0;
  378.     WCHAR_T *tmpShort = (WCHAR_T*)Source;
  379.  
  380.     while (*tmpShort++)
  381.         ++res;
  382.  
  383.     return res;
  384. }
  385. //---------------------------------------------------------------------------//
  386. WcharWrapper::WcharWrapper(const wchar_t* str) : m_str_wchar(NULL)
  387. {
  388.     if (str)
  389.     {
  390.         int len = wcslen(str);
  391.         m_str_wchar = new wchar_t[len + 1];
  392.         memset(m_str_wchar, 0, sizeof(wchar_t) * (len + 1));
  393.         memcpy(m_str_wchar, str, sizeof(wchar_t) * len);
  394.     }
  395. }
  396. //---------------------------------------------------------------------------//
  397. WcharWrapper::~WcharWrapper()
  398. {
  399.     if (m_str_wchar)
  400.     {
  401.         delete[] m_str_wchar;
  402.         m_str_wchar = NULL;
  403.     }
  404. }
  405.  
  406. wchar_t* getJVMBankFetcherStringResult()
  407. {
  408.     wchar_t* xmlStr;
  409.  
  410.     JavaVMOption options[2];
  411.     JavaVMInitArgs vm_args;
  412.     JavaVM *jvm;
  413.     JNIEnv *env;
  414.     long status;
  415.  
  416.     jclass class_BankDataFetcher;
  417.     jclass class_String;
  418.     jobjectArray args;
  419.     jmethodID id_getData;
  420.  
  421. #ifdef _WINDOWS
  422.     HINSTANCE hjvmlib;
  423.     CreateJavaVM_t createJavaVM;
  424. #endif
  425.  
  426.     char* scriptcat;
  427.     scriptcat = getenv("SCRIPT_CAT");
  428.  
  429.     const char jarlib[] = "\\AccountDataFetcher\\BankDataFetcher-1.0-jar-with-dependencies.jar";
  430.  
  431.     char *fpath = new char[strlen(scriptcat) + strlen(jarlib) + 1];
  432.     fpath[0] = '\0';
  433.  
  434.     strcat(fpath, scriptcat);
  435.     strcat(fpath, jarlib);
  436.  
  437.     const char *fOptionString = "-Djava.class.path=";
  438.  
  439.     char fopt[1200];
  440.     strcpy(fopt, fOptionString);
  441.     strcat(fopt, fpath);
  442.  
  443.     size_t size = strlen(fopt);
  444.     size++;
  445.     fopt[size] = '\0';
  446.  
  447.     options[0].optionString = fopt;
  448.     options[1].optionString = "-Xrs";
  449.  
  450.     memset(&vm_args, 0, sizeof(vm_args));
  451.     vm_args.version = JNI_VERSION_1_8;
  452.     vm_args.nOptions = 2;
  453.     vm_args.options = options;
  454.     vm_args.ignoreUnrecognized = JNI_FALSE;
  455.  
  456. #ifdef _WINDOWS
  457.     hjvmlib = loadJVMLibrary();
  458.     createJavaVM = (CreateJavaVM_t)GetProcAddress(hjvmlib, "JNI_CreateJavaVM");
  459.  
  460.     __try {
  461.         status = (*createJavaVM)(&jvm, (void **)&env, &vm_args);
  462.     }
  463.     __except (GetExceptionCode() == EXCEPTION_BREAKPOINT ?
  464.         EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
  465.     {
  466.  
  467.     }
  468. #else
  469.     __try {
  470.         status = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);
  471.     }
  472.     __except (GetExceptionCode() == EXCEPTION_BREAKPOINT ?
  473.         EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
  474.     {
  475.  
  476.     }
  477. #endif
  478.  
  479.     if (status == JNI_ERR)
  480.     {
  481.         fprintf(stderr, "Error creating VM\n");
  482.         return xmlStr;
  483.     }
  484.  
  485.     class_BankDataFetcher = env->FindClass("app/***/bankdatafetcher/BankDataFetcher");
  486.     id_getData = env->GetStaticMethodID(class_BankDataFetcher, "getData", "([Ljava/lang/String;)Ljava/lang/String;");
  487.  
  488.     class_String = env->FindClass("java/lang/String");
  489.     args = env->NewObjectArray(0, class_String, NULL);
  490.  
  491.     jobject obj_ret = env->CallStaticObjectMethod(class_BankDataFetcher, id_getData, args);
  492.     //env->CallStaticVoidMethod(class_BankDataFetcher, id_getData, args);
  493.     jstring str_ret = (jstring)obj_ret;
  494.  
  495.     LPCSTR nativeString = env->GetStringUTFChars(str_ret, 0);
  496.    
  497.     int lenA = lstrlenA(nativeString);
  498.     int lenW;
  499.     BSTR unicodestr;
  500.  
  501.     lenW = ::MultiByteToWideChar(CP_ACP, 0, nativeString, lenA, 0, 0);
  502.     if (lenW > 0)
  503.     {
  504.         // Check whether conversion was successful
  505.         unicodestr = ::SysAllocStringLen(0, lenW);
  506.         ::MultiByteToWideChar(CP_ACP, 0, nativeString, lenA, unicodestr, lenW);
  507.     }
  508.     else
  509.     {
  510.         // handle the error
  511.     }
  512.  
  513.     jvm->DestroyJavaVM();
  514.  
  515.     return unicodestr;
  516. }
  517.  
  518. #ifdef _WINDOWS
  519.  
  520. static int GetStringFromRegistry(HKEY key, const wchar_t *name, wchar_t *buf, jint bufsize)
  521. {
  522.     DWORD type, size;
  523.  
  524.     int returnVal;
  525.  
  526.     return RegQueryValueEx(key, name, 0, &type, 0, &size) == 0
  527.         && type == REG_SZ
  528.         && size < (unsigned int)bufsize
  529.         && RegQueryValueEx(key, name, 0, 0, (LPBYTE)buf, &size) == 0;
  530.  
  531. }
  532.  
  533. static void GetPublicJREHome(wchar_t *buf, jint bufsize)
  534. {
  535.     HKEY key, subkey;
  536.     wchar_t version[MAX_PATH];
  537.  
  538.     /* Find the current version of the JRE */
  539.     wchar_t *JRE_KEY = L"Software\\JavaSoft\\Java Runtime Environment";
  540.  
  541.     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_READ, &key) != 0)
  542.     {
  543.         fprintf(stderr, "Error opening registry key '%s'\n", JRE_KEY);
  544.         exit(1);
  545.     }
  546.  
  547.     if (!GetStringFromRegistry(key, L"CurrentVersion", version, sizeof(version)))
  548.     {
  549.         fprintf(stderr, "Failed reading value of registry key:\n\t%s\\CurrentVersion\n", JRE_KEY);
  550.         RegCloseKey(key);
  551.         exit(1);
  552.     }
  553.  
  554.     /* Find directory where the current version is installed. */
  555.     if (RegOpenKeyEx(key, version, 0, KEY_READ, &subkey) != 0)
  556.     {
  557.         fprintf(stderr, "Error opening registry key '%s\\%s'\n", JRE_KEY, version);
  558.         RegCloseKey(key);
  559.         exit(1);
  560.     }
  561.  
  562.     if (!GetStringFromRegistry(subkey, L"JavaHome", buf, bufsize))
  563.     {
  564.         fprintf(stderr, "Failed reading value of registry key:\n\t%s\\%s\\JavaHome\n",
  565.             JRE_KEY, version);
  566.         RegCloseKey(key);
  567.         RegCloseKey(subkey);
  568.         exit(1);
  569.     }
  570.  
  571.     RegCloseKey(key);
  572.     RegCloseKey(subkey);
  573. }
  574.  
  575. static HINSTANCE loadJVMLibrary(void)
  576. {
  577.     HINSTANCE h1, h2;
  578.  
  579.     int len;
  580.  
  581.     wchar_t msvcdll[MAX_PATH];
  582.     wchar_t javadll[MAX_PATH];
  583.     GetPublicJREHome(msvcdll, MAX_PATH);
  584.     wcscpy_s(javadll, msvcdll);
  585.     wcsncat_s(msvcdll, L"\\bin\\msvcr120.dll", MAX_PATH - wcslen(msvcdll));
  586.     msvcdll[MAX_PATH - 1] = '\0';
  587.     wcsncat_s(javadll, L"\\bin\\server\\jvm.dll", MAX_PATH - wcslen(javadll));
  588.     javadll[MAX_PATH - 1] = '\0';
  589.  
  590.     h1 = LoadLibrary(msvcdll);
  591.     if (h1 == NULL)
  592.     {
  593.         fprintf(stderr, "Can't load library msvcr120.dll\n");
  594.         exit(1);
  595.     }
  596.  
  597.     h2 = LoadLibrary(javadll);
  598.     if (h2 == NULL)
  599.     {
  600.         fprintf(stderr, "Can't load library jvm.dll\n");
  601.         exit(1);
  602.     }
  603.  
  604.     return h2;
  605.  
  606. }
  607.  
  608. #endif
Advertisement
Add Comment
Please, Sign In to add comment