I'm attempting to write an ActiveX control for InfoPath using C# - which will allow the user to open a worksite folder browser dialog and select a folder.
I've been able to build a forms application which, when run, displays the Worksite Folder Browser Dialog & returns the objectID string if the user selects folder.
I've also been able to construct valid ActiveX controls for use in InfoPath (Test controls have been standard folder browsers, message boxes, text boxes, etc..) after referencing;
http://blogs.msdn.com/infopath/archive/2005/04/15/creating-an-infopath-custom-control-using-c-and-net.aspxhttp://blogs.msdn.com/infopath/archive/2006/12/18/creating-complex-infopath-controls-in-c-sharp.aspxHowever, when I attempt to create an ActiveX\InfoPath component which opens a Worksite folder browser dialog I receive a security permissions error;
'
Application attempted to perform an operation not allowed by the security policy. To grant this application the required permission, contact your system administrator, or use the Microsoft .NET Framework Configuration Tool.
If you click Continue, the application will ignore this error and attempt to continue.
System.Security.Permissions.SecurityPermission'
Followed by a Just-In-Time debug\Exception list of which of note '
The Zone of the assembly that failed was: MyComputer' is identified.
Checking the code it is specifically this line '
IManDMS dms = new ManDMSClass();' that is causing the error to be thrown.
I've attempted to change the security settings via the '
Microsoft .NET Framework 1.1 Configuration' – But this doesn't seem to have made any difference.
Would anyone be able to help identify what I'm doing wrong here & suggest a way to resolve this?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using IManage;
using IMANEXTLib;
using IMANEXT2Lib;
namespace WFCLTest6
{
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface ICOMControl
{
[DispId(UserControl1.DISPID_VALUE)]
string Value { get; set; }
[DispId(UserControl1.DISPID_ENABLED)]
bool Enabled { get; set; }
}
[ComImport]
[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
[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);
}
[ComImport]
[Guid("9BFBBC02-EFF1-101A-84ED-00AA00341D07")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IPropertyNotifySink
{
[PreserveSig]
int OnChanged(int dispId);
[PreserveSig]
int OnRequestEdit(int dispId);
}
public delegate int PropertyNotifySinkHandler(int dispId);
[Guid("1FEE489F-A555-4408-8FBF-3F69F8C57A43")]
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(IPropertyNotifySink))]
public partial class UserControl1 : UserControl, ICOMControl, IObjectSafety
{
public event PropertyNotifySinkHandler OnChanged;
public event PropertyNotifySinkHandler OnRequestEdit;
// Constants for implementation of the IObjectSafety interface.
private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;
private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;
private const int S_OK = 0;
// Constants for DISPIDs of the Value and Enabled properties.
internal const int DISPID_VALUE = 0;
internal const int DISPID_ENABLED = 1;
public UserControl1()
{
InitializeComponent();
}
// Implementation of the IObjectSafety methods.
int IObjectSafety.GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions)
{
pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
return S_OK; // return S_OK
}
int IObjectSafety.SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
{
return S_OK; // return S_OK
}
protected int Fire_OnRequestEdit(int dispId)
{
if (this.OnRequestEdit != null)
return this.OnRequestEdit(dispId);
else return 0;
}
protected int Fire_OnChanged(int dispId)
{
if (this.OnChanged != null)
return this.OnChanged(dispId);
else return 0;
}
// Implementation of the Value property get and set methods.
public string Value
{
get { return button1.Name; }
set { button1.Name = value; }
}
private void button1_Click(object sender, EventArgs e)
{
Fire_OnChanged(UserControl1.DISPID_VALUE);
button1ClickTest();
}
// [SerializableAttribute]
// [ComVisible(true)]
// public class DMS : ManDMSClass{}
[STAThread]
private void button1ClickTest()
{
Fire_OnChanged(UserControl1.DISPID_VALUE);
//try{
IManDMS dms = new ManDMSClass();
MessageBox.Show("Gets Here 0");
dms = new ManDMSClass();
MessageBox.Show("Gets Here 1");
IManSession sess = dms.Sessions.Add("DM-DMS");
sess.TrustedLogin();
NRTSession[] saSess = new NRTSession[1];
saSess[0] = (NRTSession)sess;
IManBrowseCmd cmd = new IManBrowseCmdClass();
ContextItems context = new ContextItemsClass();
context.Add("NRTDMS", dms);
context.Add("SelectedNRTSessions", saSess);
context.Add("IMANEXT2.BrowseCmd.SelectableObjectTypes", new imObjectType[] { imObjectType.imTypeDocumentFolder });
context.Add("IMANEXT2.MultiFileSelection", false);
context.Add("IManExt.Import.DocAuthor", "LEWISSJ"); //Example of a application type.
context.Add("ParentWindow", this.Handle.ToInt32());
cmd.Initialize(context);
cmd.Update();
if (cmd.Status == (int)CommandStatus.nrActiveCommand)
{
cmd.Execute();
}
//}catch (SecurityException s) { MessageBox.Show(s.Message); }
}//endbutton1clicktest
}
}