Monday, August 25, 2008

Marshaling an array of Type to C#

Today I needed to Marshal an array of structs from a dllimport'ed function. The solution I used involved taking the pointer to the unmanaged array, and looping through it for each item. I ended up with two versions; one for value types and one for reference types.

Source:




   1:              public static T[] PtrToStructureArray<T>(IntPtr arrayPtr, int length) where T : struct

   2:              {

   3:                  T[] array = new T[length];

   4:                  if (length > 0)

   5:                  {

   6:                      int elementSize = Marshal.SizeOf(typeof(T));

   7:                      Int64 addr = arrayPtr.ToInt64();

   8:   

   9:                      for (int i = 0; i < array.Length; i++)

  10:                      {

  11:                          Marshal.PtrToStructure((IntPtr)(addr + elementSize * i), array[i]);

  12:                      }

  13:                  }

  14:                  return array;

  15:              }

  16:              public static T[] PtrToClassArray<T>(IntPtr arrayPtr, int length) where T: new()

  17:              {

  18:                  T[] array = new T[length];

  19:                  if (length > 0)

  20:                  {

  21:                      int elementSize = Marshal.SizeOf(typeof(T));

  22:                      Int64 addr = arrayPtr.ToInt64();

  23:   

  24:                      for (int i = 0; i < array.Length; i++)

  25:                      {

  26:                          array[i] = new T();

  27:                          Marshal.PtrToStructure((IntPtr)(addr + elementSize * i), array[i]);

  28:                      }

  29:                  }

  30:                  return array;

  31:              }

Tuesday, August 19, 2008

P/Invoke Tools

Sasha Goldstein has a post from Jan 2008 that details 3 helpful resources for P/Invoking win32 dll's. 

Thursday, July 3, 2008

Converting SVG to Xaml

It seemed to me that it makes no sense to be required to convert svg files in tools like that from the folk at http://www.wpf-graphics.com/ in order to import them directly into a wpf App. Now they do have a library that you can call but it isn't exactly open. I say the same comments from Jon Galloway from almost a year ago. Fortunately one of the commenter's on Jon's post provided a link to a xslt file that would convert between the two. From there is is a simple xslt transform away. I did end up with one catch. the xslt that was posted used another external file [color.xml] to do color mapping.




     
static System.Windows.UIElement LoadFromSVG(string fileUri)
{
// Create a transform
System.Xml.Xsl.XslCompiledTransform processor
= new System.Xml.Xsl.XslCompiledTransform();

// Creates a resolver
System.Xml.XmlResolver resolver
= new System.Xml.XmlUrlResolver();

// load the transform into the processor
System.Xml.XmlReader reader
= new System.Xml.XmlTextReader("svg2xaml.xsl");
processor.Load(
reader,
new System.Xml.Xsl.XsltSettings(true, true),
resolver);

// get the source file
System.Xml.XmlReader sourceXml
= new System.Xml.XmlTextReader(fileUri);

// get the results of the transform
StringWriter xamlFile = new StringWriter();
System.Xml.Xsl.XsltSettings.Default.EnableDocumentFunction
= true;
processor.Transform(
sourceXml,
null,
new System.Xml.XmlTextWriter(xamlFile),
resolver);

return System.Windows.Markup.XamlReader.Load(
new XmlTextReader(
new StringReader(xamlFile.ToString())))
as
System.Windows.UIElement;
}

Monday, June 23, 2008

Of Thoughts and Code

There is only so much a person can do before they are forced to actually go out and create something new in order to improve upon themselves.  Currently I work as a software engineer working on technology, learning new tech, and trying, generally, to keep up with what will come next down the pipeline of innovation.  Having been influenced by the writings of other programmers on journeys of there own, I have decided to try and document my own journey.