Late Binding for OutlookSecurityManager

Add-in Express™ Support Service
That's what is more important than anything else

Late Binding for OutlookSecurityManager
Late Binding for OutlookSecurityManager 
Greg Oetker




Posts: 4
Joined: 2006-05-14
Hi all,

I'm trying to write a generic email routine that uses late binding so I don't want to add a referrence for the OutlookSecurityManager, which would force the user to have it installed on there computer and the basis of this is to use the OutlookSecurityManager if it's installed or force the user to click Yes to the Outlook Security warnings.

Here is the code snippet I have, the problem is I can't set the ConnectTo and DisableOOMWarnings. If someone knows what I'm doing wrong could you please post some snippets here. Thanks in advance!


Type oSecManType = null;
object oSecMan = null;
				
// Get the Object Type for the OutlookSecurityManager
oSecManType = Type.GetTypeFromProgID("secman.OutlookSecurityManager", false);

// Set Security Properities
if (oSecManType != null)
{
	object oSecurityManager = Activator.CreateInstance(oSecManType);
	if (oSecurityManager != null)
	{
		oSecMan = MSOffice.InvokeMethod(oSecurityManager, "SecurityManager");
		if (oSecMan != null)
		{
			MSOffice.SetProperty(oSecMan, "ConnectTo", oApplication);
			MSOffice.SetProperty(oSecMan, "DisableOOMWarnings", true);
		}
	}
}

// Re-enable the Outlook Security
if (oSecMan != null)
{
	MSOffice.SetProperty(oSecMan, "DisableOOMWarnings", false);
}


The MSOffice.SetProperty is a wrapper routine I use:

private static void SetProperty(object obj, string sProperty, object oValue)
{
	object[] oParam = new object[1];
	oParam[0] = oValue;
	obj.GetType().InvokeMember(sProperty, BindingFlags.SetProperty, null, obj, oParam);
}

I have simular routines for GetProperty and InvokeMember. The SetProperty, GetProperty and InvokeMember routines work since I use them for all my MSOffice Word, Excel, PowerPoint and Outlook routines.
Posted 14 May, 2006 20:23:57 Top
Sergey Grischenko


Add-in Express team


Posts: 7235
Joined: 2004-07-05
Hi Greg.

The fact is that the secman.OutlookSecurityManager type doesn't have the ConnectTo method and DisableOOMWarnings property. Below you can find the .NET wrappers around the ConnectTo method and DisableOOMWarnings property. You can use them in your code.

public void ConnectTo(object outlookApp)
{
oSecMan.Connect(outlookApp);
}

public void DisableOOMWarnings(bool state)
{
if (oSecMan.Check(3) == 0)
oSecMan.Switch(3, (state?1:0));
}



Posted 15 May, 2006 09:07:30 Top
Greg Oetker




Posts: 4
Joined: 2006-05-14
Hi Sergey,
If secman.OutlookSecurityManager is the incorrect type which type do I need to use for the CreateInstance that will expose the ConnectTo and DisableOOMWarnings properties?

In your example you use oSecMan, I'm assuming this is being set via the CreateInstance(type) command, is this correct?
Posted 16 May, 2006 19:14:22 Top
Sergey Grischenko


Add-in Express team


Posts: 7235
Joined: 2004-07-05
Hi Greg.

The secman.OutlookSecurityManager is the correct type. This is a COM component that is used by the SecurityManager component. You should use this type if you want to take advantage of the late binding.

In your example you use oSecMan, I'm assuming this is being set via the CreateInstance(type) command, is this correct?

Yes, correct.
Posted 17 May, 2006 07:20:44 Top
Greg Oetker




Posts: 4
Joined: 2006-05-14
Hi Sergey,
Here is what I have based on the snippet you posted.


Type oSecManType = null; 
object oSecMan = null; 

// Get the Object Type for the OutlookSecurityManager
oSecManType = Type.GetTypeFromProgID("secman.OutlookSecurityManager", false);

// Set Security Properities
if (oSecManType != null)
{
	oSecMan = Activator.CreateInstance(oSecManType);
	if (oSecMan != null)
	{
		oSecMan.Connect(oApplication);
		
		if (oSecMan.Check(3) == 0) oSecMan.Switch(3, true); 
	}
}


The issue I have now is that since oSecMan is defined as an Object, I get the following errors:

- 'object' does not contain a definition for 'Connect'
- 'object' does not contain a definition for 'Check'
- 'object' does not contain a definition for 'Switch'

This leads me to belive your snippet of code is not defining oSecMan as object like my snippet of code. When I look at the object browser for the SecurityManager I see those three routines listed under the public abstract interface IOutlookSecurityManager. I guess my issue is I'm missing how to load the COM object and how to link to the routines above. If you could post a complete solution that would be helpful.

Thanks in advance,
Greg
Posted 17 May, 2006 23:23:50 Top
Sergey Grischenko


Add-in Express team


Posts: 7235
Joined: 2004-07-05
Hi Greg.

As far as I understand we are talking about the late binding.
My code requires the InvokeMember method to be used instead of '.'.
E.g. the following code
oSecMan.Switch(3, (state?1:0));

should be transformed to

oSecMan.GetType().InvokeMember("Switch", BindingFlags.InvokeMethod, null, oSecMan, new object[] {3, (state?1:0)});

I used '.' for shortening purposes.
Posted 18 May, 2006 08:41:08 Top
Greg Oetker




Posts: 4
Joined: 2006-05-14
Hi Sergey,
Thanks for clearing that up for me, the code snippet works great, thanks alot.

Here is the final Late Binding code that I used.


Type oSecManType = null;
object oSecMan = null;


// Set oApplication to an instance of the Outlook Application 
				
// Get the Object Type for the OutlookSecurityManager
oSecManType = Type.GetTypeFromProgID("secman.OutlookSecurityManager", false);

// Set Security Properities
if (oSecManType != null)
{
	// Get the Object Type for the OutlookSecurityManager
	oSecManType = Type.GetTypeFromProgID("secman.OutlookSecurityManager", false);

	// Set Security Properities
	if (oSecManType != null)
	{
		oSecMan = Activator.CreateInstance(oSecManType);
		if (oSecMan != null)
		{
		    oSecMan.GetType().InvokeMember("Connect", BindingFlags.InvokeMethod, null, oSecMan, oApplication); 
            oSecMan.GetType().InvokeMember("Switch", BindingFlags.InvokeMethod, null, oSecMan, new object[] {3, 1}); 
		}
	}
}

// Process the email here

// Re-enable the Outlook Security
if (oSecMan != null)
{
	oSecMan.GetType().InvokeMember("Switch", BindingFlags.InvokeMethod, null, oSecMan, new object[] {3, 0});
}


Once again thanks for the quick response and explanation on how to do late binding.

Greg
Posted 19 May, 2006 20:30:30 Top
Sergey Grischenko


Add-in Express team


Posts: 7235
Joined: 2004-07-05
Hi Greg.

I am very glad that it works for you. Let me know if you have any other difficulties with the OSM component.
Posted 22 May, 2006 08:45:04 Top