Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Add native pointer overloads of methods that accept IntPtr #76

Closed
11 tasks done
AArnott opened this issue Nov 23, 2015 · 12 comments
Closed
11 tasks done

Add native pointer overloads of methods that accept IntPtr #76

AArnott opened this issue Nov 23, 2015 · 12 comments
Assignees

Comments

@AArnott
Copy link
Collaborator

AArnott commented Nov 23, 2015

IntPtr parameters are harder to deal with than native pointer types (in C# anyway), and they express less in terms of type safety than pointers can.

We should consider changing our policy and code to use pointers instead of IntPtr.

One possibility is that we maintain two overloads of applicable P/Invoke methods: one that takes IntPtr and one that takes pointers, so that folks can call without using unsafe code. I don't have much sympathy for any anxiety over writing unsafe code since it's actually simpler, and partial trust is irrelevant for consumers of this library anyway. So the only reason to keep IntPtr around is for languages that don't support pointers (e.g. Powershell).

  • AdvApi32
  • BCrypt
  • DbgHelp
  • Gdi32
  • Hid
  • ImageHlp
  • Kernel32
  • NCrypt
  • Psapi
  • SetupApi
  • User32
@AArnott AArnott added this to the 0.1.0 (stable) milestone Nov 23, 2015
@AArnott
Copy link
Collaborator Author

AArnott commented Nov 23, 2015

CC: @vbfox

@vbfox
Copy link
Collaborator

vbfox commented Nov 23, 2015

Yes we need a list of all the pro/cons.

On the list of languages :

  • ✅ C# (unsafe / T* foo)
  • ✅ C++ (T* foo)
  • ✅ F# (let foo : nativeptr<'T>)
  • ✅ Boo (unsafe foo as T) didn't test myself but they had a unit test about unsafe (Language is now considered dead)
  • 🔵 VB.Net
  • 🔵 PowerShell
  • 🎱 Nemerle (?)
  • 🎱 IronSheme (?)
  • 🎱 IronPython (?)

✅: Can call pointer exposing method
🔵: Can call IntPtr overload even if pointer version exists
⁉️ Doesn't support the IntPtr overload if the pointer version is present
🎱: Didn't find anythging / not searched

Also for the two overloads solutions it's pretty easy to codegen the IntPtr overloads if we write the pointer ones.

@AArnott
Copy link
Collaborator Author

AArnott commented Nov 24, 2015

Can you add a legend for these icons above?

@vbfox
Copy link
Collaborator

vbfox commented Nov 24, 2015

Ok I differentiated between VB that doesn't allow unsafe but can call the IntPtr version if also present with the same name and languages that support it natively.

Turn out that powershell is able to call an IntPtr version too if it exists (Just tested) with the same name so no problem here either.

Everything seem to point that the dual pointer/IntPtr is the good way to go, but we need to codegen it or it will be problematic pretty fast.

@vbfox
Copy link
Collaborator

vbfox commented Nov 24, 2015

Also if we decide to go the "dual" way it's not really needed for a 0.x release, as we will only add overloads that aren't compatible and don't risk being picked by previously existing code.
But It would be nice to have that in 1.0 along with some variant of #70.

@AArnott
Copy link
Collaborator Author

AArnott commented Nov 28, 2015

Thanks, @vbfox. I agree: let's go for dual mode. Until we have the code gen going, we should at least offer IntPtr versions so we don't block out use by VB.
Presumably when we start code gen that converts IntPtr to pointers, we can remove any manually created pointer overloads.

@AArnott AArnott changed the title Consider using pointers instead of IntPtr Add native pointer overloads of methods that accept IntPtr Nov 28, 2015
@AArnott
Copy link
Collaborator Author

AArnott commented Dec 12, 2015

I just found out that unsafe code isn't allowed in C# iterator methods. That means that pointers aren't allowed in them, making it less convenient to call these methods even in a language that would otherwise allow it.

@AArnott
Copy link
Collaborator Author

AArnott commented Dec 28, 2015

An open question on resolving some of these is what to do about struct fields that are pointers. Should they be IntPtr or a native pointer type? Can we perhaps use native pointers, but offer properties that expose them as IntPtrs for the VB folks?

@AArnott
Copy link
Collaborator Author

AArnott commented Dec 28, 2015

Progress on fixing kernel32 is blocked on AArnott/CodeGeneration.Roslyn#1

@jmelosegui
Copy link
Collaborator

Well, if we want to be consistent, struct fields should be native pointer types and the codegen should inject property wrappers of those fields.

So I am leaning toward native pointer type and properties that expose them as IntPtrs.

@AArnott
Copy link
Collaborator Author

AArnott commented Dec 28, 2015

Agreed.

@AArnott
Copy link
Collaborator Author

AArnott commented Dec 29, 2015

#130 tracks the struct fields now.

@AArnott AArnott closed this as completed Dec 30, 2015
AArnott pushed a commit that referenced this issue Oct 30, 2022
Bumps [Microsoft.CodeAnalysis.FxCopAnalyzers](https://github.com/dotnet/roslyn-analyzers) from 3.3.0 to 3.3.1.
- [Release notes](https://github.com/dotnet/roslyn-analyzers/releases)
- [Changelog](https://github.com/dotnet/roslyn-analyzers/blob/master/PostReleaseActivities.md)
- [Commits](dotnet/roslyn-analyzers@v3.3.0...v3.3.1)

Signed-off-by: dependabot[bot] <[email protected]>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants