⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javatocppfacedlg.cpp

📁 C调用Java的一个例子程序
💻 CPP
字号:
// JavaToCppFaceDlg.cpp : implementation file
//

#include "stdafx.h"
#include "JavaToCppFace.h"
#include "JavaToCppFaceDlg.h"
#include <fstream>
#include "../../javaclass/JAVA_JarDumpClass.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#ifdef _DEBUG
#undef  new
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
	
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	
	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CJavaToCppFaceDlg dialog

CJavaToCppFaceDlg::CJavaToCppFaceDlg(CWnd* pParent /*=NULL*/)
: CDialog(CJavaToCppFaceDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CJavaToCppFaceDlg)
	m_Class = _T("");
	m_jar = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CJavaToCppFaceDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CJavaToCppFaceDlg)
	DDX_Control(pDX, IDC_COMBO_CLASS, m_Com_Class);
	DDX_Control(pDX, IDC_LIST_JAR, m_list_Jar);
	DDX_Control(pDX, IDC_LIST_CLASS, m_list_Class);
	DDX_Text(pDX, IDC_EDIT_CLASS, m_Class);
	DDX_Text(pDX, IDC_EDIT_JAR, m_jar);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CJavaToCppFaceDlg, CDialog)
//{{AFX_MSG_MAP(CJavaToCppFaceDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_BUTTON_BROW, OnButtonBrow)
ON_BN_CLICKED(IDC_BUTTON_BROW2, OnButtonBrow2)
ON_BN_CLICKED(IDC_BUTTON_, OnButton)
ON_WM_KEYUP()
ON_BN_CLICKED(IDC_BUTTON_CLASS, OnButtonClass)
ON_BN_CLICKED(IDC_BUTTON_JAR, OnButtonJar)
ON_BN_CLICKED(IDC_JUST, OnJust)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CJavaToCppFaceDlg message handlers


static tagToolInfo __tagToolInfo[]=
{
	IDC_BUTTON_BROW,_T("增加一个或多个jar文件!"),
		IDC_BUTTON_JAR ,_T("删除一个或多个jar文件!"),
		IDC_JUST	   ,_T("将选中的一个或多个jar文件的java类导出!"),
		IDC_BUTTON_BROW2,_T("增加java类!"),
		IDC_BUTTON_CLASS,_T("删除一个或多个jar文件!"),
		IDC_BUTTON_,_T("将选中的一个或多个类,生成对应的C++文件,生成的.h和.cpp文件he对应的jar文件在同一个目录!"),
};
BOOL CJavaToCppFaceDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	EnableToolTips(TRUE);
	m_ToolTipCtrl.Create(this);	
	m_ToolTipCtrl.Activate(TRUE);
	
	int __tooltipnum = sizeof(__tagToolInfo)/sizeof(tagToolInfo);
	
	for(int i = 0; i < __tooltipnum; ++i)
	{
		addToolTipInfo(__tagToolInfo[i].m_ID,__tagToolInfo[i].m_Info);
	}
	
	
	// TODO: Add extra initialization here
	
	
	
	
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CJavaToCppFaceDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CJavaToCppFaceDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CJavaToCppFaceDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CJavaToCppFaceDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	
	JAVA_VM * __lpJavaVM = JAVA_VM::getInstance();
	__lpJavaVM->endVM();
	CDialog::OnClose();
	CDialog::OnCancel();
}

void CJavaToCppFaceDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

void CJavaToCppFaceDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	//CDialog::OnCancel();
}

void CJavaToCppFaceDlg::OnButtonBrow() 
{
	TCHAR BASED_CODE szFilter[] = _T("Chart Files (*.jar)|*.jar|");
	CFileDialog _fd(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,szFilter);
	if (_fd.DoModal() == IDOK) {
		
		POSITION  pos = _fd.GetStartPosition();
		CString __strjar;
		do{			
			
			__strjar = _fd.GetNextPathName(pos);
			if (!__strjar.IsEmpty() ) {		
				m_list_Jar.InsertString(0,__strjar);
				
			}
		}while((pos != NULL) );
	}
	
	UpdateData(FALSE);
	
}

void CJavaToCppFaceDlg::OnButtonBrow2() 
{
	UpdateData();
	CString  __strClass;
	m_Com_Class.GetWindowText(__strClass);
	if (!__strClass.IsEmpty()) {
		m_list_Class.InsertString(0,__strClass);
		
	}
	UpdateData(FALSE);
}

