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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace Start10
{

    public class Everything
    {
        /// 
        /// 是否64位
        /// 
        /// 
        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);
    }

 

你可能感兴趣的:(计算机,c#开发,dll引用)