public static void FreeCommandCallResult(IntPtr p) { CommandCallResult rs = (CommandCallResult)Marshal.PtrToStructure(p, typeof(CommandCallResult)); if (rs.result_text != IntPtr.Zero) {//释放为result_text分配的非托管内存 Marshal.FreeHGlobal(rs.result_text); } Marshal.FreeHGlobal(p);//释放为CommandCallResult结构分配的非托管内存 }
//返回值的是CommandCallResult的指针 public static IntPtr OnCommandCall(ref PlistPackageArgs args) { CommandCallResult rs = default(CommandCallResult); rs.FreeCommandCallResult = FreeCommandCallResult; rs.type = ResultType.RESULT_TEXT;//告诉SiriProxy,返回的是文本,不带任何plist xml标记的文本 string text = "caught the command text:"; text += args.sendText; rs.result_text = Marshal.StringToHGlobalAnsi(text); IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(rs)); Marshal.StructureToPtr(rs, p, true); return(p); }