How to use the correct unmanaged DLL file according CPU architecture? (32 / 64 bits)

后端 未结 3 1663
滥情空心
滥情空心 2020-12-17 01:01

I have to use a C++ DLL file from an ASP.NET site. The site will be hosted on both 32 and 64 bits environments.

I have a 32 and 64 bits version of the unmanaged DLL

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-17 01:23

    To get the compiler/framework to do most of the work you need to

    1. have multiple build 'platforms' (typically x86, x64 - remove AnyCPU)
    2. set each "platform target" configuration for each build config
    3. we added conditional compilation symbols __WIN32 & __X64

    List the different implementations for your functions according to platform, including different dll names if you need to have both installed at once.

    #if __WIN32
            public delegate int Move(int target);
            [DllImport("my.dll", SetLastError = true, CharSet = CharSet.Auto)]
    #elif __X64
            public delegate int Move(int target);
            [DllImport("my64.dll", SetLastError = true, CharSet = CharSet.Auto)]
    #endif
    

    Otherwise you can use loadlib and manage the marshalling yourself.

提交回复
热议问题