我有几个与 IIS 和 ActiveX 控件相关的问题。
我正在通过本教程http://haseebakhtar.wordpress.com/2011/05/31/creating-an-activex-control-in-net-using-c/开发一个 activeX 控件。我使用 IE9 进行测试,使用 VS 2010 作为 IDE。这是代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
namespace AxControls
{
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("42BBA00A-515E-45b5-9EAF-3827F7AEB4FA")]
[ProgId("AxControls.HelloWorld")]
[ComDefaultInterface(typeof(IClip))]
public class HelloWorld : UserControl, IObjectSafety, IClip
{
public HelloWorld()
{
MessageBox.Show("Starting");
}
[STAThread]
public Image GetClipboardImage()
{
MessageBox.Show("try to get image");
Image returnImage = null;
if (Clipboard.ContainsImage())
{
MessageBox.Show("getting image");
returnImage = Clipboard.GetImage();
}
return returnImage;
}
private void SaveImage(Image image)
{
try
{
if (image != null)
{
MessageBox.Show("saving image");
image.Save("C:\\test.jpg");
}
}
catch (Exception e)
{
MessageBox.Show(e.StackTrace);
}
}
public void SaveImgFromClipBoard()
{
Image img = GetClipboardImage();
SaveImage(img);
}
#region IObjectSafety Members
public enum ObjectSafetyOptions
{
INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001,
INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002,
INTERFACE_USES_DISPEX = 0x00000004,
INTERFACE_USES_SECURITY_MANAGER = 0x00000008
};
public int GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions)
{
ObjectSafetyOptions m_options = ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_CALLER | ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_DATA;
pdwSupportedOptions = (int)m_options;
pdwEnabledOptions = (int)m_options;
return 0;
}
public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
{
return 0;
}
#endregion
}
}
这是另一个代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace AxControls
{
[ComImport()]
[Guid("5DC2DE9B-8D7D-490e-A904-C8CBFFD38412")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IObjectSafety
{
[PreserveSig()]
int GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions);
[PreserveSig()]
int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions);
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace AxControls
{
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("013BFB31-B3F8-4bae-B5C2-5F6D22B815E3")]
public interface IClip
{
void SaveImgFromClipBoard();
}
}
这就是我所说的:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace AxControls
{
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("013BFB31-B3F8-4bae-B5C2-5F6D22B815E3")]
public interface IClip
{
void SaveImgFromClipBoard();
}
}
1)所以,当我刷新页面时,我看到两个带有“开始”的消息框,它们一个接一个出现。我不明白为什么。我在调试模式下运行它(当然使用 IIS + ASP.NET Web 项目 + 其中的 HTML 文件)。那是我的后期构建动作
"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x86\gacutil.exe" /f /i "$(TargetDir)$(TargetFileName)"
"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64\gacutil.exe" /f /i "$(TargetDir)$(TargetFileName)"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" /unregister "$(TargetDir)$(TargetFileName)"
"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe" /unregister "$(TargetDir)$(TargetFileName)"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" "$(TargetDir)$(TargetFileName)"
"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe" "$(TargetDir)$(TargetFileName)"
我什至尝试在 Apache Web 服务器上使用它——但情况相同——构造函数似乎调用了两次。
2) 很多时候,当我在 Visual Studio 中更改我的 HTML 文件并在调试模式下运行它时,这些更改在我的浏览器中不可见。我试图清理 IE 历史记录和缓存但没有成功。看起来 IIS 将其放入缓存中。是真的吗?我怎样才能避免它?
3) 我无法理解添加所有 IObjectSafety 成员的目的。
更新:
<html>
<head>
<object name="axHello" style='display:none' id='axHello' classid='CLSID:42BBA00A-515E-45b5-9EAF-3827F7AEB4FA'
codebase='AxControls.cab#version=1,0,0,0'></object>
<script language="javascript">
<!-- Load the ActiveX object -->
var x = new ActiveXObject("AxControls.HelloWorld");
<!-- Display the String in a messagebox ffff-->
x.SaveImgFromClipBoard();
</script>
</head>
<body>
</body>
</html>