标签:init obj system directory clu 包名 wait jar包 art
一个简单需求:用C++调用Jar包。
实现基本思路:调用CreateProcess API来启动cmd.exe执行jar包。
调用类CJarAppCall。
JarAppCall.h
#pragma once class CJarAppCall { public: CJarAppCall(void); ~CJarAppCall(void); /* @brief 运行Jar包调用,等待运行完毕返回 @param[in] strJarDir jar包文件夹 @param[in] strJarName jar包名称 @return 运行是否成功 */ bool Run(const CString& strJarDir, const CString& strJarName); private: bool CallApp(const CString& strJarName); };
#include "StdAfx.h" #include "JarAppCall.h" CJarAppCall::CJarAppCall(void) { } CJarAppCall::~CJarAppCall(void) { } bool CJarAppCall::Run(const CString& strJarDir, const CString& strJarName) { //缓存当前文件夹 WCHAR sOldDir[MAX_PATH] = {0}; GetCurrentDirectory(MAX_PATH, sOldDir); //设置当前文件夹为jar包文件夹 SetCurrentDirectory(strJarDir); bool bCalRest = CallApp(strJarName); //还原当前文件夹 SetCurrentDirectory(sOldDir); return bCalRest; } bool CJarAppCall::CallApp(const CString& strJarName) { WCHAR sSysDir[MAX_PATH] = {0}; GetSystemDirectory(sSysDir, MAX_PATH); CString strFullPath = sSysDir; strFullPath += _T("\\cmd.exe"); CString strCmdLine = _T(" /C "); strCmdLine += _T("java -jar "); strCmdLine += strJarName; STARTUPINFO si = {sizeof(si)}; PROCESS_INFORMATION pi; BOOL bRunProgram = CreateProcess(strFullPath.GetBuffer(), strCmdLine.GetBuffer(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); strFullPath.ReleaseBuffer(); strCmdLine.ReleaseBuffer(); if (!bRunProgram) { return false; } // 等待程序结束 WaitForSingleObject(pi.hProcess,INFINITE); CloseHandle(pi.hProcess); return true; }
{ CString strJarDir = _T("F:\\JarRunTest"); CString strJarName = _T("run.jar"); CJarAppCall jarCall; if (!jarCall.Run(strJarDir, strJarName)) { //启动失败 AfxMessageBox(_T("启动失败")); return; } AfxMessageBox(_T("调用完毕")); }
标签:init obj system directory clu 包名 wait jar包 art
原文地址:http://www.cnblogs.com/gavanwanggw/p/6715686.html