Thursday, April 16, 2009

MemCpy in C#

Here's an example of implementing a fast equivalent of MemCpy in C# using DynamicMethods.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Text;

namespace Test
{
public unsafe class Program
{
public delegate void MemCpyFunction(void *des, void *src, uint bytes);

private static readonly MemCpyFunction MemCpy;

static Program()
{
var dynamicMethod = new DynamicMethod
(
"MemCpy",
typeof(void),
new [] { typeof(void *), typeof(void *), typeof(uint) },
typeof(Program)
);

var ilGenerator = dynamicMethod.GetILGenerator();

ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldarg_1);
ilGenerator.Emit(OpCodes.Ldarg_2);

ilGenerator.Emit(OpCodes.Cpblk);
ilGenerator.Emit(OpCodes.Ret);

MemCpy = (MemCpyFunction)dynamicMethod
.CreateDelegate(typeof(MemCpyFunction));
}

static void Main(string[] args)
{
var point1 = new Point
{
X = 10,
Y = 20
};

var point2 = new Point();

MemCpy(&point2, &point1, (uint)sizeof(Point));
}
}
}

3 Comments:

At August 1, 2010 at 11:37 PM , Blogger Unknown said...

I need the software sources VNCX.dll. I am wanting to create a monitoring application for an NGO created in Brazil can help me. all changes will mention your name and the product will have its copyright preserved.

I await your response

Rodrigues
+55 61 81885410

 
At August 1, 2010 at 11:56 PM , Blogger Unknown said...

please I need the source code of vncx.dlll or any version to run in windows xp, ou seven.Please

Rodrigues
Brasil
email:junerod@hotmil.com

 
At March 16, 2011 at 5:27 PM , Blogger Brent said...

Awesome and thank you.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home