Setting dllimport programmatically in C#

后端 未结 9 1909
情话喂你
情话喂你 2020-12-30 11:10

I am using DllImport in my solution.
My problem is that I have two versions of the same DLL one built for 32 bit and another for 64 bit.

They both e

相关标签:
9条回答
  • 2020-12-30 11:15

    I prefer to do this by using the LoadLibrary call from kernel32.dll to force a particular DLL to load from a specific path.

    If you name your 32-bit and 64-bit DLLs the same but placed them in different paths, you could then use the following code to load the correct based on the version of Windows you are running. All you need to do is call ExampleDllLoader.LoadDll() BEFORE any code referencing the ccf class is referenced:

    private static class ccf
    {
        [DllImport("myDllName")]
        public static extern int func1();
    }
    
    public static class ExampleDllLoader
    {
        [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
        private extern static IntPtr LoadLibrary(string librayName);
    
        public static void LoadDll()
        {
            String path;
    
            //IntPtr.Size will be 4 in 32-bit processes, 8 in 64-bit processes 
            if (IntPtr.Size == 4)
                path = "c:/example32bitpath/myDllName.dll";
            else
                path = "c:/example64bitpath/myDllName.dll";
    
            LoadLibrary(path);
        }
    }
    
    0 讨论(0)
  • 2020-12-30 11:19

    you can create two methods and choose one in a runtime, so you can keep Any CPU

    public static class Ccf
    {
        [DllImport(myDllName32)]
        private static extern int func32();
    
        [DllImport(myDllName64)]
        private static extern int func64();
    
    
        public static int func()
        {
            if(Environment.Is64BitProcess)
            {
                return func64();
            }
            return func32();
        }
    

    }

    0 讨论(0)
  • 2020-12-30 11:22

    Hmm, I'm wondering if you could create an interface and then a class with the methods based on the 32 bit and 64 bit dlls.

    I'm not sure if there is an explicit method to determine if you are running 64 bit, but the following might work: allow unsafe code and have an unsafe function that gets a pointer to some address and then determine whether the pointer is 4 or 8 bytes in size. Based on the result determine which implementation of the interface to create.

    0 讨论(0)
  • 2020-12-30 11:24

    You can probably achieve this with the #if keyword. If you define a conditional compiler symbol called win32, the following code will use the win32-block, if you remove it it will use the other block:

    #if win32
        private static class ccf_32
        {
            [DllImport(myDllName32)]
            public static extern int func1();
        }
    #else    
        private static class ccf_64
        {
            [DllImport(myDllName64)]
            public static extern int func1();
        }
    #endif
    

    This probably means that you can remove the class wrapping that you have now:

        private static class ccf
        {
    #if win32
            [DllImport(myDllName32)]
            public static extern int func1();
    #else    
            [DllImport(myDllName64)]
            public static extern int func1();
    #endif
        }
    

    For convenience, I guess you could create build configurations for controlling the compilation symbol.

    0 讨论(0)
  • 2020-12-30 11:27

    Why not wrap them into a method?

    private static class ccf_32_64
    {
        private static class ccf_32
        {
            [DllImport(myDllName32)]
            private static extern int func1();
        }
    
        private static class ccf_64
        {
            [DllImport(myDllName64)]
            private static extern int func1();
        }
    
        public static int func1()
        {
            if (32bit)
            {
                return ccf_32.func1();
            }
            else
            {
                return ccf_64.func1();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 11:33

    One alternative option is to have both the 32- and 64-bit versions of the unmanaged DLL have the same name, but have them live in separate folders in your build output (say, x86\ and x64\).

    Then, your installer or however else you're distributing this is updated so it knows to install the proper DLL for the platform it's installing on.

    0 讨论(0)
提交回复
热议问题