docs icon indicating copy to clipboard operation
docs copied to clipboard

Cannot get example from https://docs.microsoft.com/en-us/dotnet/standard/native-interop to work in 32-bit .NET Core

Open rlordcardano opened this issue 9 years ago • 8 comments

Cannot get example from https://docs.microsoft.com/en-us/dotnet/standard/native-interop to work in 32-bit .NET Core

I have tried to setup a new .NET Core console app, to call the SystemTime as in the example in the above link. The relevant snippet of code is pasted below. If the build is 32 bit, Visual Studio crashes with a "dotnet has crashed" error. If the build is 64 bit however, everything works seamlessly.

I can get the MessageBox to work in a 32 bit build, so it's something about GetSystemTime / kernel32.dll that causes the crash.

If this is well-known, that is fine - just thought it might be worth pointing out in the original documentation.

        [DllImport("user32.dll")]
        public static extern int MessageBox(IntPtr hWnd, String text, String caption, int options);

        [DllImport("kernel32.dll")]
        static extern void GetSystemTime(SystemTime systemTime);

        [StructLayout(LayoutKind.Sequential)]
        class SystemTime
        {
            public ushort Year;
            public ushort Month;
            public ushort DayOfWeek;
            public ushort Day;
            public ushort Hour;
            public ushort Minute;
            public ushort Second;
            public ushort Milsecond;
        }

        public static void Main(string[] args)
        {
            var st = new SystemTime();
            GetSystemTime(st);
            MessageBox(IntPtr.Zero, $"{st.Year}", "Attention!", 0);
        }

rlordcardano avatar Aug 01 '17 18:08 rlordcardano

Thanks for reporting this @rlordcardano. A lot of our content in the .NET Guide needs to be reviewed to make sure it works with .NET Core. We'll take a look at this issue.

mairaw avatar Aug 07 '17 20:08 mairaw

@mairaw Whats the progress on [DllImport("kernel32.dll")]? Let me know if you have worked on this or if you have any alternative to make it work on Linux.

I m using following method for Application. While running it on Linux it gets crashed as it is not able to load Assebly Kernel32.dll. [DllImport ("kernel32.dll")] public static extern uint GetCurrentThreadId()

ms0713 avatar Jan 04 '18 07:01 ms0713

This is currently in our backlog @ms0713. We haven't started addressing this one yet.

@weshaggard @karelz who can help with this?

mairaw avatar Jan 06 '18 02:01 mairaw

@luqunl, @shrah can you please help here?

karelz avatar Jan 07 '18 21:01 karelz

@rlordcardano are you hitting into this https://developercommunity.visualstudio.com/content/problem/17141/net-core-x86-console-app-crashes-1.html known issue? Putting the Directory.Build.targets as suggested on that page should fix your problem. Let me know if it still repros after applying that solution.

shrah avatar Jan 08 '18 19:01 shrah

Ping @rlordcardano Are you still seeing this? Did you try the solution @shrah mentioned?

BillWagner avatar Mar 06 '18 00:03 BillWagner

Closing unless we get a new repo scenario.

BillWagner avatar Mar 13 '18 22:03 BillWagner

@shrah @BillWagner @mairaw Apologies for late reply, I didn't spot this one in the many messages I get from GitHub. I just tried the suggested solution, it works perfectly now!

rlordcardano avatar Mar 18 '18 20:03 rlordcardano