static void registerApiFunctionJson(string name, Func <string, string> handler) { ApiFunc func = (strPtr) => { try { return(Marshal.StringToHGlobalAnsi(handler(Marshal.PtrToStringAnsi(strPtr)))); } catch (ArgumentOutOfRangeException) { ExecuteJavascript("throw new Error('" + name + ": Too few arguments');"); return(Marshal.StringToHGlobalAnsi("null")); } catch (FormatException) { ExecuteJavascript("throw new Error('" + name + ": Wrong argument type');"); return(Marshal.StringToHGlobalAnsi("null")); } catch (Exception e) { ExecuteJavascript("throw new Error(\"" + name + ": " + e.Message.Replace("\"", "\\\"").Replace("\n", " ") + "\");"); return(Marshal.StringToHGlobalAnsi("null")); } }; apiFunctions.Add(func); registerApiFunction_(name, func); }
static extern void registerApiFunction_([MarshalAs(UnmanagedType.LPStr)] string name, ApiFunc handler);