java程序通过JNI打开ie

xiaoxiao2022-06-16  90

最近因为工作需要,要在java应用程序中打开一个网站,方法有多种,如通过Runtime.getRuntime().exec(""),但是这种方法关不掉session。 最后选择通过JNI来打开一个IE,ie在装载网页的办法,这种方法可以关掉session,下面我把代码贴出来,以供参考 struct EnumParam { HWND hMainWnd; DWORD dwProcessID; }; // 唯一的一个 CcjniApp 对象 CCjniApp theApp; //HINSTANCE m_handle; CString getIEpath(); //查找窗口句柄 BOOL CALLBACK EnumWinProc(HWND hwnd, LPARAM lParam) { DWORD dwID; EnumParam* pep = (EnumParam*)lParam; if (!(GetWindowLong(hwnd,GWL_STYLE) & WS_VISIBLE)) { return TRUE; } GetWindowThreadProcessId(hwnd, &dwID); if (dwID == pep->dwProcessID) { pep->hMainWnd = hwnd; return 0; } return TRUE; } DWORD myProcessID = 0; HANDLE myProcessHandle; JNIEXPORT jstring JNICALL Java_coc_jni_JniEx_showIE(JNIEnv *env, jobject obj,jstring str) { EnumParam ep; STARTUPINFO si; PROCESS_INFORMATION pi; ep.hMainWnd = NULL; memset(&si, 0, sizeof(si)); si.cb = sizeof(STARTUPINFO); jboolean isCopy=FALSE; jsize len = (*env).GetStringLength(str); const jchar *commandstr = (*env).GetStringChars(str,&isCopy); const jchar *wndClassStr = NULL; CString str1 = CString(commandstr); (*env).ReleaseStringChars(str, commandstr); if(str1.Find("close")!=-1) { ::TerminateProcess(myProcessHandle,0); myProcessID = 0; myProcessHandle =NULL; return NULL; } //strcpy((LPTSTR)&str1,(LPCTSTR)commandstr); //int size = 0; //size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)commandstr, len, commandcstr,(len*2+1), NULL, NULL ); CString m_strPath = getIEpath(); CString path = m_strPath+" "+str1; DWORD dwTick; bool bCreateProcssFlag = true; if(myProcessID!=0) { dwTick = GetTickCount(); ep.dwProcessID = myProcessID; 查找窗口句柄 while(!ep.hMainWnd && GetTickCount()-dwTick<2000) { EnumWindows((WNDENUMPROC)EnumWinProc, (long)&ep); if(ep.hMainWnd==NULL) Sleep(1000); } if(ep.hMainWnd!=NULL) bCreateProcssFlag = false; } if (bCreateProcssFlag&&CreateProcess(NULL,path.GetBuffer(path.GetLength()), NULL,NULL,FALSE,0,NULL,NULL,&si,&pi)) { ep.dwProcessID = pi.dwProcessId; myProcessID = pi.dwProcessId; myProcessHandle = pi.hProcess; dwTick = GetTickCount(); while(!ep.hMainWnd && GetTickCount()-dwTick<2000) { EnumWindows((WNDENUMPROC)EnumWinProc, (long)&ep); if(ep.hMainWnd==NULL) Sleep(1000); } } HWND hParent = ep.hMainWnd; ::ShowWindow(hParent,SW_RESTORE); ::SendMessage(hParent,WM_SHOWWINDOW,0,0); ::SetForegroundWindow(hParent); return NULL; } //取得IE路径 CString getIEpath(){ HKEY hKEY;//定义有关的hKEY,在查询结束时要关闭 //打开与路径 data_Set相关的hKEY LPCTSTR data_Set="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\iexplore.exe"; CString m_strPath; //访问注册表,hKEY则保存此函数所打开的键的句柄 long ret0=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,data_Set,0,KEY_READ,&hKEY)); if(ret0!=ERROR_SUCCESS)//如果无法打开hKEY,则中止程序的执行 { AfxMessageBox("error, can't open key!"); return ""; } //查询有关的数据 LPBYTE path_Get=new BYTE[80]; DWORD type_1=REG_SZ;//定义数据类型 DWORD cbData_1=80;//定义数据长度 long ret1=::RegQueryValueEx(hKEY,"",NULL,&type_1,path_Get,&cbData_1); if(ret1!=ERROR_SUCCESS) { AfxMessageBox("error, don't query key information!"); return ""; } //显示信息 m_strPath=CString(path_Get); delete[] path_Get; //程序结束,关闭打开的hKEY ::RegCloseKey(hKEY); return m_strPath; }
转载请注明原文地址: https://www.6miu.com/read-4940861.html

最新回复(0)