不注册Activex 直接调用它
此处的Activex是ATL方式的。
[ComVisible(false)]
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000001-0000-0000-C000-000000000046")]
internal interface IClassFactory
{
void CreateInstance([MarshalAs(UnmanagedType.Interface)] object pUnkOuter, ref Guid refiid, [MarshalAs(UnmanagedType.Interface)] out object ppunk);
void LockServer(bool fLock);
}
public delegate void IETestKey(IntPtr values);
internal static class ComHelper
{
//DllGetClassObject fuction pointer signature
private delegate int DllGetClassObject(ref Guid ClassId, ref Guid InterfaceId, [Out, MarshalAs(UnmanagedType.Interface)] out object ppunk);
//Some win32 methods to load\unload dlls and get a function pointer
private class Win32NativeMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string lpFileName);
}
/// <summary>
/// Holds a list of dll handles and unloads the dlls
/// in the destructor
/// </summary>
private class DllList
{
private List<IntPtr> _dllList = new List<IntPtr>();
public void AddDllHandle(IntPtr dllHandle)
{
lock (_dllList)
{
_dllList.Add(dllHandle);
}
}
~DllList()
{
foreach (IntPtr dllHandle in _dllList)
{
try
{
Win32NativeMethods.FreeLibrary(dllHandle);
}
catch { };
}
}
}
static DllList _dllList = new DllList();
/// <summary>
/// Gets a class factory for a specific COM Class ID.
/// </summary>
/// <param name="dllName">The dll where the COM class is implemented</param>
/// <param name="filterPersistClass">The requested Class ID</param>
/// <returns>IClassFactory instance used to create instances of that class</returns>
internal static IClassFactory GetClassFactory(string dllName, string filterPersistClass)
{
//Load the class factory from the dll
IClassFactory classFactory = GetClassFactoryFromDll(dllName, filterPersistClass);
return classFactory;
}
private static IClassFactory GetClassFactoryFromDll(string dllName, string filterPersistClass)
{
//Load the dll
IntPtr dllHandle = Win32NativeMethods.LoadLibrary(dllName);
if (dllHandle == IntPtr.Zero)
return null;
//Keep a reference to the dll until the process\AppDomain dies
_dllList.AddDllHandle(dllHandle);
//Get a pointer to the DllGetClassObject function
IntPtr dllGetClassObjectPtr = Win32NativeMethods.GetProcAddress(dllHandle, "DllGetClassObject");
if (dllGetClassObjectPtr == IntPtr.Zero)
return null;
//Convert the function pointer to a .net delegate
DllGetClassObject dllGetClassObject = (DllGetClassObject)Marshal.GetDelegateForFunctionPointer(dllGetClassObjectPtr, typeof(DllGetClassObject));
//Call the DllGetClassObject to retreive a class factory for out Filter class
Guid filterPersistGUID = new Guid(filterPersistClass);
//Guid IClassFactoryGUID = new Guid("FCF82F0F-F8A9-4527-A36E-CC87FAFAA270"); //IClassFactory class id
Guid IClassFactoryGUID = new Guid("00000001-0000-0000-C000-000000000046"); //IClassFactory class id
Object unk;
if (dllGetClassObject(ref filterPersistGUID, ref IClassFactoryGUID, out unk) != 0)
return null;
//Yippie! cast the returned object to IClassFactory
return (unk as IClassFactory);
}
public static Delegate TestDll(string dllName)
{
IntPtr dllHandle = Win32NativeMethods.LoadLibrary(dllName);
if (dllHandle == IntPtr.Zero)
return null;
//Keep a reference to the dll until the process\AppDomain dies
//_dllList.AddDllHandle(dllHandle);
//Get a pointer to the DllGetClassObject function
IntPtr dllGetClassObjectPtr = Win32NativeMethods.GetProcAddress(dllHandle, "DllGetClassObject");
if (dllGetClassObjectPtr == IntPtr.Zero)
return null;
return (Delegate)Marshal.GetDelegateForFunctionPointer(dllGetClassObjectPtr, typeof(IETestKey));
}
}
不说废话,上代码
public enum IFilterReturnCode : uint
{
/// <summary>
/// Success
/// </summary>
S_OK = 0,
/// <summary>
/// The function was denied access to the filter file.
/// </summary>
E_ACCESSDENIED = 0x80070005,
/// <summary>
/// The function encountered an invalid handle,
/// probably due to a low-memory situation.
/// </summary>
E_HANDLE = 0x80070006,
/// <summary>
/// The function received an invalid parameter.
/// </summary>
E_INVALIDARG = 0x80070057,
/// <summary>
/// Out of memory
/// </summary>
E_OUTOFMEMORY = 0x8007000E,
/// <summary>
/// Not implemented
/// </summary>
E_NOTIMPL = 0x80004001,
/// <summary>
/// Unknown error
/// </summary>
E_FAIL = 0x80000008,
/// <summary>
/// File not filtered due to password protection
/// </summary>
FILTER_E_PASSWORD = 0x8004170B,
/// <summary>
/// The document format is not recognised by the filter
/// </summary>
FILTER_E_UNKNOWNFORMAT = 0x8004170C,
/// <summary>
/// No text in current chunk
/// </summary>
FILTER_E_NO_TEXT = 0x80041705,
/// <summary>
/// No more chunks of text available in object
/// </summary>
FILTER_E_END_OF_CHUNKS = 0x80041700,
/// <summary>
/// No more text available in chunk
/// </summary>
FILTER_E_NO_MORE_TEXT = 0x80041701,
/// <summary>
/// No more property values available in chunk
/// </summary>
FILTER_E_NO_MORE_VALUES = 0x80041702,
/// <summary>
/// Unable to access object
/// </summary>
FILTER_E_ACCESS = 0x80041703,
/// <summary>
/// Moniker doesn't cover entire region
/// </summary>
FILTER_W_MONIKER_CLIPPED = 0x00041704,
/// <summary>
/// Unable to bind IFilter for embedded object
/// </summary>
FILTER_E_EMBEDDING_UNAVAILABLE = 0x80041707,
/// <summary>
/// Unable to bind IFilter for linked object
/// </summary>
FILTER_E_LINK_UNAVAILABLE = 0x80041708,
/// <summary>
/// This is the last text in the current chunk
/// </summary>
FILTER_S_LAST_TEXT = 0x00041709,
/// <summary>
/// This is the last value in the current chunk
/// </summary>
FILTER_S_LAST_VALUES = 0x0004170A
}
[ComImport, Guid("接口的GUID")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface ISISSKeyManager
{
[DispId(1)]
IFilterReturnCode IETestKey([Out, MarshalAs(UnmanagedType.BStr)] out string KeyInfo);
[DispId(2)]
IFilterReturnCode CheckKeys([In, MarshalAs(UnmanagedType.BStr)]string KeyInfo, [In, MarshalAs(UnmanagedType.BStr)]string KeyInfo2, [Out, MarshalAs(UnmanagedType.BStr)]out string KeyTest);
}
public class LoadDllHelper
{
public static ISISSKeyManager LoadFilterFromDll(string dllName, string filterPersistClass)
{
//Get a classFactory for our classID
IClassFactory classFactory = ComHelper.GetClassFactory(dllName, filterPersistClass);
if (classFactory == null)
return null;
//And create an IFilter instance using that class factory
Guid IFilterGUID = new Guid("接口的GUID");
Object obj;
classFactory.CreateInstance(null, ref IFilterGUID, out obj);
return (obj as ISISSKeyManager);
}
}
调用方式:
var gethelperId = LoadDllHelper.LoadFilterFromDll("xxxx.dll", "com的GUID");
if (gethelperId != null)
{
string str = "1".PadLeft(300, '1');
IntPtr outPtr = Marshal.StringToBSTR(str);
var s = gethelperId.IETestKey(out str);
MessageBox.Show(s.ToString());
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构