ActiveX html 控件不调用代码

发布于 2024-11-26 15:17:55 字数 2259 浏览 1 评论 0原文

我有一个 ActiveX HTML 对象,如下定义:

<object id="HSMBTPrintX1" width="350" height="350"
   classid="CLSID:68d05400-18a6-4b39-b3ff-a17d77c1eddf" codebase=".\HSMBTPrintX.ocx">

和一个按钮:

<input type="button" name="Print" value="Print" onclick="PassParameter()" />

该按钮调用按钮和控件上方定义的一些 javascript:

<script language="javascript" type="text/javascript">
    function PassParameter() {
            if (StringInput.value != " ") {
                alert('1 - ' + HSMBTPrintX1);
                HSMBTPrintX1.InputParameter = StringInput.value;
                alert('2 - ' + StringInput.value);
                var temp = HSMBTPrintX1.PrintLabel();
                alert('3 -' + temp);
            }
    }
</script>

我的 PrintLabel C++ 代码方法,此处:

char* CHSMBTPrintXCtrl::PrintLabel(void)
{
    return "temp";
    //AFX_MANAGE_STATE(AfxGetStaticModuleState());

    // TODO: Add your dispatch handler code here
    TCHAR g_szComPort[16];
    char szout[512];
    HANDLE hCom;
    TCHAR comPort[16];
    unsigned long bytesWritten;
    int counter;
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    if (!FindBluetoothPort(g_szComPort))
    return "minus two";
    hCom = CreateFile(comPort,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);
    if (hCom == NULL)
    return "minus one";
    CStringA charstr(m_InputParameter);
    // Create text to send to printer
    strcpy(szout, "^XA\n");
    strcat(szout, "^PRC ^LH0,0 ^LL203 ^PW406 ^MD5 ^MFF,F ^MMT ^MNM ^CF0,20,12 ^XB\n");
    strcat(szout, "^FO20,05 ^AFN ^FV MASTER PACK ^FS\n");
    strcat(szout, "^XZ");

    // Write file to Bluetooth COM port
    if(WriteFile(hCom,szout,256,&bytesWritten,NULL)==0)
    {
        return "one";
    }
    return "two";
    FireLabelPrinted();
}

如您所见,(仅用于测试),只要函数被调用它返回一些文本。我尝试在 JavaScript 警报中输出此文本。

问题是,“temp”javascript 变量输出为“未定义”...所以我假设 PrintLabel 函数甚至从未被调用。


对于一些背景信息(可能有助于或可能没有帮助得出结论),我正在使用 VS2008 创建一个 Visual C++ 智能设备项目。该解决方案还添加了智能设备 CAB 项目。然后构建整个解决方案;我将 .CAB、.INF、.OCX 和 .HTML 文件复制到运行 Windows CE 的手持设备上的单个文件夹中。

另外,我对 C++ 和 ActiveX 都不是很熟悉。任何帮助表示赞赏!

如果我需要提供更多信息,请为我指明方向。

谢谢!

I have an ActiveX HTML Object as defined here:

<object id="HSMBTPrintX1" width="350" height="350"
   classid="CLSID:68d05400-18a6-4b39-b3ff-a17d77c1eddf" codebase=".\HSMBTPrintX.ocx">

And a button:

<input type="button" name="Print" value="Print" onclick="PassParameter()" />

This button calls some javascript defined above the button and control:

<script language="javascript" type="text/javascript">
    function PassParameter() {
            if (StringInput.value != " ") {
                alert('1 - ' + HSMBTPrintX1);
                HSMBTPrintX1.InputParameter = StringInput.value;
                alert('2 - ' + StringInput.value);
                var temp = HSMBTPrintX1.PrintLabel();
                alert('3 -' + temp);
            }
    }
</script>

My PrintLabel C++ code method, here:

char* CHSMBTPrintXCtrl::PrintLabel(void)
{
    return "temp";
    //AFX_MANAGE_STATE(AfxGetStaticModuleState());

    // TODO: Add your dispatch handler code here
    TCHAR g_szComPort[16];
    char szout[512];
    HANDLE hCom;
    TCHAR comPort[16];
    unsigned long bytesWritten;
    int counter;
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    if (!FindBluetoothPort(g_szComPort))
    return "minus two";
    hCom = CreateFile(comPort,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);
    if (hCom == NULL)
    return "minus one";
    CStringA charstr(m_InputParameter);
    // Create text to send to printer
    strcpy(szout, "^XA\n");
    strcat(szout, "^PRC ^LH0,0 ^LL203 ^PW406 ^MD5 ^MFF,F ^MMT ^MNM ^CF0,20,12 ^XB\n");
    strcat(szout, "^FO20,05 ^AFN ^FV MASTER PACK ^FS\n");
    strcat(szout, "^XZ");

    // Write file to Bluetooth COM port
    if(WriteFile(hCom,szout,256,&bytesWritten,NULL)==0)
    {
        return "one";
    }
    return "two";
    FireLabelPrinted();
}

And as you can see, (just for testing), as soon as the function is called it returns some text. I attempt to output this text in a javascript alert.

The issue is, the "temp" javascript variable outputs as "undefined"... so i'm assuming the PrintLabel function never even gets called.


For some background information (which may or may not help in a conclusion), i am using VS2008, creating a Visual C++ Smart Device project. The solution also has a Smart Device CAB Project added on. The entire solution is then built; I copy the .CAB, .INF, .OCX and .HTML files to a single folder on a handheld device running Windows CE.

Additionally, I am not very familiar with C++... nor ActiveX. Any help is appreciated!!

If i need to provide more information, just point me in a direction.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝海 2024-12-03 15:17:55

为了向 javascript 公开一个新方法,您需要将其添加到您的 IDL 文件中;此外,您将无法从 ActiveX 控件返回 char*,您需要返回 BSTR。在您的情况下,当函数超出范围并变得无效时, char* 将被释放。

实际发生的情况是,javascript 检查 COM 接口上的 PrintLabel 方法,但没有找到,只是默认为未定义。

In order to expose a new method to javascript you need to add it to your IDL file; in addition, you won't be able to return a char* from an ActiveX control, you'll need to return a BSTR. In your case, the char* is released when the function goes out of scope and becomes invalid anyway.

What is actually occurring is that javascript checks for a PrintLabel method on the COM interface, doesn't find one, and just defaults to undefined.

开始看清了 2024-12-03 15:17:55

除了@Taxilian 的评论之外,您还必须实现IObjectSafety 否则浏览器将无法访问该对象(这是针对 CE 浏览器的控件最常见的遗漏之一)。

In addition to @Taxilian's comments, you also must implement IObjectSafety or the browser won't be able to access the object (this is one of the more common omissions for CE browser-targeted controls).

懵少女 2024-12-03 15:17:55

我认为问题出在我的 HTML 对象上...我现在有了版本号,并且类 ID 全部大写:

<object id="HSMBTPrintX1" width="350" height="350"
                    classid="CLSID:68D05400-18A6-4B39-B3FF-A17D77C1EDDF"
                    codebase=".\HSMBTPrintX.ocx#1,0,0,1">

现在一切正常。

I think the issue was with my HTML object... i now have the version number, and all caps for the class id:

<object id="HSMBTPrintX1" width="350" height="350"
                    classid="CLSID:68D05400-18A6-4B39-B3FF-A17D77C1EDDF"
                    codebase=".\HSMBTPrintX.ocx#1,0,0,1">

everything is working fine now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文