0

I have a problem with a WPF application. When running it, it breaks with a System.Windows.Markup.XamlParseException giving:

Additional information: 'The invocation of the constructor on type 'myproject_csharp.MainWindow'
that matches the specified binding constraints threw an exception.' Line number '7' and line
position '9'.

At this position I am calling a function out of an external DLL which expects a variable arguments list:

int pos = 0;
res = myfunc_init(ref pos,__arglist(several parameters here));

This function itself is defined as

[DllImportAttribute("my.dll", EntryPoint = "myfunc_init_api", CharSet = CharSet.Ansi, CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static extern uint myfunc_init(ref int var, __arglist);

From what I can see, everything is correct. So where could this exception come from? Any issues with the variable arguments list?

One thing to note: in the IDE the function is marked as erroneous with "No overload for method takes 2 arguments" but then int compiles without errors.

Thanks!

Elmi
  • 5,899
  • 15
  • 72
  • 143
  • Since there is no [MCVE] it is hard to figure out exactly how that exception is thrown... but code shown in the post possibly not supposed to work (assuming you trying to pass array as separate parameters (https://stackoverflow.com/questions/32175822/how-to-p-invoke-arglist-function)... – Alexei Levenkov Dec 12 '19 at 06:38

1 Answers1

0

I would try to call this function outside the constructor. Call it outside the MainWindow in a bootstrapper or trigger it after the constructor or call it in the background like:

in the constructor call:

Task.Run(() => Init());

and then

private static void Init() {
    try {
        int pos = 0;
        res = myfunc_init(ref pos,__arglist(several parameters here));

        //Call OnPropertyChanged() for relevant UI Elements

    } catch(Exception ex) {
        //Do error handling outside the constructor...
    }
}

If you do it like this the XAML will load and than you can handle errors seperately.

Markus
  • 2,184
  • 2
  • 22
  • 32