Skip to content
qiannian edited this page Nov 23, 2023 · 1 revision

c# 示例

c# 免注册示例

Code

using System.Reflection;
using System.Runtime.InteropServices;

internal class OpSoft : IDisposable
{
    private readonly Type obj = null;
    private object obj_object = null;
    private const string REG_DLL = "tools.dll";
    private const string OP_DLL = "op_x64.dll";

    #region Dispose
    private bool disposed = false;   // 是否已经释放资源的标志
    public OpSoft()
    {
        obj = Type.GetTypeFromProgID("op.opsoft");
        obj_object = Activator.CreateInstance(obj);
    }
    ~OpSoft() => Dispose(false);
    protected virtual void Dispose(bool disposing)
    {
        if (!disposed)
        {
            if (obj_object != null)
            {
                Marshal.ReleaseComObject(obj_object);
                obj_object = null;
            }
        }
        disposed = true;
    }
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    #endregion

    #region Regist
    [DllImport(REG_DLL, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
    private static extern int setupA(string path);
    /// <summary> 免注册调用OP插件 </summary>
    public static bool Register()
    {
        return setupA(OP_DLL) == 1;
    }
    #endregion

    public string Ver()
    {
        object result;
        result = obj.InvokeMember("Ver", BindingFlags.InvokeMethod, null, obj_object, null);
        return result.ToString();
    }
}

使用示例

internal class Program
{
    static void Main(string[] args)
    {
        Console.ReadLine();
        if (!OpSoft.Register())
        {
            Console.WriteLine("注册失败");
            return;
        }
        OpSoft opSoft = new OpSoft();
        Console.WriteLine("Ver:" + opSoft.Ver());
        Console.ReadLine();

    }
}
Clone this wiki locally