예제 #1
0
        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);
        }
예제 #2
0
 static extern void registerApiFunction_([MarshalAs(UnmanagedType.LPStr)] string name, ApiFunc handler);