//调用 com名称 方法 参数
InvokeMethod("ScriptEngine.Script", "Run", new object[] { script });
public class COMInfo
{
public object Instance;
public Type COMType;
public COMInfo(Type type, object instance)
{
this.Instance = instance;
this.COMType = type;
}
}
[DllImport("ole32.dll")]
static extern int CLSIDFromProgID([MarshalAs(UnmanagedType.LPWStr)] string lpszProgID, out Guid pclsid);
public static object InvokeMethod(string comName, string methodName, object[] args)
{
object ret = null;
COMInfo com = GetCOMInfo(comName);
try
{
ret = com.COMType.InvokeMember(methodName, BindingFlags.Default | BindingFlags.InvokeMethod, null, com.Instance, args);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
return ret;
}
public static COMInfo GetCOMInfo(string comName)
{
COMInfo comInfo;
Type type;
object instance = null;
instance = CreateInstance(comName, out type);
comInfo = new COMInfo(type, instance);
return comInfo;
}
private static object CreateInstance(string progName, out Type type)
{
object instance = null;
type = null;
try
{
Guid clsid;
int result = CLSIDFromProgID(progName, out clsid);
type = Type.GetTypeFromCLSID(clsid, true);
instance = Activator.CreateInstance(type);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
return instance;
}