0

The SafeHandleZeroOrMinusOneIsInvalid or similar classes cannot be used in a Portable Class Library targeting .NET 4.0 and Windows Store Apps.

Does anybody know why or how one could use this or use a different class?

Yes, I would like some unsafe stuff in a portable class library.

nietras
  • 3,949
  • 1
  • 34
  • 38
  • Unfortunatelly, it is not possible. Portable Class Library is a quite small subset of BCL and does not allow you to use Win32 API calls. – ie. Sep 04 '12 at 09:59
  • but the SafeHandle as such is available and PInvoke is also I think, so one could just copy the source of SafeHandleZeroOrMinusOnesIsInvalid. Actually, what I need is basically a SafeHandle class for managing native memory using Marshal.AllocHGlobal which is available in a portable class library: http://msdn.microsoft.com/en-us/library/s69bkh17.aspx – nietras Sep 05 '12 at 11:41
  • Portable does allow calling Win32 APIs when targeting platforms that support it (in this case, .NET and Windows Store apps) – David Kean Sep 13 '12 at 16:55

1 Answers1

1

Portable is limited to the platforms that you are targeting. In this case, Windows Store apps doesn't expose this type. It's implementation is very simple, here's one I just whipped up:

public abstract class SafeHandleZeroOrMinusOneIsInvalid : SafeHandle
{
    protected SafeHandleZeroOrMinusOneIsInvalid(bool ownsHandle)
        : base(IntPtr.Zero, ownsHandle)
    {
    }

    public override bool IsInvalid
    {
        get { return base.handle == IntPtr.Zero || base.handle == (IntPtr)(-1); }
    }
}
David Kean
  • 5,722
  • 26
  • 26
  • Yes, that was my initial answer, just copy the implementation, but was hoping for a reason why Microsoft decided not to include these. There is none as far as I can tell... – nietras Sep 15 '12 at 08:31