Not that C# is all bad, mind you. It's pretty decent when it comes to UI and the day-to-day meanderings and wanderings of what needs to happen to keep a program running. Perhaps its nicest feature is the ease with which I can attach an outside dll and use that, because now that I need to make sure I have some serious horsepower, C# is stepping out of the way.
One thing that irritates me: how hard it is to debug unmanaged C++ from inside a managed C# environment. But mmr, I hear you cry, the whole freakin' point of C# is that everything can be managed for you, and the IDE will hold your hand. But sometimes, I don't want my hand held, I want to run free. Free as the wind blows, free as the grass grows.
To attach a dll, just do this:
using System.Runtime.InteropServices;
[DllImport("mydll.dll")]
public static extern void MyFunc(int inImagePtr, int outImagePtr, int inYSize, int inXSize);
private unsafe ushort[] UseMyFunc(ushort[] inChannelData, int inRows, int inColumns) {
ushort[] theOutData = new ushort[inChannelData.Length];
fixed (ushort* inBufferPtr = &inChannelData[0]) {
fixed (ushort* outBufferPtr = &theOutData[0]) {
MyFunc((int)inBufferPtr, (int)outBufferPtr, inRows, inColumns);
}
}
return theOutData;
}
}
Essentially, you need to have a function exposed by the dll, and then you can use that function. So you get the speed of C++ inside the function, but the handiness of C# outside the function. Not a bad combination.

No comments:
Post a Comment