dotnetbrowser – Random IT Utensils https://blog.adamfurmanek.pl IT, operating systems, maths, and more. Wed, 01 Jan 2020 16:02:12 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 DotNetBrowser and invalid external method call https://blog.adamfurmanek.pl/2020/06/13/dotnetbrowser-and-invalid-external-method-call/ https://blog.adamfurmanek.pl/2020/06/13/dotnetbrowser-and-invalid-external-method-call/#respond Sat, 13 Jun 2020 08:00:41 +0000 https://blog.adamfurmanek.pl/?p=3346 Continue reading DotNetBrowser and invalid external method call]]> Recently I was debugging the issue with DotNetBrowser and external call from JavaScript. I had the following:

FleepBrowser.Browser.DocumentLoadedInMainFrameEvent += delegate(object sender, DotNetBrowser.Events.LoadEventArgs e)
{
	FleepBrowser.Browser.ExecuteJavaScriptAndReturnValue("window").AsObject().SetProperty("external", new ExternalCodeHandler
	{
		NewMessagesCountCallback = delegate(int count, int allCount)
		{
			// ...
		}
	});
}

So I configure the browser and then set external object to handle callback. The callback is a method accepting two integers. It was called using this JS code:

window.external.NewMessagesCount(conversations.not(".muted").length, conversations.length);

It worked correctly on multiple machines for over a year. One day I ran the application on new machine and it stopped working. I added handler:

try{
	window.external.NewMessagesCount(conversations.not(".muted").length, conversations.length);
}catch(e){
	alert(e);
}

It showed dialog with mscorlib: Input string was not in a correct format. C# handler wasn’t called at all.

I attached dnSpy and it showed exception thrown in mscorlib Number.cs.

Okay, so what is wrong here? Notice that C# handler expects integers but JS doesn’t have them. So it passes numbers as doubles. It worked on one machine and didn’t work on the other so it immediately suggested there was something wrong with locales. Indeed, the former had English locale and the double was passed as 1.00000. The latter was running with Polish locale so it expected the coma and hence failed.

I don’t know exactly why it didn’t work but it wasn’t hard to fix it. First, change the C# handler to accept strings. Second, change JS code to pass the argument as window.external.NewMessagesCount("" + conversations.not(".muted").length, "" + conversations.length);

]]>
https://blog.adamfurmanek.pl/2020/06/13/dotnetbrowser-and-invalid-external-method-call/feed/ 0