JavaScript调用C++
方案一:
// html & JavaScript
...
<button value="Click me" οnclick="window.navigate('app:command&arg1=1&arg2=2')" />
...
// C++: 响应 的消息函数OnBeforeNavigat2
virtual void OnBeforeNavigate2( LPCTSTR lpszURL, DWORD nFlags, LPCTSTR lpszTargetFrameName,
CByteArray& baPostedData, LPCTSTR lpszHeaders, BOOL* pbCancel );
// C++ code goes here
CXXXDlg::OnBeforeNavigate2( LPCTSTR lpszURL, DWORD nFlags, LPCTSTR lpszTargetFrameName,
CByteArray& baPostedData, LPCTSTR lpszHeaders, BOOL* pbCancel )
{
CString strUrl = lpszURL;
if(strUrl.Left(4) == _T("app:"))
{
// cancel the common url navigate and call your c++ code here
*pbCancel = TURE;
// call other c++ function here or parse the argument in the strUrl
}
// go common url navigate here
}
方案二:
// html & javaScript
...
function CallCpp()
{
alert('start to call cpp here');
window.external.JavaScriptCallCpp('This is a test for call C++ in JavaScript');
}
<button οnclick="CallCpp()" >JavaScript访问C++代码</button>
...
// C++ code goes here
CString javaScriptName = _T("JavaScriptCallCpp");
#define DISPID_CallCppFromJs 1
// 实现IDispatch 接口
Code
// .h
class CImpIDispatch : public IDispatch
{
protected:
ULONG m_cRef;
public:
CImpIDispatch(void);
~CImpIDispatch(void);
STDMETHODIMP QueryInterface(REFIID, void **);
STDMETHODIMP_(ULONG) AddRef(void);
STDMETHODIMP_(ULONG) Release(void);
//IDispatch
STDMETHODIMP GetTypeInfoCount(UINT* pctinfo);
STDMETHODIMP GetTypeInfo( UINT iTInfo,
LCID lcid,
ITypeInfo** ppTInfo);
STDMETHODIMP GetIDsOfNames(
REFIID riid,
LPOLESTR *rgszNames,
UINT cNames,
LCID lcid,
DISPID *rgDispId);
STDMETHODIMP Invoke(
DISPID dispIdMember,
REFIID riid,
LCID lcid,
WORD wFlags,
DISPPARAMS *pDispParams,
VARIANT *pVarResult,
EXCEPINFO *pExcepInfo,
UINT *puArgErr);
};
// .cpp
STDMETHODIMP CImpIDispatch::QueryInterface( REFIID riid, void **ppv )
{
*ppv = NULL;
if ( IID_IDispatch == riid )
{
*ppv = this;
}
if ( NULL != *ppv )
{
((LPUNKNOWN)*ppv)->AddRef();
return NOERROR;
}
return E_NOINTERFACE;
}
STDMETHODIMP_(ULONG) CImpIDispatch::AddRef(void)
{
return ++m_cRef;
}
STDMETHODIMP_(ULONG) CImpIDispatch::Release(void)
{
return --m_cRef;
}
//IDispatch
STDMETHODIMP CImpIDispatch::GetTypeInfoCount(UINT* )
{
return E_NOTIMPL;
}
STDMETHODIMP CImpIDispatch::GetTypeInfo(
UINT ,
LCID ,
ITypeInfo** )
{
return E_NOTIMPL;
}
STDMETHODIMP CImpIDispatch::GetIDsOfNames(
REFIID riid,
OLECHAR** rgszNames,
UINT cNames,
LCID lcid,
DISPID* rgDispId)
{
HRESULT hr;
UINT i;
// Assume some degree of success
hr = NOERROR;
for ( i=0; i < cNames; i++) {
CString cszName = rgszNames[i];
if (cszName == javaScriptName)
{
rgDispId[i] = DISPID_CallCppFromJs;
}
else {
// One or more are unknown so set the return code accordingly
hr = ResultFromScode(DISP_E_UNKNOWNNAME);
rgDispId[i] = DISPID_UNKNOWN;
}
}
return hr;
}
STDMETHODIMP CImpIDispatch::Invoke(
DISPID dispIdMember,
REFIID ,
LCID ,
WORD wFlags,
DISPPARAMS* pDispParams,
VARIANT* pVarResult,
EXCEPINFO* ,
UINT* puArgErr)
{
CXXXDlg* pDlg = (CCppCallJsDlg*) AfxGetMainWnd();
if (dispIdMember == DISPID_CallCppFromJs)
{
if (wFlags & DISPATCH_PROPERTYGET)
{
if (pVarResult != NULL)
{
VariantInit(pVarResult);
V_VT(pVarResult)=VT_BOOL;
V_BOOL(pVarResult)=true;
}
}
if (wFlags & DISPATCH_METHOD)
{
CString cszArg1= pDispParams->rgvarg[0].bstrVal;
pDlg->CallByScript(cszArg1);
}
}
return S_OK;
}
// 改写COleControlSit
Code
// .h
class CCustomControlSite:public COleControlSite
{
public:
CCustomControlSite(COleControlContainer *pCnt):COleControlSite(pCnt){}
BEGIN_INTERFACE_PART(DocHostShowUI, IDocHostShowUI)
INIT_INTERFACE_PART(CDocHostSite, DocHostShowUI)
STDMETHOD(ShowHelp)(
HWND hwnd,
LPOLESTR pszHelpFile,
UINT uCommand,
DWORD dwData,
POINT ptMouse,
IDispatch __RPC_FAR *pDispatchObjectHit);
STDMETHOD(ShowMessage)(
HWND hwnd,
LPOLESTR lpstrText,
LPOLESTR lpstrCaption,
DWORD dwType,
LPOLESTR lpstrHelpFile,
DWORD dwHelpContext,
LRESULT __RPC_FAR *plResult);
END_INTERFACE_PART(DocHostShowUI)
protected:
DECLARE_INTERFACE_MAP();
BEGIN_INTERFACE_PART(DocHostUIHandler, IDocHostUIHandler)
STDMETHOD(ShowContextMenu)( DWORD dwID,
POINT __RPC_FAR *ppt,
IUnknown __RPC_FAR *pcmdtReserved,
IDispatch __RPC_FAR *pdispReserved);
STDMETHOD(GetHostInfo)(
DOCHOSTUIINFO __RPC_FAR *pInfo);
STDMETHOD(ShowUI)(
DWORD dwID,
IOleInPlaceActiveObject __RPC_FAR *pActiveObject,
IOleCommandTarget __RPC_FAR *pCommandTarget,
IOleInPlaceFrame __RPC_FAR *pFrame,
IOleInPlaceUIWindow __RPC_FAR *pDoc);
STDMETHOD(HideUI)(void);
STDMETHOD(UpdateUI)(void);
STDMETHOD(EnableModeless)( BOOL fEnable);
STDMETHOD(OnDocWindowActivate)( BOOL fEnable);
STDMETHOD(OnFrameWindowActivate)( BOOL fEnable);
STDMETHOD(ResizeBorder)(
LPCRECT prcBorder,
IOleInPlaceUIWindow __RPC_FAR *pUIWindow,
BOOL fRameWindow);
STDMETHOD(TranslateAccelerator)(
LPMSG lpMsg,
const GUID __RPC_FAR *pguidCmdGroup,
DWORD nCmdID);
STDMETHOD(GetOptionKeyPath)(
LPOLESTR __RPC_FAR *pchKey,
DWORD dw);
STDMETHOD(GetDropTarget)(
IDropTarget __RPC_FAR *pDropTarget,
IDropTarget __RPC_FAR *__RPC_FAR *ppDropTarget);
STDMETHOD(GetExternal)(
IDispatch __RPC_FAR *__RPC_FAR *ppDispatch);
STDMETHOD(TranslateUrl)(
DWORD dwTranslate,
OLECHAR __RPC_FAR *pchURLIn,
OLECHAR __RPC_FAR *__RPC_FAR *ppchURLOut);
STDMETHOD(FilterDataObject)(
IDataObject __RPC_FAR *pDO,
IDataObject __RPC_FAR *__RPC_FAR *ppDORet);
END_INTERFACE_PART(DocHostUIHandler)
};
class CCustomOccManager :public COccManager
{
public:
CCustomOccManager(){}
COleControlSite* CreateSite(COleControlContainer* pCtrlCont)
{
CCustomControlSite *pSite = new CCustomControlSite(pCtrlCont);
return pSite;
}
};
// .cpp
BEGIN_INTERFACE_MAP(CCustomControlSite, COleControlSite)
INTERFACE_PART(CCustomControlSite, IID_IDocHostShowUI, DocHostShowUI)
INTERFACE_PART(CCustomControlSite, IID_IDocHostUIHandler, DocHostUIHandler)
END_INTERFACE_MAP()
ULONG CCustomControlSite::XDocHostShowUI::AddRef()
{
METHOD_PROLOGUE(CCustomControlSite, DocHostShowUI);
return pThis->ExternalAddRef();
}
ULONG CCustomControlSite::XDocHostShowUI::Release()
{
METHOD_PROLOGUE(CCustomControlSite, DocHostShowUI);
return pThis->ExternalRelease();
}
HRESULT CCustomControlSite::XDocHostShowUI::QueryInterface(REFIID riid, void ** ppvObj)
{
METHOD_PROLOGUE(CCustomControlSite, DocHostShowUI);
return pThis->ExternalQueryInterface( &riid, ppvObj );
}
HRESULT CCustomControlSite::XDocHostShowUI::ShowHelp(HWND hwnd,
LPOLESTR pszHelpFile,
UINT nCommand,
DWORD dwData,
POINT ptMouse,
IDispatch * pDispatchObjectHit)
{
METHOD_PROLOGUE(CCustomControlSite, DocHostShowUI);
return S_OK;
}
HRESULT CCustomControlSite::XDocHostShowUI::ShowMessage(HWND hwnd,
LPOLESTR lpstrText,
LPOLESTR lpstrCaption,
DWORD dwType,
LPOLESTR lpstrHelpFile,
DWORD dwHelpContext,
LRESULT * plResult)
{
METHOD_PROLOGUE(CCustomControlSite, DocHostShowUI);
MessageBox(hwnd, (CString)lpstrText, _T("Cpp & JavaScript"), MB_ICONWARNING);
return S_OK;
}
ULONG FAR EXPORT CCustomControlSite::XDocHostUIHandler::AddRef()
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
return pThis->ExternalAddRef();
}
ULONG FAR EXPORT CCustomControlSite::XDocHostUIHandler::Release()
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
return pThis->ExternalRelease();
}
HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::QueryInterface(REFIID riid, void **ppvObj)
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
HRESULT hr = (HRESULT)pThis->ExternalQueryInterface(&riid, ppvObj);
return hr;
}
// * CImpIDocHostUIHandler::GetHostInfo
// *
// * Purpose: Called at initialization
// *
HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::GetHostInfo( DOCHOSTUIINFO* pInfo )
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
pInfo->dwFlags = DOCHOSTUIFLAG_NO3DBORDER;
pInfo->dwDoubleClick = DOCHOSTUIDBLCLK_DEFAULT;
return S_OK;
}
// * CImpIDocHostUIHandler::ShowUI
// *
// * Purpose: Called when MSHTML.DLL shows its UI
// *
HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::ShowUI(
DWORD dwID,
IOleInPlaceActiveObject * ,
IOleCommandTarget * pCommandTarget,
IOleInPlaceFrame * ,
IOleInPlaceUIWindow * )
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
// We've already got our own UI in place so just return S_OK
return S_OK;
}
// * CImpIDocHostUIHandler::HideUI
// *
// * Purpose: Called when MSHTML.DLL hides its UI
// *
HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::HideUI(void)
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
return S_OK;
}
// * CImpIDocHostUIHandler::UpdateUI
// *
// * Purpose: Called when MSHTML.DLL updates its UI
// *
HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::UpdateUI(void)
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
// MFC is pretty good about updating it's UI in it's Idle loop so I don't do anything here
return S_OK;
}
// * CImpIDocHostUIHandler::EnableModeless
// *
// * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::EnableModeless
// *
HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::EnableModeless(BOOL )
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
return E_NOTIMPL;
}
// * CImpIDocHostUIHandler::OnDocWindowActivate
// *
// * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::OnDocWindowActivate
// *
HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::OnDocWindowActivate(BOOL )
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
return E_NOTIMPL;
}
// * CImpIDocHostUIHandler::OnFrameWindowActivate
// *
// * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::OnFrameWindowActivate
// *
HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::OnFrameWindowActivate(BOOL )
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
return E_NOTIMPL;
}
// * CImpIDocHostUIHandler::ResizeBorder
// *
// * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::ResizeBorder
// *
HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::ResizeBorder(
LPCRECT ,
IOleInPlaceUIWindow* ,
BOOL )
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
return E_NOTIMPL;
}
// * CImpIDocHostUIHandler::ShowContextMenu
// *
// * Purpose: Called when MSHTML.DLL would normally display its context menu
// *
HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::ShowContextMenu(
DWORD ,
POINT* pptPosition,
IUnknown* ,
IDispatch* )
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
return E_NOTIMPL;
//CMenu menu;
//menu.LoadMenu(IDR_CUSTOM_POPUP);
//CMenu* pSubMenu = menu.GetSubMenu(0);
Because we passed in theApp.m_pMainWnd all of our
WM_COMMAND handlers for the menu items must be handled
in CCustomBrowserApp. If you want this to be your dialog
you will have to grab a pointer to your dialog class and
pass the hWnd of it into the last parameter in this call
//pSubMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, pptPosition->x, pptPosition->y, theApp.m_pMainWnd);
return S_OK; // We've shown our own context menu. MSHTML.DLL will no longer try to show its own.
}
// * CImpIDocHostUIHandler::TranslateAccelerator
// *
// * Purpose: Called from MSHTML.DLL's TranslateAccelerator routines
// *
HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::TranslateAccelerator(LPMSG lpMsg,
const GUID __RPC_FAR *pguidCmdGroup,
DWORD nCmdID)
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
//disable F5
if(lpMsg->message == WM_KEYDOWN && GetAsyncKeyState(VK_F5) < 0)
return S_OK;
if(GetKeyState(VK_CONTROL) & 0x8000)
{
//disable ctrl + O
if(lpMsg->message == WM_KEYDOWN && GetAsyncKeyState(0x4F) < 0)
return S_OK;
//disable ctrl + p
if(lpMsg->message == WM_KEYDOWN && GetAsyncKeyState(0x50) < 0)
return S_OK;
//disable ctrl + N
if(lpMsg->message == WM_KEYDOWN && GetAsyncKeyState(0x4E) < 0)
return S_OK;
}
//disable back space
if(lpMsg->wParam == VK_BACK)
return S_OK;
return S_FALSE;
}
// * CImpIDocHostUIHandler::GetOptionKeyPath
// *
// * Purpose: Called by MSHTML.DLL to find where the host wishes to store
// * its options in the registry
// *
HRESULT FAR EXPORT CCustomControlSite::XDocHostUIHandler::GetOptionKeyPath(BSTR* pbstrKey, DWORD)
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
return E_NOTIMPL;
}
STDMETHODIMP CCustomControlSite::XDocHostUIHandler::GetDropTarget(
IDropTarget __RPC_FAR *pDropTarget,
IDropTarget __RPC_FAR *__RPC_FAR *ppDropTarget)
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
return E_NOTIMPL;
}
STDMETHODIMP CCustomControlSite::XDocHostUIHandler::GetExternal(
IDispatch __RPC_FAR *__RPC_FAR *ppDispatch)
{
// return the IDispatch we have for extending the object Model
IDispatch* pDisp = (IDispatch*)theApp.m_pDispOM;
pDisp->AddRef();
*ppDispatch = pDisp;
return S_OK;
}
STDMETHODIMP CCustomControlSite::XDocHostUIHandler::TranslateUrl(
DWORD dwTranslate,
OLECHAR __RPC_FAR *pchURLIn,
OLECHAR __RPC_FAR *__RPC_FAR *ppchURLOut)
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
return E_NOTIMPL;
}
STDMETHODIMP CCustomControlSite::XDocHostUIHandler::FilterDataObject(
IDataObject __RPC_FAR *pDO,
IDataObject __RPC_FAR *__RPC_FAR *ppDORet)
{
METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
return E_NOTIMPL;
}
// 修改App
Code
// .h
class CImpIDispatch;
class CXXXApp : public CWinApp
{
CImpIDispatch *m_pDispOM;
}
// .cpp
BOOL CXXXApp::InitInstance()
{
CWinApp::InitInstance();
CCustomOccManager *pMgr = new CCustomOccManager;
// Create an IDispatch class for extending the Dynamic HTML Object Model
m_pDispOM = new CImpIDispatch;
// Set our control containment up but using our control container
// management class instead of MFC's default
AfxEnableControlContaine r(pMgr);
}
link:http://www.cnblogs.com/dlbrant/archive/2009/02/17/1392430.html
方案一:
// html & JavaScript
...
<button value="Click me" οnclick="window.navigate('app:command&arg1=1&arg2=2')" />
...
virtual void OnBeforeNavigate2( LPCTSTR lpszURL, DWORD nFlags, LPCTSTR lpszTargetFrameName,
CByteArray& baPostedData, LPCTSTR lpszHeaders, BOOL* pbCancel );
// C++ code goes here
CXXXDlg::OnBeforeNavigate2( LPCTSTR lpszURL, DWORD nFlags, LPCTSTR lpszTargetFrameName,
{
}
方案二:
// html & javaScript
...
function CallCpp()
{
alert('start to call cpp here');
window.external.JavaScriptCallCpp('This is a test for call C++ in JavaScript');
}
<button οnclick="CallCpp()" >JavaScript访问C++代码</button>
...
// C++ code goes here
CString javaScriptName = _T("JavaScriptCallCpp");
#define DISPID_CallCppFromJs 1
// 实现IDispatch 接口
Code
// .h
class CImpIDispatch : public IDispatch
{
};
// .cpp
STDMETHODIMP CImpIDispatch::QueryInterface( REFIID riid, void **ppv )
{
}
STDMETHODIMP_(ULONG) CImpIDispatch::AddRef(void)
{
}
STDMETHODIMP_(ULONG) CImpIDispatch::Release(void)
{
}
//IDispatch
STDMETHODIMP CImpIDispatch::GetTypeInfoCount(UINT* )
{
}
STDMETHODIMP CImpIDispatch::GetTypeInfo(
{
}
STDMETHODIMP CImpIDispatch::GetIDsOfNames(
{
}
STDMETHODIMP CImpIDispatch::Invoke(
{
}
// 改写COleControlSit
Code
// .h
class CCustomControlSite:public COleControlSite
{
public:
BEGIN_INTERFACE_PART(DocHostShowUI, IDocHostShowUI)
protected:
BEGIN_INTERFACE_PART(DocHostUIHandler, IDocHostUIHandler)
END_INTERFACE_PART(DocHostUIHandler)
};
class CCustomOccManager :public COccManager
{
public:
};
// .cpp
BEGIN_INTERFACE_MAP(CCustomControlSite, COleControlSite)
END_INTERFACE_MAP()
ULONG CCustomControlSite::XDocHostShowUI::AddRef()
{
}
ULONG CCustomControlSite::XDocHostShowUI::Release()
{
}
HRESULT CCustomControlSite::XDocHostShowUI::QueryInterface(REFIID riid, void ** ppvObj)
{
}
HRESULT CCustomControlSite::XDocHostShowUI::ShowHelp(HWND hwnd,
{
}
HRESULT CCustomControlSite::XDocHostShowUI::ShowMessage(HWND hwnd,
{
}
ULONG FAR EXPORT
{
}
ULONG FAR EXPORT
{
}
HRESULT FAR EXPORT
{
}
// * CImpIDocHostUIHandler::GetHostInfo
// *
// * Purpose: Called at initialization
// *
HRESULT FAR EXPORT
{
}
// * CImpIDocHostUIHandler::ShowUI
// *
// * Purpose: Called when MSHTML.DLL shows its UI
// *
HRESULT FAR EXPORT
{
}
// * CImpIDocHostUIHandler::HideUI
// *
// * Purpose: Called when MSHTML.DLL hides its UI
// *
HRESULT FAR EXPORT
{
}
// * CImpIDocHostUIHandler::UpdateUI
// *
// * Purpose: Called when MSHTML.DLL updates its UI
// *
HRESULT FAR EXPORT
{
}
// * CImpIDocHostUIHandler::EnableModeless
// *
// * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::EnableModeless
// *
HRESULT FAR EXPORT
{
}
// * CImpIDocHostUIHandler::OnDocWindowActivate
// *
// * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::OnDocWindowActivate
// *
HRESULT FAR EXPORT
{
}
// * CImpIDocHostUIHandler::OnFrameWindowActivate
// *
// * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::OnFrameWindowActivate
// *
HRESULT FAR EXPORT
{
}
// * CImpIDocHostUIHandler::ResizeBorder
// *
// * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::ResizeBorder
// *
HRESULT FAR EXPORT
{
}
// * CImpIDocHostUIHandler::ShowContextMenu
// *
// * Purpose: Called when MSHTML.DLL would normally display its context menu
// *
HRESULT FAR EXPORT
{
}
// * CImpIDocHostUIHandler::TranslateAccelerator
// *
// * Purpose: Called from MSHTML.DLL's TranslateAccelerator routines
// *
HRESULT FAR EXPORT
{
}
// * CImpIDocHostUIHandler::GetOptionKeyPath
// *
// * Purpose: Called by MSHTML.DLL to find where the host wishes to store
// *
// *
HRESULT FAR EXPORT
{
}
STDMETHODIMP CCustomControlSite::XDocHostUIHandler::GetDropTarget(
{
}
STDMETHODIMP CCustomControlSite::XDocHostUIHandler::GetExternal(
{
}
STDMETHODIMP CCustomControlSite::XDocHostUIHandler::TranslateUrl(
{
}
STDMETHODIMP CCustomControlSite::XDocHostUIHandler::FilterDataObject(
{
}
// 修改App
Code
// .h
class CImpIDispatch;
class CXXXApp : public CWinApp
{
}
// .cpp
BOOL CXXXApp::InitInstance()
{
}
link:http://www.cnblogs.com/dlbrant/archive/2009/02/17/1392430.html