EDN Admin
Well-known member
Hi,<br/>
<br/>
running this code to set a property on the hosted Sysmon.ocx ActiveX control and calling a parameterless method works fine:<br/>
<br/>
<pre lang="x-c# using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Reflection;
namespace TestApplication
{
public partial class Form1 : Form
{
private AxHostingControl axHost;
private object ocxObject;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ocxObject.GetType().InvokeMember("DisplayProperties", BindingFlags.InvokeMethod, null, ocxObject, null);
}
private void Form1_Load(object sender, EventArgs e)
{
Application.OleRequired();
this.axHost = new AxHostingControl(Type.GetTypeFromProgID("Sysmon",true).GUID.ToString());
this.Controls.Add(this.axHost);
this.ocxObject = axHost.GetOcx();
}
private void button2_Click(object sender, EventArgs e)
{
ocxObject.GetType().InvokeMember("ShowToolbar", BindingFlags.SetProperty, null, ocxObject, new object[] { false });
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
this.axHost.Dispose();
}
private void button3_Click(object sender, EventArgs e)
{
object[] Parameters = new object[2];
Parameters[0] = "\Process(*)\% Processor Time";
Parameters[1] = new object();
try
{
ocxObject.GetType().InvokeMember("AddCounter", BindingFlags.InvokeMethod, null, ocxObject, Parameters);
}
catch (Exception err)
{
if (err.InnerException is COMException)
{
MessageBox.Show((err.InnerException as COMException).Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show(err.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
internal class AxHostingControl : AxHost
{
public AxHostingControl(
string clsidProgId)
: base(clsidProgId)
{
}
}
}
[/code]
<br/>
<br/>
<br/>
But how do i call "AddCounter" dynamically like the ones above? The given call here fails with E_FAIL. I cant use a wrapper assembly like a Sysmon.interop.dll or something like that. I have to do this at runtime via reflection. Any idea?<br/>
<br/>
Thanks in advance,...<br/>
<br/>
K.<br/>
<
------------------------- Beste GrÃsse / Best regards / Votre bien devoue Kerem GÃmrÃkcà http://entwicklung.junetz.de ------------------------- "This reply is provided as is, without warranty express or implied."<br/>
View the full article
<br/>
running this code to set a property on the hosted Sysmon.ocx ActiveX control and calling a parameterless method works fine:<br/>
<br/>
<pre lang="x-c# using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Reflection;
namespace TestApplication
{
public partial class Form1 : Form
{
private AxHostingControl axHost;
private object ocxObject;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ocxObject.GetType().InvokeMember("DisplayProperties", BindingFlags.InvokeMethod, null, ocxObject, null);
}
private void Form1_Load(object sender, EventArgs e)
{
Application.OleRequired();
this.axHost = new AxHostingControl(Type.GetTypeFromProgID("Sysmon",true).GUID.ToString());
this.Controls.Add(this.axHost);
this.ocxObject = axHost.GetOcx();
}
private void button2_Click(object sender, EventArgs e)
{
ocxObject.GetType().InvokeMember("ShowToolbar", BindingFlags.SetProperty, null, ocxObject, new object[] { false });
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
this.axHost.Dispose();
}
private void button3_Click(object sender, EventArgs e)
{
object[] Parameters = new object[2];
Parameters[0] = "\Process(*)\% Processor Time";
Parameters[1] = new object();
try
{
ocxObject.GetType().InvokeMember("AddCounter", BindingFlags.InvokeMethod, null, ocxObject, Parameters);
}
catch (Exception err)
{
if (err.InnerException is COMException)
{
MessageBox.Show((err.InnerException as COMException).Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show(err.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
internal class AxHostingControl : AxHost
{
public AxHostingControl(
string clsidProgId)
: base(clsidProgId)
{
}
}
}
[/code]
<br/>
<br/>
<br/>
But how do i call "AddCounter" dynamically like the ones above? The given call here fails with E_FAIL. I cant use a wrapper assembly like a Sysmon.interop.dll or something like that. I have to do this at runtime via reflection. Any idea?<br/>
<br/>
Thanks in advance,...<br/>
<br/>
K.<br/>
<
------------------------- Beste GrÃsse / Best regards / Votre bien devoue Kerem GÃmrÃkcà http://entwicklung.junetz.de ------------------------- "This reply is provided as is, without warranty express or implied."<br/>
View the full article