Monday, October 20, 2008

P/Invoke cheat sheet

I’m working a lot with p/invoke, and know how it’s hard to produce correct signature for unmanaged method. So, today I decided to publish basic cheat sheet for methods, parameters and attributes you should use in order to invoke unmanaged methods from managed code without a lot of problems. We start with data type translations. Here the table to understand it.

Data type from unmanaged signature Data type in managed signature
int int

the same with all other simple types such as double, uint, etc or private objects
void* IntPtr
int* ref int

the same with all other simple types such as double, uint, etc or private objects
char** ref IntPtr

later, you should get ascii string by using System.Runtime.InteropServices.Marshal.PtrToStringAnsi() method
wcar_t** ref IntPtr

later, you should get ascii string by using System.Runtime.InteropServices.Marshal.PtrToStringUni() method
const int* ref int
const char* [System.Runtime.InteropServices.In()] [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string
(variable argument) [System.Runtime.InteropServices.In()] [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.AsAny)] object

You can use either System.Runtime.InteropServices.In or System.Runtime.InteropServices.Out attribute to specify how arguments should be used.

Now we done with simple arguments, let’s see what can be done when argument is actually callback or delegate?

Unmanaged definition Managed definition
typedef void (*MyCallback)(int Arg)

[System.Runtime.InteropServices.UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.Cdecl)]delegate void MyCallback(int Arg)
Caller cleans stack argument is used to assure, that we can call varargs type function, usually used by API provider. It is very similar to C# overrides for methods. Also you can use StdCall (this is default), ThisCall – stores this first and pushes other parameters on the stack, FastCall – not very supported :(

 

To call all those methods, we should know managed equivalents of unmanaged types. Here the table. The rule is simple – know how many bytes unmanaged type has and find managed type with the same number of bytes. Other words, you can marshal int into IntPtr too…

Unmanaged type Managed equivalent
bool bool
char sbyte (signed), byte (unsigned)
wchar_t char
double double
float single
int, long (signed) Int32
int, long (unsigned) UInt32
__int64 (signed) Int64
__int64 UInt64
short (signed) Int16
short (unsigned) UInt16
void void

But not only types are problem in managed/unmanaged transitions. Also structures are aligned differently. For this purpose we can use StructLayout attribute. Even if unmanaged classes are sequential and you used correct managed data types, you can find you with problems in Pack. What “pack” is? Pack is actually slot size in bytes for members of your structure. It can be 0, 1, 2, 4, 8, 16, 32, 64, or 128 and depends on the platform and application setting.

Now you can see, that it is not very complicated to create managed signatures when you have header of unmanaged assemblies. So go ahead and ask, if I missed something.

That’s all by now. Have a nice day and be good people.

Monday, October 13, 2008

Set binding, based on trigger

Let’s say, that you want to set binding. However, you want to set it by using some trigger. Wait! Why I need it? Let’s say, that I have some very special object, that actually has no hard-coded properties. All it’s properties are created during the runtime, based on some trigger. In this case, I still want to use binding, however, if the property does not exists, I cannot do it. In this special case (that we’ll probably speak more later), we can use this class – TriggerBinding.

How to do it? How to extend build-in binding and add such functionality? Simple. All we need is to write custom markup extension. So, we’ll create new one

public class TriggerBindingExtension : MarkupExtension

And expose all regular binding properties + three custom – TriggerSource, TriggerSourceName and Trigger itself

public IValueConverter Converter { get; set; }
public object ConverterParameter { get; set; }
public string ElementName { get; set; }
public RelativeSource RelativeSource { get; set; }
public object Source { get; set; }
public string TriggerSourceName { get; set; }
public UIElement TriggerSource { get; set; }
[TypeConverter(typeof(RoutedEventConverter))]
public RoutedEvent Trigger { get; set; }
public bool ValidatesOnDataErrors { get; set; }
public bool ValidatesOnExceptions { get; set; }
public string StringFormat { get; set; }
[TypeConverter(typeof(EnumConverter))]
public BindingMode Mode { get; set; }
[TypeConverter(typeof(PropertyPathConverter))]
public PropertyPath Path { get; set; }
[TypeConverter(typeof(BooleanConverter))]
public bool IsAsync { get; set; }
public string XPath { get; set; }
[TypeConverter(typeof(CultureInfoIetfLanguageTagConverter))]
public CultureInfo ConverterCulture { get; set; }

Next step is to override ProvideValue method of MarkupExtension and create the actual binding. We still do not want to bind it to any property

