一、javascript调用c++,方法有两种
方案1:
1.html编写
<html>
<head>
</head>
<body>
<h1>
TEST
</h1>
<input type='button'name="xx3" value=调用c++函数 οnclick="window.navigate('app:command&arg1=1&arg2=2')">
</body>
<script language="javascript">
function TestFunc()
{
alert("TestFunc");
}
</script>
</html>
2.C++的CHtmlView类重写OnBeforeNavigate2函数
void CAddGoogleMap_CHtmlView::OnBeforeNavigate2(LPCTSTR lpszURL, DWORD nFlags, LPCTSTR lpszTargetFrameName, CByteArray& baPostedData, LPCTSTR lpszHeaders, BOOL* pbCancel)
{
// TODO: Add your specialized code here and/or call the base class
CString strUrl = lpszURL;
if(strUrl.Left(4) == _T("app:"))
{
// cancel the common url navigate and call your c++ code here
*pbCancel = TRUE;
MessageBox("调用了C++函数", "来自对话框消息");
// call other c++ function here or parse the argument in the strUrl
}
CHtmlView::OnBeforeNavigate2(lpszURL, nFlags, lpszTargetFrameName, baPostedData, lpszHeaders, pbCancel);
}
方案2:
1.编写html
<html>
<head>
</head>
<body>
<script language="javascript">
function CallCpp()
{
alert('start to call cpp here');
window.external.JavaScriptCallCpp('This is a test for call C++ in JavaScript');
}
</script>