Dienstag, 29. Mai 2012

Code Quality mit Visual Studio und TFS

Besser spät als nie, hier die Slides und Beispiele zu meinem Vortrag "Steigerung der Codequalität mit Visual Studio und TFS".

Bei Fragen und Anregungen gerne melden.

Viel Spaß und viel Erfolg beim clean coden :-)

Montag, 21. Mai 2012

Limits of COM Interop in Silverlight 5?

You can read a whole lot of posts about the P/Invoke capabilities of Silverlight 5. For some weired reasons I needet to initiatie an OLE Drag and Drop from Silverlight to Visual Basic 6.

I thought to use P/Invoke to call the Ole32.dll DoDragDrop Method, which is used under the hood by equally named WinForms function. It has the following signature:

[DllImport("ole32.dll")]
static extern int DoDragDrop(IDataObject pDataObject, IDropSource pDropSource,
   int dwOKEffect, int[] pdwEffect);

Even though there is a IDataObject Interface in Silverlight it is not the matching one for this COM call. So I decided to marshal it by hand, as well as the IDropSource interface. This is were the trouble starts.

The IDataObject interface looks like this (at least this is what I think is right):

  [ComImport]
  [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  [Guid("0000010E-0000-0000-C000-000000000046")]
  public interface IDataObject
  {
    void GetData([In] ref FORMATETC format, out STGMEDIUM medium);
    void GetDataHere([In] ref FORMATETC format, ref STGMEDIUM medium);
    [PreserveSig]
    int QueryGetData([In] ref FORMATETC format);
    [PreserveSig]
    int GetCanonicalFormatEtc([In] ref FORMATETC formatIn, out FORMATETC formatOut);
    void SetData([In] ref FORMATETC formatIn, [In] ref STGMEDIUM medium,
      bool release);

    IEnumFORMATETC EnumFormatEtc(DATADIR direction);
    [PreserveSig]
    int DAdvise([In] ref FORMATETC pFormatetc, ADVF advf, IAdviseSink adviseSink, out int connection);
    void DUnadvise(int connection);
    [PreserveSig]
    int EnumDAdvise(out IEnumSTATDATA enumAdvise);
  }

As you can see, there are lots of other types and interfaces in there. All in all there are nine further interfaces, six structs and six enumerations needed to merely define this interface. I did all of it, but when I try to run the code I get the following error:



The error message is "Invalid managed / unmanaged type combination" - which is refered to on stack overflow with the solution that the struct layout must be set to sequential. I felt that this was not my problem but I tried it - with no luck.

What makes me wonder is the second part of the error message. It tells us that marshalling from and to COM interface pointer is not supported. As far as I can see this is a limitation of P/Invoke in Silverlight - but I can find no reference elsewehere on the net. All P/Invoke samples call rather simple functions that accept only primitive data types.

Anyway I have uploaded the whole source code so anyone who feels lucky today can give it a try...