public override sealed object ProvideValue(IServiceProvider serviceProvider) {
        var _valueProvider = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
        if (_valueProvider != null) {
           var _bindingTarget = _valueProvider.TargetObject as FrameworkElement;
           var _bindingProperty = _valueProvider.TargetProperty as DependencyProperty;
           var _binding = new Binding();
           _binding.Path = Path;
           _binding.XPath = XPath;
           _binding.Mode = Mode;
           _binding.Converter = Converter;
           _binding.ConverterCulture = ConverterCulture;
           _binding.ConverterParameter = ConverterParameter;
           _binding.IsAsync = IsAsync;
           if (ElementName != null) _binding.ElementName = ElementName;
           if (RelativeSource != null) _binding.RelativeSource = RelativeSource;
           if (Source != null) _binding.Source = Source;
           _binding.ValidatesOnDataErrors = ValidatesOnDataErrors;
           _binding.ValidatesOnExceptions = ValidatesOnExceptions;
           _binding.StringFormat = StringFormat;

Now, if we have TriggerSource and Trigger, everything fine, we can set subscribe to the trigger event and wire binding in the handler

private RoutedEventHandler _bindigHandler;

private void _registerHandler(FrameworkElement bindingTarget, DependencyProperty bindingProperty, Binding binding) {
   if (_bindigHandler == default(RoutedEventHandler)) {
      _bindigHandler = (RoutedEventHandler)(delegate {
         var _oldValue = bindingTarget.GetValue(bindingProperty);
         bindingTarget.SetBinding(bindingProperty, binding);
         if (_oldValue != bindingProperty.DefaultMetadata.DefaultValue) {
            bindingTarget.SetValue(bindingProperty, _oldValue);
            BindingOperations.GetBindingExpression(bindingTarget, bindingProperty).UpdateSource();
         }
      });
      TriggerSource = TriggerSource ?? bindingTarget;
      if (Trigger != null && TriggerSource != null) {
         TriggerSource.AddHandler(Trigger, _bindigHandler);
      } else {
         _bindigHandler.Invoke(this, null);
      }
   }
}

However, we also want to be able to seek Logical tree and find trigger source by it’s name. Let’s say this way

<TextBox Name="one" Text="{l:TriggerBinding ElementName=two, Path=Text, Mode=TwoWay, TriggerSourceName=three, Trigger=Button.Click}"/>
<TextBox Name="two"/>
<Button Name="three" Content="Click to trigger binding"/>

What’s the problem? We also have FrameworkElement.FindName method and LogicalTreeHelper.FindLogicalNode. But wait, in our case, when TextBox “one” is initialized, we still have no Button “three”, so we have to wait until the root element of TextBox will be fully loaded and then try to find logical node. For this purpose, we have very useful extended method

public static DependencyObject GetRoot(this DependencyObject current) {
         var _root = current;
         do { _root = LogicalTreeHelper.GetParent(_root); if (_root != null) current = _root; } while (_root != null);
         return current;
      }

So, all we have to do is to subscribe to Loaded event of the root and then wire the trigger for set binding

if (!string.IsNullOrEmpty(TriggerSourceName)) {
   var _root = _bindingTarget.GetRoot() as FrameworkElement;
   _root.Loaded += delegate {
      TriggerSource = LogicalTreeHelper.FindLogicalNode(_root, TriggerSourceName) as UIElement;
      _registerHandler(_bindingTarget, _bindingProperty, _binding);
   };
} else { _registerHandler(_bindingTarget, _bindingProperty, _binding); }

We done. Have a good day and be good people.

BTW, it’s also possible to postpone binding by using timer. Paul Stovell has very good sample of how to perform it.

Source code for this article

Saturday, October 11, 2008

Issues, you reported were fixed

Hi, folks. And thank you for reporting issues with WpfPerf Performance Profiling tool. Now it was fixed, so, download and use new and fixed version.

Keep reporting, as you can see, you have the power to change!

image

Imaginary by Malcolm Dare

Sunday, October 05, 2008

Silverlight Bidi Controls Library RC0 and movement from Beta 2 to RC0

Finally, I got free minute to convert Silverlight BiDi controls from Silverlight beta 2 to RC0 (you can download SL rc0 tools for VS2008 here) and as usual some breaking changes (the full list is here)

  • Calendar and DatePicker moved from System.Windows.Controls.Extended into System.Windows.Controls – Extended namespace is now deprecated.
  • CalendarButton is not inside System.Windows.Controls.Primitives
  • TypeConverter.CanConvertFrom(Type sourceType) was changed and now it has new first parameter ITypeDescriptorContext context
  • TypeConverter.CanConvertFrom(object value) was changed and now it has new first parameter ITypeDescriptorContext context and second parameter System.Globalization.CultureInfo culture
  • TypeConverter.ConvertFromString is not virtual anymore
  • TextDecorationCollectionConverter was removed
  • generic.xaml should be placed into themes directory (as in WPF)
  • VisualTransition.Duration is not VisualTransition.GeneratedDuration
  • ContentPresenter has no HorizontalContentAlignment and VerticalContentAlignment. It has HorizontalAlignment and VerticalAlignment now. Also it has no Background, Padding,TextAlignment,TextDecorations and TextWrapping properties

Those, basically, all changes done in Silverlight RTL support library. So, you can download and use the latest version within Silverlight RC0 version

Have a nice day and be good people.

Saturday, October 04, 2008

Free Microsoft Expression series video training catalogs

MSDN download center just published video catalogs, where you can find sample chapters of Total Training’s video series for Microsoft Expression studio. Worth to download and see. Here the breakdown by products

Also, if you are “in” Office development, it makes sense to take a look in to Data Connectivity Components for Office 2007 System. It can be used by non-Microsoft Office applications to read data from Office system files.

Happy downloading!