ActiveX, Javascript, and BSTR

本文详细介绍了如何通过ActiveX控件实现JavaScript与ActiveX之间的字符串传递,包括从JavaScript向ActiveX传递字符串的方法及从ActiveX返回字符串到JavaScript的技术细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ActiveX, oh where to begin. I can say that ActiveX development is a good nightmare for any programmer. Let me mention that aside from being an older one of Microsoft’s technologies, there in fact very few resources on the topic itself. On CodeProject, you’ll find an example web control. The example shows you how to call your ActiveX functions and access it’s properties from Javascript. This example, although good, does not show you how you can with your ActiveX control interact with Javascript indepth.

One of the problems that I faced was how to return a string to Javascript from an ActiveX control function. Also, the reverse gave me some difficult, about how you send a string from Javascript to an ActiveX control function. It is not as easy as you might think.

Passing a string to an ActiveX control function from Javascript

First you must declare your function, in three different places no less. The first places is your IDL:

[id (01)] int32 PrintString(BSTR String);

Second, declare it in your control’s header.

int32 PrintString(LPCTSTR String);

And thirdly you must declare it in your ActiveX control dispatch map, as follows.

DISP_FUNCTION_ID(AxControl, "PrintString", DISPID_PRINTSTRING, PrintString, VT_I4, VTS_BSTR)

Once you declare your function all over the place, you can define it like so..

int32 AxControl::PrintString(LPCTSTR String)
{
printf("Javascript string: %s/n", String);
return 0;
}

Even though Javascript is suppose to return a wide-character BSTR, it sadly does not. If you try and attempt to convert the BSTR into a multi-character string it will only ending up giving you garbage. Perhaps I am wrong, but this was the only way I could get it to work. I found this solution at the bottom of the barrel while look at another person’s code who had implemented their ActiveX control this way.

In your Javascript code you can now do the following..

<script type="text/javascript">
AxControl.PrintString("Hello world");
</script>

Returning a string from an ActiveX control function to Javascript

Declare your function..
IDL:

[id (02)] BSTR GetString(int32 Method);

ActiveX control header..

BSTR GetString(int32 Method);

ActiveX dispatch map..

DISP_FUNCTION_ID(AxControl, "GetString", DISPID_GETSTRING, GetString, VT_BSTR, VTS_I4)

You can then define your function as follows.

BSTR AxControl::GetString(int32 Method)
{
uint16 WideString[120];
char *ReturnString;

if (Method == 1)
ReturnString = "Supplied method was 1";
else
ReturnString = "Supplied method was not 1";

if (MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, ReturnString, -1, WideString, 120) == 0)
return NULL;

return (BSTR)SysAllocString(WideString);
}

That is how you would return a string to Javascript from your ActiveX control. In your Javascript code then you might do something like what I have done below.

<script type="text/javascript">
alert(AxControl.GetString(1));
alert(AxControl.GetString(2));
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值