TaskDialog from WinAPICodePack does not work on .NET 4.0
Asked Answered
D

1

6

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!
Dreiser answered 21/8, 2012 at 17:6 Comment(0)
D
9

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));
Dreiser answered 21/8, 2012 at 19:35 Comment(1)
We experiencing this exception only in 4.5.1. It works in 4.0 for us but started to crash after upgrade то 4.5.1. I've downloaded latest nuget package, it contains changes in exactly this method with exactly same code, but it didn't helped. First time dialog appeared with Chinese hieroglyphs, second time it crashed with AccessViolation from this method.Inwards

© 2022 - 2024 — McMap. All rights reserved.