Проблема с доступом к COM-объекту из JavaScript
У меня есть .NET сборка, которая должна работать как ActiveX компонент. Вот код:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Security;
using System.Security.Permissions;
using System.ComponentModel;
[assembly: AllowPartiallyTrustedCallers]
namespace MyLibrary
{
[
Guid("BC4DD1A3-5E8C-442A-9F7E-1C2234465FCE"),
InterfaceType(ComInterfaceType.InterfaceIsDual),
ComVisible(true)
]
public interface IMyInterface
{
[DispId(1)]
string GetMessage();
[DispId(2)]
int DisplayAlert(string text);
}
[
Guid("92E55448-D14E-455F-A6BE-4F97DA9FB625"),
ProgId("MyLibrary.MyComponent"),
ClassInterface(ClassInterfaceType.None),
ComDefaultInterface(typeof(IMyInterface)),
ComVisible(true)
]
public class MyComponent : IMyInterface
{
[ZoneIdentityPermission(SecurityAction.Demand, Zone = SecurityZone.Intranet)]
public string GetMessage()
{
MessageBox.Show("Привет мир!");
return "Сообщение от MyComponent";
}
public int DisplayAlert(string text)
{
MessageBox.Show(text, "Уведомление");
return 1;
}
}
}
Регистрация прошла успешно через regasm /codebase /tlb /verbose MyLibrary.dll
. В реестре появилась запись “MyLibrary.MyComponent”, в IE компонент отображается как включенный.
HTML код:
<!DOCTYPE html>
<html>
<head>
<title>Тест ActiveX</title>
</head>
<body onload="InitializeComponent()">
<OBJECT id="myActiveX" name="myActiveX"
classid="clsid:92E55448-D14E-455F-A6BE-4F97DA9FB625"
codebase="MyLibrary.MyComponent">
</OBJECT>
<script type="text/javascript">
function InitializeComponent() {
try {
document.myActiveX.GetMessage();
} catch(error) {
alert("Ошибка: " + error.description);
}
}
</script>
</body>
</html>
Проблема: Получаю ошибку “Unable to get value of the property ‘myActiveX’: object is null or undefined”. CLSID правильный (проверял), но что-то не так с доступом к объекту. Может быть проблема в атрибутах id/name/codebase или в способе вызова методов?