unsafe

c#: generically convert unmanaged array to managed list

I am dealing with a set of native functions that return data through dynamically-allocated arrays. The functions take a reference pointer as input, then point it to the resulting array. For example: typedef struct result { //..Some Members..// } int extern WINAPI getInfo(result**); After the call, 'result' points to a null-termi...

C#: convert generic pointer to array

I want to convert a byte* to a byte[], but I also want to have a reusable function to do this: public unsafe static T[] Create<T>(T* ptr, int length) { T[] array = new T[length]; for (int i = 0; i < length; i++) array[i] = ptr[i]; return array; } Unfortunately I get a compiler error because T might be a ".NET man...

BigEndianBitConverter in Silverlight?

I'm trying to use the MiscUtil.Conversion utility in Silverlight. http://www.yoda.arachsys.com/csharp/miscutil/ When I try to compile it, I get an error saying Silverlight's BitConverter class does not have these two methods: DoubleToInt64Bits Int64BitsToDouble Well, I opened up Reflector and found them in mscorlib: public unsafe lon...

Can this function be any safer ? Looking for tips and your thoughts !!!

Hi everyone, this is somewhat of an odd question. I wrote a C function. Its 'like' strchr / strrchr. It's supposed to look for a character in a c-string, but going backwards, and return a pointer to it. As c strings are not "null initiated", it also takes a third parameter 'count', indicating the number of chars it should look backwards...

C#: Retrieving and using an IntPtr* through reflection

I'm currently working on some code which reflects over structures that are marshaled back from calls into a native dll. Some of the structs contain IntPtr* fields that point to null-terminated arrays of pointers. These fields require special processing. When reflecting over the structs, I can recognize these fields because they are ma...

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

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...

How to convert fixed byte/char[100] to managed char[] in C#?

What's the best way to convert a fixed byte or char[100] to a managed char[] in C#? I ended up having to use pointer arithmetic and I'm wondering if there is an easier way -- something like a memcpy or another way? using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text;...

Faster (unsafe) BinaryReader in .NET

I came across a situation where I have a pretty big file that I need to read binary data from. Consequently, I realized that the default BinaryReader implementation in .NET is pretty slow. Upon looking at it with Reflector I came across this: public virtual int ReadInt32() { if (this.m_isMemoryStream) { MemoryStream str...

.NET Micro Framework Unsafe code

Does .NET Micro Framework support unsafe code? In other words, can I use pointers in my code for .NET Micro Framework? ...

Generic BitConverter-like method?

Hello, I've recently encountered a situation where I need to create a generic method to read a datatype out of a byte array. I've created the following class: public class DataStream { public int Offset { get; set; } public byte[] Data { get; set; } public T Read<T>() where T : struct { unsafe { ...

sun.misc.Unsafe: How to get the Bytes from an address

Hi! I have heared there is a way to read a value from the memory (as long as the memory is control by the JVM). But how do i get the bytes from the address 8E5203 for example? There is a method called getBytes(long). Can I use this? Thanks a lot! Pete ...

Structure Generics in C#

Hi Please, help me with this problem: I Try define a structure like this: unsafe struct sNodo<T> { public T info; public sNodo<T>* sIzq;} but i get this error: Cannot take the address of, get the size of, or declare a pointer to a managed type sNodo, how can I fix it? I'm trying to create a stack "generic" usi...

Reading from an unmanaged stream - unsafe code, IntPtr

The following is exposed in the Firefox (Gecko) 3.5 code: [Guid("fa9c7f6c-61b3-11d4-9877-00c04fa0cf4a"), ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface nsIInputStream { void Close(); int Available(); int Read(IntPtr aBuf, uint aCount); int ReadSegments(IntPtr aWriter, IntPtr aClosure, uint aCount); bool I...

Explain this C# code: byte* p = (byte*) (void*) Scan0;

I found the code from the net in which i cant understand this line:- byte* p = (byte*)(void*)Scan0; There Scan0 is System.IntPtr. It is code of C#.Net. Plz Explain the above line. The complete code is given below. this is code to convert a image in grayscale. public static Image GrayScale(Bitmap b) { BitmapData bmData ...

Can a WPF Browser Application run unsafe code?

I am writing some software that plots out fractals, and lets the user explore them interactively. I currently have my code in a windowed wpf app. I would like get into a browser hosted wpf app, so that I could display them on my website. The problem is that the code utilizes some "unsafe" code to do the rendering. I am using BitmapDa...

To call a method that requires IntPtr, is it better to use /unsafe, or Marshal.AllocHGlobal?

I have a class that will have a few instances persistent throughout the duration of the application. These objects will each need to call a dll method that appends data from an existing float[] buffer, and passes the full dataset to a DLL method that accepts an IntPtr (float array), several times per second. Is it better to do it as un...

Does "fixed" really guarantee anything when passing pointers (ie int[]) to DLLs?

I tried searching for this but haven't found anything, however when passing an int[] into a native DLL function as a pointer, isn't there still the danger that the DLL could maintain a reference to the pointer, and then try to access it again after the "fixed" block has terminated? Wouldn't this cause memory access errors if the GC has ...

C#: Using pointer types as fields?

In C#, it's possible to declare a struct (or class) that has a pointer type member, like this: unsafe struct Node { public Node* NextNode; } Is it ever safe (err.. ignore for a moment that ironic little unsafe flag..) to use this construction? I mean for longterm storage on the heap. From what I understand, the GC is free to move th...

gdi32.dll + unsafe code on 32+64Bit Windows

Which target cpu should I use to compile a .NET 3.5 app (developed on a 32 Bit System) that contains an assembly which uses [DllImport("gdi32.dll")] static extern IntPtr CopyEnhMetaFile(IntPtr hemfSrc, IntPtr hNULL); and which also has the option checked: "Allow unsafe code" because of the following code: ((byte*) dst.Scan0.ToPoin...

Unsafe code and fixed statements with StringBuilder

I was wondering about how passing a String or a StringBuilder to a C function which output a string by parameter. I've found a great answer in http://stackoverflow.com/questions/1687558/calling-unmanaged-function-from-c-should-i-pass-stringbuilder-or-use-unsafe-cod But I have a doubt. Anyone can explain to me why the garbage collector ...