unsafe

Calling AuditQuerySystemPolicy() (advapi32.dll) from C# returns “The parameter is incorrect”

我的梦境 提交于 2019-12-06 08:05:07
问题 The sequence is like follows: Open a policy handle with LsaOpenPolicy() (not shown) Call LsaQueryInformationPolicy() to get the number of categories; For each category: Call AuditLookupCategoryGuidFromCategoryId() to turn the enum value into a GUID; Call AuditEnumerateSubCategories() to get a list of the GUIDs of all subcategories; Call AuditQuerySystemPolicy() to get the audit policies for the subcategories. All of these work and return expected, sensible values except the last. Calling

Difference between Marshal.SizeOf and sizeof, I just don't get it

一个人想着一个人 提交于 2019-12-06 04:44:39
问题 Until now I have just taken for granted that Marshal.SizeOf is the right way to compute the memory size of a blittable struct on the unmanaged heap (which seems to be the consensus here on SO and almost everywhere else on the web). But after having read some cautions against Marshal.SizeOf (this article after "But there's a problem...") I tried it out and now I am completely confused: public struct TestStruct { public char x; public char y; } class Program { public static unsafe void Main

Usefulness of bool* in C#

大兔子大兔子 提交于 2019-12-06 03:47:56
Can I use bool* in any kind of meaningful way. How would I convert bool* to a byte for instance, or store bool* in a byte My goal is to manage my own memory in a project of mine, the specifics aren't important, just something id like to do. Now I would like to be able to store my own variables, and i happen to need to store a boolean value. How can i, using unsafe and a byte* , store this boolean value, and utilize my space best? Ideally to store 4 bits in the byte. In C# you wouldn't normally use a bool* , which is something you can only use in unsafe code (which brings forth a whole lot of

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

巧了我就是萌 提交于 2019-12-06 03:27:00
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; } Then there is no syntax error produced. Is there a reason for this, or is something up with the

Struct Pointer Initialization in C#

强颜欢笑 提交于 2019-12-06 02:38:16
问题 unsafe public class Temp { public struct Node { Node *left; Node *right; int value; } public Temp() { Node* T=new Node(); T->left=null; T->right=null; T->value=10; } } main() { Temp temp=new Temp(); } It gives error that Object reference not set to instance of an object. How can I do it when I want to make AVL Tree Program(which I have created and tested in C++ but copying in C# gives error) 回答1: Don't try to use pointers in C# like this. If you are porting C++ code that uses pointers as

Where to set unsafe mode in Safari 10 to made java plugin access local file

别等时光非礼了梦想. 提交于 2019-12-06 02:17:58
I just updated my Safari from Safari 9 to safari 10 beta version. My client is a java applet runs from a page. In Safari 9 and before version, it runs OK. But in Safari 10 beta, it fails. I found that safari 10 blocks access to local files from Java applet. So I think it is because the java plugin runs under safe mode. But in Safari ->preferences->security->Plug-in Settings. there is no unsafe mode option to check. there is only three option :Ask,Off,On. So my client can not access local file in safe mode now. My mac system is OS X El Capitan version 10.11.6 and Safari version is version 10.0

Why does modifying a mutable reference's value through a raw pointer not violate Rust's aliasing rules?

自古美人都是妖i 提交于 2019-12-05 23:23:04
问题 I don't have a particularly solid understanding of Rust's aliasing rules (and from what I've heard they're not solidly defined), but I'm having trouble understanding what makes this code example in the std::slice documentation okay. I'll repeat it here: let x = &mut [1, 2, 4]; let x_ptr = x.as_mut_ptr(); unsafe { for i in 0..x.len() { *x_ptr.offset(i as isize) += 2; } } assert_eq!(x, &[3, 4, 6]); The problem I see here is that x , being an &mut reference, can be assumed to be unique by the

How to call unsafe code from ASP.NET xproj

杀马特。学长 韩版系。学妹 提交于 2019-12-05 18:02:40
问题 I created a class library today with the new Class Library (Package) templates in Visual Studio 2015. Apparently it uses a fancy new project format, the ASP.NET xproj, for building the package. While that's fine by me, how do I call unsafe code from the library? I looked into Project > Properties > Build , where the option to toggle unsafe code should be, but all I got was this: So yeah, no such luck. I even tried pasting "<AllowUnsafeBlocks>true</AllowUnsafeBlocks>" manually into the .xproj

How to create an array or a slice from an array unsafe.Pointer in golang?

▼魔方 西西 提交于 2019-12-05 08:42:49
A pointer to an array, let's say: p := uintptr(unsafe.Pointer(&array)) size := 5 I can't access to the variable array , the code above is used to make it more clear. Also, I know the size of the array, but the size is not constant, it changes according to the runtime. Now, I want to initialize a slice or an array with the known pointer, size and of course the data type. I come up with the following code: data := make([]byte, size) stepSize := unsafe.Sizeof(data[0]) for i := 0; i < size; i++ { data[i] = *(*byte)(unsafe.Pointer(p)) p += stepSize } fmt.println(data) but this method does memory

Unsafe code to change length (by mutation!) of a String object?

霸气de小男生 提交于 2019-12-05 06:25:41
As .NET doesn't use C style null s to end a string how can I keep the allocated string but change the length of it by using unsafe code? As I understand .NET using a 20 bytes header for every string, presumably this is where the length of the string is stored, is there anyway to directly modify this length? So .NET will keep the string in memory but when I call .Length it'll return the .Length I want. if this is possible, also it would be interesting to hear all crazy possible side-effects of this UPDATE I'm trying accomplish this without using reflection. From Strings UNDOCUMENTED public