void CJavaToCppFaceDlg::OnButton() 
{
	if (m_list_Class.GetCount() <= 0) {
		return;
	}
	HMODULE  _hmodule = ::LoadLibrary(_T("JavaToCpp.exe"));
	if (_hmodule) {
		::FreeLibrary(_hmodule);
		CString _cmdline = "\"";
		_cmdline += "JavaToCpp.exe\" ";
		_cmdline += getListString(m_list_Jar);
		_cmdline += getListString(m_list_Class);		
		WinExec(_cmdline.GetBuffer(0),SW_SHOW);
	}else{
		AfxMessageBox(_T("缺少 JavaToCpp.exe!"));
	}
}

CString CJavaToCppFaceDlg::getListString(CListBox & _list)
{
	CString __str;
	int __num = _list.GetCount();
	CString  __ListText;
	for(int i = 0; i < __num; ++i)
	{
		_list.GetText(i,__ListText);
		__str += __ListText;
		__str += " ";
	}
	
	return __str;
}

CString CJavaToCppFaceDlg::getCurPath()
{
	TCHAR _DIR[MAX_PATH];
	GetCurrentDirectory(MAX_PATH,_DIR);
	
	CString __path = _DIR;
	
	return __path;
}


void CJavaToCppFaceDlg::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	
	CDialog::OnKeyUp(nChar, nRepCnt, nFlags);
}

void CJavaToCppFaceDlg::OnButtonClass() 
{
	INT _pos = m_list_Class.GetCurSel();
	
	if (_pos != LB_ERR ) {
		m_list_Class.DeleteString(_pos);
		UpdateData(FALSE);
	}	
}

void CJavaToCppFaceDlg::OnButtonJar() 
{
	INT _pos = m_list_Jar.GetCurSel();
	
	if (_pos != LB_ERR ) {
		m_list_Jar.DeleteString(_pos);
		UpdateData(FALSE);
	}	
}

void CJavaToCppFaceDlg::OnJust() 
{
	
	if (m_list_Jar.GetCount() <=0) {
		return;
	}
	JNI_CPP_StartSubEnv();
	
	do {		
		
		JAVA_JarDumpClass* __lpDumpClass = new(JAVA_JarDumpClass::getSig()) JAVA_JarDumpClass();
		
		if (!__lpDumpClass) {
			SHOWMESSAGE(_T("can not create java class JAVA_JarDumpClass!"));
			break;			
		}
		
		
		__lpDumpClass->load(); //must do before used
		
		JAVA_String __jarFile ;
		JNI_Boolean  __isLoadSucc;
		
		INT __jarNum = m_list_Jar.GetCount();
		CString __str;
		for(int __i = 0 ; __i < __jarNum; ++__i)
		{
			m_list_Jar.GetText(__i,__str);
			if (!__str.IsEmpty()) {
				__jarFile = __str.GetBuffer(0);
				__isLoadSucc = __lpDumpClass->LoadJarFile_Ljava_lang_String__Z(__jarFile.getJniString());
				if (__isLoadSucc == JNI_False ) {
					SHOWMESSAGE(_T("JAVA_JarDumpClass LoadJarFile unsuccessful!"));
					return;
				}
			}
			
		}
		
		__lpDumpClass->getFirst__V();
		JNI_Int __classNum = __lpDumpClass->ClassAmount__I();
		JAVA_String __JavaString;
		CString    __ClassName;
		while (m_Com_Class.DeleteString(0) > 0) {}
		
		for(__i = 0 ; __i < __classNum; ++__i)
		{
			TCHAR __buffer[255] = {0};
			__JavaString = (JNI_String)__lpDumpClass->getOne__Ljava_lang_String_();
			__ClassName = __JavaString.getLocalString();
			TCHAR * __lp = __ClassName.GetBuffer(0);
			int i = 0;
			while (*__lp != '\0') {
				if (*__lp == '/') {
					__buffer[i] = '.';
				}else
				{
					__buffer[i] = *__lp;
				}
				
				++__lp; ++i;
			}
			__ClassName = __buffer;
			m_Com_Class.AddString(__ClassName);
		}
		if (m_Com_Class.GetCount() > 0) {
			m_Com_Class.SetCurSel(0);
		}
		
	} while(0);	
	
	JNI_CPP_EndSubEnv();
}

void CJavaToCppFaceDlg::addToolTipInfo(UINT _id, const TCHAR *_info)
{
	CWnd * __lpWnd = GetDlgItem(_id);
	if (__lpWnd) {
		m_ToolTipCtrl.AddTool(__lpWnd,_info);
	}
	
}

BOOL CJavaToCppFaceDlg::PreTranslateMessage(MSG* pMsg) 
{
	if (pMsg)
	{
		m_ToolTipCtrl.RelayEvent(pMsg);
	}	
	return CDialog::PreTranslateMessage(pMsg);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -