unsafe

P/Invoke from C to C# without knowing size of array

眉间皱痕 提交于 2019-12-22 17:37:26
问题 Right know in my code I have structure declared as like this, with fixed this 16, know at compile time. struct CONSOLE_SCREEN_BUFFER_INFOEX { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public int ColorTable[]; } but what I need is to be able to have this structure: struct CONSOLE_SCREEN_BUFFER_INFOEX { int arraySize; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0)] public int ColorTable[]; } get the arraySize from C function response, initialize ColorTable array with proper

Why does the 'stackalloc' keyword not work with properties?

狂风中的少年 提交于 2019-12-22 09:48:28
问题 I was writing some unsafe code recently in C# and noticed this produces a syntax error: public unsafe class UnsafeByteStream { public UnsafeByteStream(int capacity) { this.Buffer = stackalloc byte[capacity]; } public byte* Buffer { get; } } The result of this is: "Invalid expression term 'stackalloc' / ; expected / } expected" . However, when I assign this first to a local field, like so: public UnsafeByteStream(int capacity) { byte* buffer = stackalloc byte[capacity]; this.Buffer = buffer; }

What is the actual function of the C# project setting “Allow unsafe code”

情到浓时终转凉″ 提交于 2019-12-22 05:51:16
问题 I was wondering if the C# project setting "Allow unsafe code" applies only to unsafe C# code in the project itself, or is it necessary to set this option when linking in a native C++ DLL? What about linking in a managed DLL that itself links to a native DLL? What does this option really do, under the hood? 回答1: It has to do with the "unsafe" keyword in C#. "unsafe" turns off all the checks that would normally happen and allow you to directly access the memory. it doesn't refer to calling

What is the actual function of the C# project setting “Allow unsafe code”

白昼怎懂夜的黑 提交于 2019-12-22 05:51:07
问题 I was wondering if the C# project setting "Allow unsafe code" applies only to unsafe C# code in the project itself, or is it necessary to set this option when linking in a native C++ DLL? What about linking in a managed DLL that itself links to a native DLL? What does this option really do, under the hood? 回答1: It has to do with the "unsafe" keyword in C#. "unsafe" turns off all the checks that would normally happen and allow you to directly access the memory. it doesn't refer to calling

Why is my unsafe code block slower than my safe code?

99封情书 提交于 2019-12-22 04:56:06
问题 I am attempting to write some code that will expediently process video frames. I am receiving the frames as a System.Windows.Media.Imaging.WriteableBitmap . For testing purposes, I am just applying a simple threshold filter that will process a BGRA format image and assign each pixel to either be black or white based on the average of the BGR pixels. Here is my "Safe" version: public static void ApplyFilter(WriteableBitmap Bitmap, byte Threshold) { // Let's just make this work for this format

What are the implications of using unsafe code

独自空忆成欢 提交于 2019-12-22 01:24:24
问题 Aside from the fact that the code itself can access memory directly. What are the other implications of using the "/unsafe" compiler flag and the "fixed" keyword? Are there any knock on effects related to code signing and deployment of my .exe (my app is desktop only)? (This isn't about whether or not I should be doing this, the why is covered in my question here) 回答1: Unsafe code is not verifiable, so you have to be aware of that. In a Full Trust environment, that's not a big deal, but if

Arithmetic operation resulted in an overflow in unsafe C#

馋奶兔 提交于 2019-12-21 18:05:35
问题 Background We've been using some code copied verbatim from Joe Duffy's "Concurrent Programming on Windows" (page 149) in production for over a year. The code (below) is used in our Asp.Net web application to probe if there's enough stack space. Our site allows users to script out their own web pages and control logic in a simple proprietry scripting language - it's possible for a user to script something nasty and cause a stackoverflow exception, so we use Duffy's code example to stop

Arithmetic operation resulted in an overflow in unsafe C#

孤街浪徒 提交于 2019-12-21 18:03:06
问题 Background We've been using some code copied verbatim from Joe Duffy's "Concurrent Programming on Windows" (page 149) in production for over a year. The code (below) is used in our Asp.Net web application to probe if there's enough stack space. Our site allows users to script out their own web pages and control logic in a simple proprietry scripting language - it's possible for a user to script something nasty and cause a stackoverflow exception, so we use Duffy's code example to stop

ILGenerator: How to use unmanaged pointers? (I get a VerificationException)

↘锁芯ラ 提交于 2019-12-21 16:25:45
问题 I'm making a sound synthesis program in wich the user can create his own sounds doing node-base compositing, creating oscillators, filters, etc... The program compiles the nodes onto an intermediary language wich is then converted onto an MSIL via the ILGenerator and DynamicMethod It works with an array in wich all operations and data are stored, but it will be faster if i was able to use pointers to allow me to do some bit-level operations later PD: Speed is very important!! I noticed that

Is it safe and defined behavior to transmute between a T and an UnsafeCell<T>?

妖精的绣舞 提交于 2019-12-21 09:25:19
问题 A recent question was looking for the ability to construct self-referential structures. In discussing possible answers for the question, one potential answer involved using an UnsafeCell for interior mutability and then "discarding" the mutability through a transmute. Here's a small example of such an idea in action. I'm not deeply interested in the example itself, but it's just enough complication to require a bigger hammer like transmute as opposed to just using UnsafeCell::new and/or