Everything.dll动态库调用,兼容32位和64位

27 篇文章 0 订阅
6 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace Start10
{

    public class Everything
    {
        /// <summary>
        /// 是否64位
        /// </summary>
        /// <returns></returns>
        public static bool IsSys64bit()
        {
            if (IntPtr.Size == 8)
            {

                return true;
            }
            else
            {

                return false;
            }

        }

        internal class Everything32
        {
            const string LIB_NAME = "Everything32.dll";

            #region 引用定义
            const int EVERYTHING_OK = 0;
            const int EVERYTHING_ERROR_MEMORY = 1;
            const int EVERYTHING_ERROR_IPC = 2;
            const int EVERYTHING_ERROR_REGISTERCLASSEX = 3;
            const int EVERYTHING_ERROR_CREATEWINDOW = 4;
            const int EVERYTHING_ERROR_CREATETHREAD = 5;
            const int EVERYTHING_ERROR_INVALIDINDEX = 6;
            const int EVERYTHING_ERROR_INVALIDCALL = 7;

            [DllImport(LIB_NAME)]
            public static extern int Everything_SetSearch(string lpSearchString);
            [DllImport(LIB_NAME)]
            public static extern void Everything_SetMatchPath(bool bEnable);
            [DllImport(LIB_NAME)]
            public static extern void Everything_SetMatchCase(bool bEnable);
            [DllImport(LIB_NAME)]
            public static extern void Everything_SetMatchWholeWord(bool bEnable);
            [DllImport(LIB_NAME)]
            public static extern void Everything_SetRegex(bool bEnable);
            [DllImport(LIB_NAME)]
            public static extern void Everything_SetMax(int dwMax);
            [DllImport(LIB_NAME)]
            public static extern void Everything_SetOffset(int dwOffset);

            [DllImport(LIB_NAME)]
            public static extern bool Everything_GetMatchPath();
            [DllImport(LIB_NAME)]
            public static extern bool Everything_GetMatchCase();
            [DllImport(LIB_NAME)]
            public static extern bool Everything_GetMatchWholeWord();
            [DllImport(LIB_NAME)]
            public static extern bool Everything_GetRegex();
            [DllImport(LIB_NAME)]
            public static extern UInt32 Everything_GetMax();
            [DllImport(LIB_NAME)]
            public static extern UInt32 Everything_GetOffset();
            [DllImport(LIB_NAME)]
            public static extern string Everything_GetSearch();
            [DllImport(LIB_NAME)]
            public static extern int Everything_GetLastError();

            [DllImport(LIB_NAME, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
            public static extern bool Everything_Query();

            [DllImport(LIB_NAME)]
            public static extern void Everything_SortResultsByPath();

            [DllImport(LIB_NAME)]
            public static extern int Everything_GetNumFileResults();
            [DllImport(LIB_NAME)]
            public static extern int Everything_GetNumFolderResults();
            [DllImport(LIB_NAME)]
            public static extern int Everything_GetNumResults();
            [DllImport(LIB_NAME)]
            public static extern int Everything_GetTotFileResults();
            [DllImport(LIB_NAME)]
            public static extern int Everything_GetTotFolderResults();
            [DllImport(LIB_NAME)]
            public static extern int Everything_GetTotResults();
            [DllImport(LIB_NAME)]
            public static extern bool Everything_IsVolumeResult(int nIndex);
            [DllImport(LIB_NAME)]
            public static extern bool Everything_IsFolderResult(int nIndex);
            [DllImport(LIB_NAME)]
            public static extern bool Everything_IsFileResult(int nIndex);
            [DllImport(LIB_NAME)]
            public static extern void Everything_GetResultFullPathName(int nIndex, StringBuilder lpString, int nMaxCount);
            [DllImport(LIB_NAME)]
            public static extern void Everything_Reset();
            #endregion
        }

        internal class Everything64
        {
            const string LIB_NAME = "Everything64.dll";

            #region 引用定义
            const int EVERYTHING_OK = 0;
            const int EVERYTHING_ERROR_MEMORY = 1;
            const int EVERYTHING_ERROR_IPC = 2;
            const int EVERYTHING_ERROR_REGISTERCLASSEX = 3;
            const int EVERYTHING_ERROR_CREATEWINDOW = 4;
            const int EVERYTHING_ERROR_CREATETHREAD = 5;
            const int EVERYTHING_ERROR_INVALIDINDEX = 6;
            const int EVERYTHING_ERROR_INVALIDCALL = 7;

            [DllImport(LIB_NAME)]
            public static extern int Everything_SetSearch(string lpSearchString);
            [DllImport(LIB_NAME)]
            public static extern void Everything_SetMatchPath(bool bEnable);
            [DllImport(LIB_NAME)]
            public static extern void Everything_SetMatchCase(bool bEnable);
            [DllImport(LIB_NAME)]
            public static extern void Everything_SetMatchWholeWord(bool bEnable);
            [DllImport(LIB_NAME)]
            public static extern void Everything_SetRegex(bool bEnable);
            [DllImport(LIB_NAME)]
            public static extern void Everything_SetMax(int dwMax);
            [DllImport(LIB_NAME)]
            public static extern void Everything_SetOffset(int dwOffset);

            [DllImport(LIB_NAME)]
            public static extern bool Everything_GetMatchPath();
            [DllImport(LIB_NAME)]
            public static extern bool Everything_GetMatchCase();
            [DllImport(LIB_NAME)]
            public static extern bool Everything_GetMatchWholeWord();
            [DllImport(LIB_NAME)]
            public static extern bool Everything_GetRegex();
            [DllImport(LIB_NAME)]
            public static extern UInt32 Everything_GetMax();
            [DllImport(LIB_NAME)]
            public static extern UInt32 Everything_GetOffset();
            [DllImport(LIB_NAME)]
            public static extern string Everything_GetSearch();
            [DllImport(LIB_NAME)]
            public static extern int Everything_GetLastError();

            [DllImport(LIB_NAME, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
            public static extern bool Everything_Query();

            [DllImport(LIB_NAME)]
            public static extern void Everything_SortResultsByPath();

            [DllImport(LIB_NAME)]
            public static extern int Everything_GetNumFileResults();
            [DllImport(LIB_NAME)]
            public static extern int Everything_GetNumFolderResults();
            [DllImport(LIB_NAME)]
            public static extern int Everything_GetNumResults();
            [DllImport(LIB_NAME)]
            public static extern int Everything_GetTotFileResults();
            [DllImport(LIB_NAME)]
            public static extern int Everything_GetTotFolderResults();
            [DllImport(LIB_NAME)]
            public static extern int Everything_GetTotResults();
            [DllImport(LIB_NAME)]
            public static extern bool Everything_IsVolumeResult(int nIndex);
            [DllImport(LIB_NAME)]
            public static extern bool Everything_IsFolderResult(int nIndex);
            [DllImport(LIB_NAME)]
            public static extern bool Everything_IsFileResult(int nIndex);
            [DllImport(LIB_NAME)]
            public static extern void Everything_GetResultFullPathName(int nIndex, StringBuilder lpString, int nMaxCount);
            [DllImport(LIB_NAME)]
            public static extern void Everything_Reset();
            #endregion
        }

        internal static void Everything_SetSearch(string sKeyWord)
        {
            if (IntPtr.Size == 8)
                Everything64.Everything_SetSearch(sKeyWord);
            else
                Everything32.Everything_SetSearch(sKeyWord);
        }

        internal static void Everything_Query()
        {
            if (IntPtr.Size == 8)
                Everything64.Everything_Query();
            else
                Everything32.Everything_Query();

        }

        internal static int Everything_GetNumResults()
        {
            if (IntPtr.Size == 8)
                return Everything64.Everything_GetNumResults();
            else
                return Everything32.Everything_GetNumResults();
        }
        internal static bool Everything_IsFolderResult(int idx)
        {
            if (IntPtr.Size == 8)
                return Everything64.Everything_IsFolderResult(idx);
            else
                return Everything32.Everything_IsFolderResult(idx);
        }
        
        internal static void Everything_SortResultsByPath()
        {
            if (IntPtr.Size == 8)
                Everything64.Everything_SortResultsByPath();
            else
                Everything32.Everything_SortResultsByPath();
        }

        internal static void Everything_GetResultFullPathName(int i, StringBuilder buf, int bufsize)
        {
            if (IntPtr.Size == 8)
                Everything64.Everything_GetResultFullPathName(i, buf, bufsize);
            else
                Everything32.Everything_GetResultFullPathName(i, buf, bufsize);

        }


    }

}

也可用如下方式定义简化:

低版本VS不支持非常量初始化

    internal static string LIB_NAME = IsSys64bit() ? "libTestDLL_X64.dll" : "libTestDLL.dll";
 
        [DllImport(LIB_NAME, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr Demo(string input, int cnt);

另一种封装方式:函数名不同,指定相同的EntryPoint调用函数名称

    //编译两个DLL,一个WIN32,一个X64
    //用别名方式声明后在C#中封装

    static bool is64bit = (IntPtr.Size == 8);
            [DllImport("t64.dll", EntryPoint = "test")]
    static extern int test64(byte[] contextArray);
    [DllImport("t32.dll", EntryPoint = "test")]
    static extern int test32(byte[] contextArray);
    public static int test(byte[] contextArray)
    {
        return is64bit ? test64(contextArray) : test32(contextArray);
    }

 

要在Electron + TypeScript + Vite项目中调用Everything.dll实现快速搜索,可以按照以下步骤进行操作: 1. 安装Everything软件并启动它,确保Everything.dll已经被正确安装。 2. 在项目中安装node-ffi和ref-napi,这两个模块可以帮助我们在Node.js中调用DLL函数。可以使用以下命令进行安装: ```bash npm install node-ffi ref-napi ``` 3. 创建一个名为everything.ts的TypeScript模块,并在其中定义我们要调用Everything.dll函数。可以参考以下代码: ```typescript import { FFI } from 'ffi-napi'; /** * Everything.dll中的函数声明 */ export interface EverythingDLL extends FFI.Library { Everything_SetSearchW: (search: Buffer) => void; Everything_QueryW: (wait: number) => void; Everything_GetNumResults: () => number; Everything_GetResultFullPathNameW: (index: number, buf: Buffer, size: number) => number; Everything_CleanUp: () => void; } /** * 加载Everything.dll */ const everythingDll: EverythingDLL = FFI.Library('Everything.dll', { Everything_SetSearchW: ['void', ['pointer']], Everything_QueryW: ['void', ['int']], Everything_GetNumResults: ['int', []], Everything_GetResultFullPathNameW: ['int', ['int', 'pointer', 'int']], Everything_CleanUp: ['void', []], }); export default everythingDll; ``` 4. 在项目中使用everything.ts模块来进行搜索。可以参考以下代码: ```typescript import everythingDll from './everything'; /** * 搜索文件 * @param search 搜索关键字 * @returns 搜索结果数组 */ export function searchFiles(search: string): string[] { // 将搜索关键字转为UTF-16编码的Buffer const searchBuffer = Buffer.from(search, 'ucs2'); // 设置搜索关键字 everythingDll.Everything_SetSearchW(searchBuffer); // 执行搜索 everythingDll.Everything_QueryW(1); // 获取搜索结果数量 const resultCount = everythingDll.Everything_GetNumResults(); // 获取搜索结果 const results: string[] = []; for (let i = 0; i < resultCount; i++) { const buf = Buffer.alloc(4096); everythingDll.Everything_GetResultFullPathNameW(i, buf, buf.length); const result = buf.toString('ucs2'); results.push(result); } // 清理搜索结果 everythingDll.Everything_CleanUp(); return results; } ``` 5. 在Electron应用程序中使用searchFiles函数进行搜索。可以参考以下代码: ```typescript import { app, BrowserWindow } from 'electron'; import { searchFiles } from './everything'; function createWindow() { const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true, }, }); win.loadFile('index.html'); // 在渲染进程中使用searchFiles函数进行搜索 win.webContents.on('did-finish-load', () => { const results = searchFiles('test'); console.log(results); }); } app.whenReady().then(() => { createWindow(); app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow(); } }); }); app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } }); ``` 这样就可以在Electron + TypeScript + Vite项目中调用Everything.dll实现快速搜索了。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@David Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值