Setting dllimport programmatically in C#

后端 未结 9 1957
情话喂你
情话喂你 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: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.

提交回复
热议问题