Bug 2560 - Add a something similar to IntPtr for long type
Summary: Add a something similar to IntPtr for long type
Status: RESOLVED NOT_ON_ROADMAP
Alias: None
Product: Runtime
Classification: Mono
Component: Interop ()
Version: unspecified
Hardware: PC All
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-12-17 17:04 UTC by xanatos
Modified: 2017-07-11 22:04 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NOT_ON_ROADMAP

Description xanatos 2011-12-17 17:04:42 UTC
Under Windows a "native" long is always 4 bytes (Windows is ILP32/LLP64). Under Linux/MacOsX a "native" long can be 4 (32 bits processes) or 8 bytes (64 bits processes) (they are ILP32/LP64). This is a problem for native libraries that use long as parameters in functions and/or in structs. What I ask is something like the IntPtr struct that has a "variable" length depending on the bits of the process and the OS. For example it could be called NativeLong. It would be something like:

[StructLayout(LayoutKind.Explicit)]
public struct NativeLong
{
    /// <summary>
    /// 
    /// </summary>
    public static readonly int Size = !Environment.Is64BitProcess || (Environment.OSVersion.Platform == PlatformID.Win32S || Environment.OSVersion.Platform == PlatformID.Win32Windows || Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.WinCE) ? 4 : 8;

    /// <summary>
    /// 
    /// </summary>
    [FieldOffset(0)]
    [MarshalAs(UnmanagedType.I4)]
    public long Value;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static implicit operator NativeLong(long value)
    {
        return new NativeLong { Value = value };
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static implicit operator long(NativeLong value)
    {
        return value.Value;
    }
}

where the MarshalAs would be changed behind the scene by the runtime between I4 and I8.

Clearly a specular NativeULong struct would be possible.
Comment 1 Mikayla Hutchinson [MSFT] 2011-12-17 17:42:07 UTC
It's unfortunate that UnmanagedType.SysInt and SysUInt already exist and match long/ulong, but only on LP64. For that reason they're not very useful, and I don't think Mono implements them.
Comment 2 xanatos 2011-12-18 01:47:55 UTC
I would say that SysInt and SysUInt map to IntPtr and UIntPtr, so to pointer-length data type.

As an example of where this would be useful:

the OpenSSL library uses the long type as a parameter/return value in various functions. 

http://www.openssl.org/docs/crypto/OPENSSL_VERSION_NUMBER.html

    long SSLeay(void);

this function returns the version of the library... as a long.
Comment 3 xanatos 2011-12-18 06:05:35 UTC
Another possibility would be adding a MarshalAsExAttribute class. You would use it with the MarshalAs attribute, like this:

[MarshalAsEx(UnmanagedTypeEx.NativeLong)]
[MarshalAs(UnmanagedType.I4)]
long MyVal;

The .NET runtime would ignore it and always use the MarshalAs, while the Mono runtime could use the overrides from the MarshalAsEx. This could be extended to cover the "problems" of Ansi/Unicode/UTF8/UTF32 native calling that are present (http://tirania.org/blog/archive/2003/Sep-13.html).
Comment 4 Rodrigo Kumpera 2017-07-11 22:04:42 UTC
This is now up to the dotnet group to handle.