Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TaskDialog from WinAPICodePack does not work on .NET 4.0

I am writing an application and wanted to use the TaskDialogIndirect function - however I did not want to write a huge amount of P/Invoke stuff etc. so I have included the WinAPICodePack. There is one problem though! When I create a control for the TaskDialog and add it to the dialog everything works fine. However running the Show methods results in an OverflowException in mscorlib. I tracked this down to some pointer handling and marshalling.

Experimenting with the code had the result, that I found out, that the DLL MUST be compiled for .NET 3.5 and the including app TOO. Having my app on .NET 4.0 invokes this error... any idea about a workaround - or can you even reproduce this error?

Another issue is that I can set the the Icon property to whatevery I want, but the icon is not shown at all. I have to setup an Opened event which dynamically sets the icon...

Information: Running Visual Studio 2012 RTM on Windows 8 Pro x64 RTM. Application is a standard WPF app.

Sample code:

TaskDialog td = new TaskDialog();
td.Cancelable = true;
td.Caption = "Caption";
td.InstructionText = "Instructions";
td.Text = "Text";

TaskDialogCommandLink buttonElevation =
    new TaskDialogCommandLink("elevation", "Elevation Required Sample");
buttonElevation.UseElevationIcon = true;

td.Controls.Add(buttonElevation);
td.Show(); // OverflowException occurs here!
like image 349
Christian Ivicevic Avatar asked Aug 21 '12 17:08

Christian Ivicevic


People also ask

What is the task Dialog Project?

This project aims to provide a complete .NET implementation (C#) of the Task Dialog with all the features that are also available in the native APIs, with all the marshalling and memory management done under the hood. The project targets .NET Framework 4.7.2 and .NET Standard 2.0.

What happened to the Microsoft API code pack?

Only a half year ago it was promoted on channel9 as Something that should be in everyones pack -- today the Microsoft Archive is "retired" and totally keeps quiet that the API code pack ever existed. Show activity on this post.

What is previewlostkeyboardfocus not detectable via API analysis?

Not detectable via API analysis. Beginning in the .NET Framework 4.5, calling MessageBox.Show from a PreviewLostKeyboardFocus handler will cause the handler to re-fire when the message box is closed, potentially resulting in an infinite loop of message boxes. It may be avoided by calling MessageBox.Show instead of MessageBox.Show.

What is gchandle in task dialog callback?

For the Task Dialog callback, a static delegate is used to avoid the overhead of creating native functions during runtime for each new Task Dialog instance. A GCHandle is used in the callback to map the supplied reference data back to the actual Task Dialog instance.


1 Answers

I fixed this issues which was basically a 32bit/64bit error. In the NativeTaskDialog.cs file there is one line critical, it's in the function

IntPtr AllocateAndMarshalButtons(
    TaskDialogNativeMethods.TaskDialogButton[] structs)

You need to locate the following line

 currentPtr = (IntPtr)((int)currentPtr + Marshal.SizeOf(button));

and replace it with

 currentPtr = (IntPtr)(currentPtr.ToInt64() + Marshal.SizeOf(button));
like image 94
Christian Ivicevic Avatar answered Sep 29 '22 01:09

Christian Ivicevic