Bugzilla – Bug 321766
Unable to cast a ulong to a IntPtr
Last modified: 2007-09-15 21:24:23 UTC
---- Reported by stephen.gennard@microfocus.com 2006-08-10 19:04:42 MST ---- Description of Problem: Steps to reproduce the problem: 1. Create a .cs that contains: using System; namespace MicroFocus.MONO.Bugs { public class IntPtrTest { public static uint MyVirtualAddress32() { return (uint)2147483647; } public static ulong MyVirtualAddress64() { return (ulong)9223372036854775807; } public static int Main() { IntPtr x = IntPtr.Zero; if (IntPtr.Size == 4) x = (IntPtr)MyVirtualAddress32(); else x = (IntPtr)MyVirtualAddress64(); if (x != IntPtr.Zero) { Console.WriteLine("PASS"); } else { Console.WriteLine("FAIL"); } return 0; } } } 2. gmcs the above Actual Results: D:\development\netx50\ilrts\mono\bugs>call gmcs IntPtrTest1.cs IntPtrTest1.cs(24,10): error CS0030: Cannot convert type `ulong' to `System.IntP tr' Expected Results: D:\development\netx50\ilrts\mono\bugs>csc IntPtrTest1.cs Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42 for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727 Copyright (C) Microsoft Corporation 2001-2005. All rights reserved. D:\development\netx50\ilrts\mono\bugs>IntPtrTest1 PASS How often does this happen? Everytime we cast a ulong to a IntPtr Additional Information: The code snippet is from our COBOL compiler's Runtime... Our Runtime runs on both 32bit and 64bit, and as such is sensitive to the IntPtr.Size. Please note, the IntPtr does *NOT* contain a unmanaged physical address but contains a value in our own virtual address map. If you have any questions, please feel free to drop me a line. ---- Additional Comments From stephen.gennard@microfocus.com 2006-08-10 19:31:58 MST ---- Created an attachment (id=170236) demo ---- Additional Comments From jonpryor@vt.edu 2006-08-11 07:06:11 MST ---- The reason this fails on Mono is because System.IntPtr doesn't define any conversions from or to ulong -- there is no IntPtr(ulong) constructor, for example. A workaround is to introduce a cast: ulong value = ...; IntPtr p = (IntPtr) (long) value; You could also use (void*) as the intermediate cast. I would like to know why this works with CSC.EXE, as msdn2 doesn't list any IntPtr members that work with the UInt64, so either CSC.EXE has an internally-permitted conversion from ulong, or their class library has some members which aren't documented. ---- Additional Comments From stephen.gennard@microfocus.com 2006-08-11 07:51:02 MST ---- When using: int a = 42; IntPtr b = (IntPtr)a; Console.WriteLine(b); Microsoft generates a "conv.u8" and use the "op_Explicit(int64)", rather than using op_Explicit(int32) directly. So I think their compiler is indeed allowing the up sizing. The IL code snippet is: IL_0000: nop IL_0001: ldc.i4.s 42 IL_0003: stloc.0 IL_0004: ldloc.0 IL_0005: conv.u8 IL_0006: call native int [mscorlib] System.IntPtr::op_Explicit(int64) IL_000b: stloc.1 IL_000c: ldloc.1 IL_000d: box [mscorlib]System.IntPtr IL_0012: call void [mscorlib]System.Console::WriteLine (object) IL_0017: nop IL_0018: ldc.i4.0 IL_0019: stloc.2 IL_001a: br.s IL_001c IL_001c: ldloc.2 IL_001d: ret ---- Additional Comments From marek.safar@seznam.cz 2006-08-11 09:18:48 MST ---- *** This bug has been marked as a duplicate of https://bugzilla.novell.com/show_bug.cgi?id=MONO59800 *** Imported an attachment (id=170236) Unknown bug field "cf_op_sys_details" encountered while moving bug <cf_op_sys_details>Windows XP</cf_op_sys_details> Unknown operating system unknown. Setting to default OS "Other". This bug was marked DUPLICATE in the database it was moved from. Changing resolution to "MOVED"