unsafe

C# Child Process from Legacy C++ App Windowing Problems...

We have a c++ legacy application and have been extending it with c# applets that are invoked using COM from the parent c++ app. They bring up windows that are not modal. Moreover, I think these .NET windows are not proper children of the c++ application, since EnumChildWindows misses them, and EnumWindows finds them. One child-like beh...

How to read string from pointer to buffer in C#

Hi, How can I read the error string in C# from this C++ dll call? // // PARAMETERS: // objptr // Pointer to class instance. // // pBuffer // Pointer to buffer receiving NULL terminated error message string. // If this value is zero, the function returns the required buffer size, in bytes, // and makes no use of the pB...

How can I display a pointer address in C#?

I've not done any pointers since I've been programming in C# - and my C++ days were long ago. I thought I should refresh my knowledge and was just playing around with them because of another question on here. I understand them all okay, but I can't figure out how to write the pointer's address to the console... char c = 'c'; char d = ...

What would happen in an thread unsafe .NET queue object?

I have a .NET queue object. The producer thread do the Enqueue operation, the data enqueued in the queue is a byte[] array, while the other consumer thread do the Dequeue operation on the same queue object. I use locks to handle concurrency. My code seems to work fine all the time, but yesterday, weird things happened. The data I got fr...

Nested calls can lead to uninitialized arguments in C?

Is len correctly initialized and safe in set_array call? void object_copy (Object* self, Object* obj) { int len; object_set_array (self, object_get_array (obj, &len), len); } If not, what would you recommend? ...

Marshal.PtrToStringUni() vs new String() ?

Suppose i have a pointer of type char* to unicode string, and i know the length: char* _unmanagedStr; int _unmanagedStrLength; and i have 2 ways to convert it to .NET string: Marshal.PtrToStringUni((IntPtr)_unmanagedStr, _unmanagedStrLength); and new string(_unmanagedStr, 0, _unmanagedStrLength); In my tests, both calls gives me...

Why you cannot use an unsafe keyword in an iterator context?

In looking at this question which Jon did a fine job in answering... 'How to read a text file reversly with iterator'. And there was a similar question in which I answered using pointers hocus pocus..'.net is there a way to read a text file from bottom to top' before it got closed.... Now I did set out to try solve this using pointers, ...

Should I mingle my safe code with my unsafe code?

I'm working on a project that uses a bunch of WIN32 API calls and requires some unsafe code. From a best practices standpoint should I isolate this code in its own DLL compiled with /unsafe switch while keeping my main application safe? To put it another way. Is there any reason not to compile a project with the /unsafe switch? Are ther...

Can I use a C# dll with unsafe code in VB.NET?

There is an FastBitmap class for C#, that lets you acces and modify pixel information of am bitmap. I have already used it in some C# project, but I need it now in VB.NET. The problem is that that class uses unsafe code, that is not supported in VB.NET. The question is. Can I compile the FastBitmap class in a dll and use it in VB.NET? ...

Copy Small Bitmaps on to Large Bitmap with Transparency Blend: What is faster than graphics.DrawImage(smallBitmap, x , y) ?

I have identified this call as a bottleneck in a high pressure function. graphics.DrawImage(smallBitmap, x , y); Is there a faster way to blend small semi transparent bitmaps into a larger semi transparent one? Example Usage: XY[] locations = GetLocs(); Bitmap[] bitmaps = GetBmps(); //small images sizes vary approx 30px x 30px...

C#. How to pass message from unsafe callback to managed code?

Is there a simple example of how to pass messages from unsafe callback to managed code? I have a proprietary dll which receives some messages packed in structs and all is coming to a callback function. The example of usage is as follows but it calls unsafe code too. I want to pass the messages into my application which is all managed c...

C# memory management: unsafe keyword and pointers

What are the consequences (positive/negative) of using the unsafe keyword in C# to use pointers? For example, what becomes of garbage collection, what are the performance gains/losses, what are the performance gains/losses compared to other languages manual memory management, what are the dangers, in which situation is it really justifia...

AccessViolationException, attempted to read or write protected memory

I'm using a dll that contains unmanaged code for interacting with specific hardware, and I'm trying to use it from C#, but I keep getting an AccessViolationException. What's causing it, and how can I fix it? namespace FingerPrint { public unsafe partial class Form1 : Form { [DllImport("MyDll.dll")] public static ex...

Converting C# void* to byte[]

In C#, I need to write T[] to a stream, ideally without any additional buffers. I have a dynamic code that converts T[] (where T is a no-objects struct) to a void* and fixes it in memory, and that works great. When the stream was a file, I could use native Windows API to pass the void * directly, but now I need to write to a generic Stre...

C# Implementing a custom stream writer-esque class

How would I go about writing my own stream manipulator class? Basically what I'm trying to wrap my head around is storing the reference to the underlying stream in the writer. For example, when writing to a memory stream using a StreamWriter, when a Write() is made, the underlying memory stream is written to. Can I store the referenc...

C# huge size 2-dim arrays

I need to declare square matrices in C# WinForms with more than 20000 items in a row. I read about 2GB .Net object size limit in 32bit and also the same case in 64bit OS. So as I understood the single answer - is using unsafe code or separate library built withing C++ compiler. The problem for me is worth because ushort[20000,20000] is...

Retrieving a pixel alpha value for a UIImage (MonoTouch)

This question is a duplicate of 1042830, but MonoTouch-specific. Is there a way that's safer than allocating an IntPtr, drawing into it using CGBitmapContext and then reading bytes at the appropriate offset? ...

Create a subarray reference in C# (using unsafe ?)

Hello there, I'm refactoring a library we currently use, and I'm faced with the following problem. We used to have the following stuff : class Blah { float[][] data; public float[] GetDataReference(int index) { return data[index]; } } For various reasons, I have replaced this jagged array version with a 1 dim...

C# Generic Arrays and math operations on it

Hello, I'm currently involved in a project where I have very large image volumes. This volumes have to processed very fast (adding, subtracting, thresholding, and so on). Additionally most of the volume are so large that they event don't fit into the memory of the system. For that reason I have created an abstract volume class (VoxelVol...

How do I use unsafe values in an enum?

I need to use this enum in my C# application, but it won't let me use these values. When I specify the type as uint I can use the -1 value, and when I specify int I can't use the last 2 values. Is there a way to use the unchecked keyword here to allow me to define all of these values? These values are coming from an external source, so I...