Cannot get example from https://docs.microsoft.com/en-us/dotnet/standard/native-interop to work in 32-bit .NET Core
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);
}
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 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()
This is currently in our backlog @ms0713. We haven't started addressing this one yet.
@weshaggard @karelz who can help with this?
@luqunl, @shrah can you please help here?
@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.
Ping @rlordcardano Are you still seeing this? Did you try the solution @shrah mentioned?
Closing unless we get a new repo scenario.
@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!