3,245
社区成员
发帖
与我相关
我的任务
分享
var AMFCocxTest;
if (!AMFCocxTest)
AMFCocxTest = {};
(function() {
AMFCocxTest.ocxTest = function() {
native function ocxTest();
return ocxTest();
};
AMFCocxTest.ocxTest01 = function() {
native function ocxTest01();
return ocxTest01();
};
AMFCocxTest.ocxTest02 = function(BSTR) {
native function ocxTest02();
return ocxTest02(BSTR);
};
AMFCocxTest.ocxTest03 = function(BSTR) {
native function ocxTest03();
return ocxTest03(BSTR);
};
AMFCocxTest.ocxTest04 = function(BSTR1,BSTR2,BSTR3) {
native function ocxTest04();
return ocxTest04(BSTR1,BSTR2,BSTR3);
};
AMFCocxTest.ocxTest05 = function(BSTR,int) {
native function ocxTest05();
return ocxTest05(BSTR,int);
};
})();
var AMFCocxTest01;
if (!AMFCocxTest01)
AMFCocxTest01 = {};
(function() {
AMFCocxTest01.ocxTest02 = function(BSTR) {
native function ocxTest02();
return ocxTest02(BSTR);
};
AMFCocxTest01.Test = function(BSTR1,BSTR2) {
native function Test();
return Test(BSTR1,BSTR2);
};
})();
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Visual C++ Language Reference and related
// electronic documentation provided with Microsoft Visual C++.
// See these sources for detailed information regarding the
// Microsoft Visual C++ product.
// NOTE: This example will only work with Excel8 in Office97
// Compile with cl /GX comexcel.cpp
// TO DO: Edit the #import paths
//#pragma message ("Make sure you go to Tools.Options.Directories and add the paths to mso97.dll and vbeext1.olb. Mso97.dll will usually be in c:\\\"Program Files\"\\\"Microsoft Office\"\\Office, and vbeext1.olb will be in c:\\\"Program Files\"\\\"Common Files\"\\\"Microsoft Shared\"\\VBA")
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\Office11\\mso.dll" no_namespace rename("DocumentProperties", "DocumentPropertiesXL")
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\VBA6\\VBE6EXT.OLB" no_namespace
#import "C:\\Program Files\\Microsoft Office\\OFFICE11\\excel.exe" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces
#pragma warning (disable:4192 4146)
#include <stdio.h>
#include <tchar.h>
void dump_com_error(_com_error &e)
{
_tprintf(_T("Oops - hit an error!\n"));
_tprintf(_T("\a\tCode = %08lx\n"), e.Error());
_tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
_tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
_tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
}
// If this is placed in the scope of the smart pointers, they must be
// explicitly Release(d) before CoUninitialize() is called. If any reference
// count is non-zero, a protection fault will occur.
struct StartOle
{
StartOle() { CoInitialize(NULL); }
~StartOle() { CoUninitialize(); }
} _inst_StartOle;
void main()
{
using namespace Excel;
_ApplicationPtr pXL;
try
{
pXL.CreateInstance(L"Excel.Application");
pXL->Visible = VARIANT_TRUE;
WorkbooksPtr pBooks = pXL->Workbooks;
_WorkbookPtr pBook = pBooks->Add((long)xlWorksheet);
_WorksheetPtr pSheet = pXL->ActiveSheet;
RangePtr pRange;
pRange = pSheet->Range["A21"];
pRange->Value2 = 75L;
//pRange->NumberFormatLocal = "@";
_CommandBarsPtr pCmdbars = pXL->CommandBars;
int iCmdbars = pCmdbars->GetCount();
Sleep(1000);
pRange = pSheet->Range["20:20"];
pRange->Insert( (long)Excel::xlDown );
pRange->Merge();
Sleep(1000);
pBook->Saved = VARIANT_TRUE;
pXL->Quit();
}
catch(_com_error &e)
{
dump_com_error(e);
pXL->Quit();
}
}
[/quote]
赵老师,我试了一下,javascript不支持C++ #import rename功能。请问ocx在没有源码的情况下可以修改方法名么?如果可以,如何修改?// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Visual C++ Language Reference and related
// electronic documentation provided with Microsoft Visual C++.
// See these sources for detailed information regarding the
// Microsoft Visual C++ product.
// NOTE: This example will only work with Excel8 in Office97
// Compile with cl /GX comexcel.cpp
// TO DO: Edit the #import paths
//#pragma message ("Make sure you go to Tools.Options.Directories and add the paths to mso97.dll and vbeext1.olb. Mso97.dll will usually be in c:\\\"Program Files\"\\\"Microsoft Office\"\\Office, and vbeext1.olb will be in c:\\\"Program Files\"\\\"Common Files\"\\\"Microsoft Shared\"\\VBA")
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\Office11\\mso.dll" no_namespace rename("DocumentProperties", "DocumentPropertiesXL")
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\VBA6\\VBE6EXT.OLB" no_namespace
#import "C:\\Program Files\\Microsoft Office\\OFFICE11\\excel.exe" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces
#pragma warning (disable:4192 4146)
#include <stdio.h>
#include <tchar.h>
void dump_com_error(_com_error &e)
{
_tprintf(_T("Oops - hit an error!\n"));
_tprintf(_T("\a\tCode = %08lx\n"), e.Error());
_tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
_tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
_tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
}
// If this is placed in the scope of the smart pointers, they must be
// explicitly Release(d) before CoUninitialize() is called. If any reference
// count is non-zero, a protection fault will occur.
struct StartOle
{
StartOle() { CoInitialize(NULL); }
~StartOle() { CoUninitialize(); }
} _inst_StartOle;
void main()
{
using namespace Excel;
_ApplicationPtr pXL;
try
{
pXL.CreateInstance(L"Excel.Application");
pXL->Visible = VARIANT_TRUE;
WorkbooksPtr pBooks = pXL->Workbooks;
_WorkbookPtr pBook = pBooks->Add((long)xlWorksheet);
_WorksheetPtr pSheet = pXL->ActiveSheet;
RangePtr pRange;
pRange = pSheet->Range["A21"];
pRange->Value2 = 75L;
//pRange->NumberFormatLocal = "@";
_CommandBarsPtr pCmdbars = pXL->CommandBars;
int iCmdbars = pCmdbars->GetCount();
Sleep(1000);
pRange = pSheet->Range["20:20"];
pRange->Insert( (long)Excel::xlDown );
pRange->Merge();
Sleep(1000);
pBook->Saved = VARIANT_TRUE;
pXL->Quit();
}
catch(_com_error &e)
{
dump_com_error(e);
pXL->Quit();
}
}
std::string extensionCode =
"var AMFCocxTest;"
"if (!AMFCocxTest)"
" AMFCocxTest = {}; "
"(function() {"
" AMFCocxTest.ocxTest = function() {"
" native function ocxTest();"
" return ocxTest();"
" };"
" AMFCocxTest.ocxTest01 = function() {"
" native function ocxTest01();"
" return ocxTest01();"
" };"
" AMFCocxTest.ocxTest02 = function(BSTR) {"
" native function ocxTest02();"
" return ocxTest02(BSTR);"
" };"
" AMFCocxTest.ocxTest03 = function(BSTR) {"
" native function ocxTest03();"
" return ocxTest03(BSTR);"
" };"
" AMFCocxTest.ocxTest04 = function(BSTR1,BSTR2,BSTR3) {"
" native function ocxTest04();"
" return ocxTest04(BSTR1,BSTR2,BSTR3);"
" };"
" AMFCocxTest.ocxTest05 = function(BSTR,int) { "
" native function ocxTest05();"
" return ocxTest05(BSTR,int);"
" };"
"})();
"var AMFCocxTest01;"
"if (!AMFCocxTest01)"
" AMFCocxTest01 = {}; "
"(function() {"
" AMFCocxTest01.ocxTest02 = function(BSTR) {"
" native function ocxTest02();"
" return ocxTest02(BSTR);"
" };"
" AMFCocxTest01.Test = function(BSTR1,BSTR2) {"
" native function Test();"
" return Test(BSTR1,BSTR2);"
" };"
"})();"
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript" >
function ocxT()
{
var result = AMFCocxTest.ocxTest();
alert(result);
}
function ocxT1()
{
var result = AMFCocxTest.ocxTest01();
alert(result);
}
function ocxT2()
{
var result = AMFCocxTest.ocxTest02("A111");
alert(result);
}
function ocxT3()
{
var result = AMFCocxTest.ocxTest03("A111");
alert(result);
}
function ocxT4()
{
var result = AMFCocxTest.ocxTest04("A111", "B222", "C333");
alert(result);
}
function ocxT5()
{
var result = AMFCocxTest.ocxTest05("A111", 22);
alert(result);
}
function ocx1T()
{
var result = AMFCocxTest01.ocxTest02("A111");
alert(result);
}
function ocx1T1()
{
var result = AMFCocxTest01.Test("C111", "D111");
alert(result);
}
</script>
</head>
<body style="width:100%;height:100%;background-color:green;">
<p>这是c++与JS交互测试脚本</p>
<div >
<Text>---------------------------</Text>
<Text>以下为测试按钮</Text>
<Text>---------------------------</Text>
</div>
<div >
<button onclick="ocxT();">TEST</button>
<button onclick="ocxT1();">TEST01</button>
<button onclick="ocxT2();">TEST02</button>
<button onclick="ocxT3();">TEST03</button>
<button onclick="ocxT4();">TEST04</button>
<button onclick="ocxT5();">TEST05</button>
</div>
<br><br>
<div >
<button onclick="ocx1T();">ocx2_1</button>
<button onclick="ocx1T1();">ocx2_2</button>
</div>
</body>
</html>
通过注册表获取控件clsid
HRESULT hr = NULL;
IDispatch* pIDispatch = NULL;
hr = ::CoInitialize(NULL);
CLSID clsid;
LPCOLESTR Lregname = A2OLE(regname.c_str()); //控件名
hr = CLSIDFromProgID( Lregname,&clsid);
//得到IDispatch接口指针
hr = ::CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IDispatch, (void**)&pIDispatch);
std::string str = methname;
LPOLESTR m_name = A2OLE(str.c_str()); //控件中方法名
if (SUCCEEDED(hr))
{
DISPID dispid;
// 得到DISPID
hr=pIDispatch->GetIDsOfNames(IID_NULL,&m_name,1,::GetUserDefaultLCID(),&dispid);
if (SUCCEEDED(hr))
{
VARIANT VarResult;
::VariantInit(&VarResult);
UINT iError = -1;
DISPPARAMS param; //定义参数
param.cArgs = arguments.size();//参数个数
param.cNamedArgs = 0;
param.rgdispidNamedArgs = NULL;
param.rgvarg = new VARIANTARG[param.cArgs]; //定义参数类型和赋值
//获取js调用方法中的参数
for (size_t i = 0; i < arguments.size(); i++)
{
int j = arguments.size()-i-1;
if(arguments[j]->IsString())
{
param.rgvarg[i].vt = VT_BSTR;
param.rgvarg[i].bstrVal = SysAllocString(arguments[j]->GetStringValue().c_str());
}
else if (arguments[j]->IsInt())
{
param.rgvarg[i].vt = VT_I2;
param.rgvarg[i].intVal = arguments[j]->GetIntValue();
}
else if (arguments[j]->IsBool())
{
param.rgvarg[i].vt = VT_BOOL;
param.rgvarg[i].boolVal = arguments[j]->GetBoolValue();
}
else
return;
}
// 通过DISPID使用方法
hr = pIDispatch->Invoke(dispid, IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD, ¶m, &VarResult, NULL, &iError);
if (!SUCCEEDED(hr))
return;
//V8回调结果
retval = CefV8Value::CreateString(VarResult.bstrVal);
//释放缓存
for (size_t i = 0; i < arguments.size(); ++i)
{
SysFreeString(param.rgvarg[i].bstrVal);
}
delete [] param.rgvarg;
}
pIDispatch->Release();
}
主要是不同控件里有相同的方法名,调用的时候总是被最后一个覆盖。
bool ClientV8ExtensionHandler::Execute(const CefString &name, CefRefPtr<CefV8Value> object,
const CefV8ValueList &arguments, CefRefPtr<CefV8Value> &retval,
CefString &exception)
顺便问一下,&name只是检索方法名么?