unsafe

How to get char** using C#? [duplicate]

孤者浪人 提交于 2019-11-30 12:06:58
This question already has an answer here: Marshal an array of strings from C# to C code using p/invoke 3 answers C# call C++ DLL passing pointer-to-pointer argument 3 answers I need to pass an argument to an unsafe DllImported function in the form of: [DllImport("third_party.dll")] private static extern unsafe int start(int argc, char** argv); I'm assuming it's an array of strings. However when I try to do the following, I get 'Cannot convert from string[] to char**' error. How do I go about getting this to work? Thanks. string[] argv = new string[] { }; start(0, argv); EDIT 1: The question

Unsafe code in C#

戏子无情 提交于 2019-11-30 11:39:26
What are the limitations of unsafe code, in C#? For example, can I do virtually arbitrary pointer casts and arithmetic as if I were using C or C++? codekaizen Yes. All bets are off when unsafe is in play. This is the idea behind "unsafe" - that the "safety" of verifiable types is removed, and you can cast from a pointer of one type to a pointer of another type without the runtime keeping you from shooting yourself in the foot , if you so desire - much like C or C++. Here's an example of using different pointer types in C#: fixed (Byte* dstBytes = &currentImage[0]) { var dstBuffer = (Int64*

how to add unsafe keyword in web based asp.net application c#

假装没事ソ 提交于 2019-11-30 06:01:33
问题 Hi how i can use unsafe keyword in web based application for pointers? In windows application we have setting in properties section of project under build tag we can check allow unsafe code checkbox, but in web based application how to allow unsafe code or any other in replacement of unsafe code (Pointer) asp.net c# Thank you. 回答1: If this is a web application you can simply go to the properties of the project and allow unsafe code as you would do in any standard class library. If it is a web

Why is a fixed size buffers (arrays) must be unsafe?

荒凉一梦 提交于 2019-11-30 05:59:44
Let's say I want to have a value type of 7 bytes (or 3 or 777). I can define it like that: public struct Buffer71 { public byte b0; public byte b1; public byte b2; public byte b3; public byte b4; public byte b5; public byte b6; } A simpler way to define it is using a fixed buffer public struct Buffer72 { public unsafe fixed byte bs[7]; } Of course the second definition is simpler. The problem lies with the unsafe keyword that must be provided for fixed buffers. I understand that this is implemented using pointers and hence unsafe. My question is why does it have to be unsafe? Why can't C#

Error lnk2026: module unsafe for safeseh image

ぃ、小莉子 提交于 2019-11-30 05:36:10
I got this error when building a sample visual C++ project. First I downloaded 3 sample projects, all solve the same problem, print out all the prime numbers less than N (you may know these sample projects ?). I built the pure-C project without any problem. But when I tried to build the assembly-based project one, I got this error. Thank you. Try to disable SAFESEH. From spec: /SAFESEH was specified, but a module was not compatible with the safe exception handling feature. In Visual Studio 2012 Express Edition: Right-click on your project -> Properties -> Configuration Properties -> Linker ->

Are ref and out in C# the same a pointers in C++?

笑着哭i 提交于 2019-11-30 03:34:54
问题 I just made a Swap routine in C# like this: static void Swap(ref int x, ref int y) { int temp = x; x = y; y = temp; } It does the same thing that this C++ code does: void swap(int *d1, int *d2) { int temp=*d1; *d1=*d2; *d2=temp; } So are the ref and out keywords like pointers for C# without using unsafe code? 回答1: They're more limited. You can say ++ on a pointer, but not on a ref or out . EDIT Some confusion in the comments, so to be absolutely clear: the point here is to compare with the

C# Get type of fixed field in unsafe struct with reflection

眉间皱痕 提交于 2019-11-30 02:37:51
问题 I'm trying to get the field types of an unsafe struct using some fixed fields. The fixed fields FieldType do not return the actual type. [StructLayout(LayoutKind.Sequential, Pack = 1)] public unsafe struct MyStruct { public UInt32 Field1; public fixed sbyte Field2[10]; public UInt64 Field3; } void Test() { var theStruct = new MyStruct(); string output = ""; foreach (FieldInfo fi in theStruct.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance)) { output += fi.Name + ": " + fi

sun.misc.Unsafe的各种神技Unsafe

会有一股神秘感。 提交于 2019-11-30 01:15:48
1. sun.misc.Unsafe包 Unsafe类在jdk 源码的多个类中用到,这个类的提供了一些绕开JVM的更底层功能,基于它的实现可以提高效率。但是,它是一把双刃剑:正如它的名字所预示的那样,它是Unsafe的,它所分配的内存需要手动free(不被GC回收)。Unsafe类,提供了JNI某些功能的简单替代:确保高效性的同时,使事情变得更简单。 这篇文章主要是以下文章的整理、翻译。 http://mishadoff.com/blog/java-magic-part-4-sun-dot-misc-dot-unsafe/ 1. Unsafe API的大部分方法都是native实现,它由105个方法组成,主要包括以下几类: (1)Info相关。主要返回某些低级别的内存信息:addressSize(), pageSize() (2)Objects相关。主要提供Object和它的域操纵方法:allocateInstance(),objectFieldOffset() (3)Class相关。主要提供Class和它的静态域操纵方法:staticFieldOffset(),defineClass(),defineAnonymousClass(),ensureClassInitialized() (4)Arrays相关。数组操纵方法:arrayBaseOffset()

How to use unsafe code Unity

心已入冬 提交于 2019-11-29 20:55:10
问题 I want to use c++ code in c# for unity using CLR. The program works properly outside of unity, but inside of engine it gives me an error: "cs0227: unsafe code requires the 'unsafe' command line option to be specified" I am really confused, because the project builds successfully in visual studio (without any errors or warnings). I have " allow unsafe " button activated. using UnityEngine; using System.Collections; using System; using System.Collections.Generic; using System.Linq; using System

How to get char** using C#? [duplicate]

耗尽温柔 提交于 2019-11-29 17:51:57
问题 This question already has an answer here: Marshal an array of strings from C# to C code using p/invoke 3 answers C# call C++ DLL passing pointer-to-pointer argument 3 answers I need to pass an argument to an unsafe DllImported function in the form of: [DllImport("third_party.dll")] private static extern unsafe int start(int argc, char** argv); I'm assuming it's an array of strings. However when I try to do the following, I get 'Cannot convert from string[] to char**' error. How do I go about