Comments (7)
Well, in this case why not having only byte[]
?
Is there any reason to actually need the IntPtr
and byte*
overloads?
I thought we introduced pointers to have more meaningful parameters, and code generation to solve the gap for those languages that cannot call pointer exposing methods, but on those scenarios where we can use an array I don't see the need of the pointer at all (since the parameter is meaningful enough and it can be used by all languages)
from pinvoke.
@jmelosegui: that's a fair question.
There are two reasons for using byte*
instead of byte[]
. These reasons aren't usually compelling enough to warrant using byte*
so most methods that can should probably use byte[]
. But for a few methods or libraries, they are quite compelling. They are:
byte*
can point anywhere, including (for example) the middle of abyte[]
. So if you already have an array with the content you want to pass in, but that content isn't at the start of the array,byte*
lets you point directly at it, whereasbyte[]
requires that you copy the whole content into a new array just so it's at the beginning of it.byte*
enables you to be certain that the memory doesn't get copied or moved around by the CLR.byte[]
as a reference type can be moved during a GC's defragmentation of memory. If your data is sensitive (say, as part of a cryptographic operation) best practices suggest you scrub the buffer when you're done so that memory dumps don't capture the data. But if the buffer gets copied around in memory by the GC, you can't effectively scrub all the old locations. Locking the buffer location mitigates this, and a very convenient way to do this (if it's all local variables within a method) is using C#fixed
with pointers and not messing with multiple levels of try/finally and GC pinning abyte[]
.
So I expect to use this enhancement primarily in the BCrypt and NCrypt libraries (where I'm already writing these overloads by hand) and probably nowhere else.
from pinvoke.
I'm also using pointer inside arrays (Wrapped in an ArraySegment) for IO access to USB devices without allocating memory for each call.
It's not necessary for most API but if the codegen can do them automatically, why not have them ?
from pinvoke.
Thank you @AArnott and @vbfox for pointing out those use cases. Now I get the reasons behind this issue.
from pinvoke.
I've edited the proposal to include the addition of the MarshalAsAttribute
in the generated code.
from pinvoke.
I've removed [MarshalAs]
, since the generated methods are never P/Invoke methods themselves so it's not applicable.
Also: I've added a 3rd overload for folks who want to pass in byte[]
and native pointers for other parameters. I discovered the need for this combination during the migration to the code generated second overload -- a lot of my own extra overloads had this combination.
from pinvoke.
Also: I've added a 3rd overload for folks who want to pass in byte[] and native pointers for other parameters.
That makes a lot sense.
from pinvoke.
Related Issues (20)
- Request: Better exception handling or documentation for CreateFile HOT 3
- Refresh packages on NuGet HOT 1
- Change NTSTATUS/HResult FACILITY_CODE to match CsWin32 definitions HOT 3
- No release / release notes for 0.7.124 HOT 1
- User32.InvalidateRect missing
- Refactor FacilityCode bitshift op to Facility properties
- MCG0037 error release build with Kernel32 HOT 3
- Support for .NET 6 to include in Maui? HOT 2
- User32+MOUSEINPUT.cs has the wrong type for mouseData
- `User32.EnumChildWindows` has wrong typed argument HOT 1
- Dwmapi DwmIsCompositionEnabled is missing HOT 1
- Init script exception calling "DownloadFile" 404 Not Found HOT 5
- Kernel32.QueryFullProcessImageName broken on Windows 7 HOT 2
- Type of DLGTEMPLATE HOT 1
- User32.SetLayeredWindowAttributes missing HOT 2
- DirectX Video Acceleration HOT 1
- Missing user32 DeleteMenu HOT 5
- Missing user32 RegisterHotKey HOT 2
- Add mpr.dll HOT 1
- Add support for mssign32.dll HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pinvoke.