public static IntPtr libvlc_new(string[] arguments) { PointerToArrayOfPointerHelper argv = new PointerToArrayOfPointerHelper(); argv.pointers = new IntPtr[11]; for (int i = 0; i < arguments.Length; i++) { argv.pointers[i] = Marshal.StringToHGlobalAnsi(arguments[i]); } IntPtr argvPtr = IntPtr.Zero; try { int size = Marshal.SizeOf(typeof(PointerToArrayOfPointerHelper)); argvPtr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(argv, argvPtr, false); return(libvlc_new(arguments.Length, argvPtr)); } finally { for (int i = 0; i < arguments.Length + 1; i++) { if (argv.pointers[i] != IntPtr.Zero) { Marshal.FreeHGlobal(argv.pointers[i]); } } if (argvPtr != IntPtr.Zero) { Marshal.FreeHGlobal(argvPtr); } } }
//将string []转换为IntPtr public static IntPtr StrToIntPtr(string[] args) { try { IntPtr ip_args = IntPtr.Zero; PointerToArrayOfPointerHelper argv = new PointerToArrayOfPointerHelper(); argv.pointers = new IntPtr[11]; for (int i = 0; i < args.Length; i++) { argv.pointers[i] = Marshal.StringToHGlobalAnsi(args[i]); } int size = Marshal.SizeOf(typeof(PointerToArrayOfPointerHelper)); ip_args = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(argv, ip_args, false); return(ip_args); } catch (Exception) { return(IntPtr.Zero); } }
/// <summary> /// 传入播放参数 /// </summary> /// <param name="arguments"></param> /// <returns></returns> public static IntPtr libvlc_new(string[] arguments) { PointerToArrayOfPointerHelper argv = new PointerToArrayOfPointerHelper(); argv.pointers = new IntPtr[11]; for (int i = 0; i < arguments.Length; i++) { argv.pointers[i] = Marshal.StringToHGlobalAnsi(arguments[i]); //将托管 System.String 中的内容复制到非托管内存,并在复制时转换为 ANSI 格式。 } IntPtr argvPtr = IntPtr.Zero; try { int size = Marshal.SizeOf(typeof(PointerToArrayOfPointerHelper)); //返回非托管类型的大小(以字节为单位)。 argvPtr = Marshal.AllocHGlobal(size); //从进程的非托管内存中分配内存。 Marshal.StructureToPtr(argv, argvPtr, false); //将数据从托管对象封送到非托管内存块。 return(libvlc_new(arguments.Length, argvPtr)); //创建一个libvlc实例,它是引用计数的 } finally { for (int i = 0; i < arguments.Length + 1; i++) { if (argv.pointers[i] != IntPtr.Zero) { Marshal.FreeHGlobal(argv.pointers[i]); //释放以前使用 System.Runtime.InteropServices.Marshal.AllocHGlobal(System.IntPtr) 从进程的非托管内存中分配的内存。 } } if (argvPtr != IntPtr.Zero) { Marshal.FreeHGlobal(argvPtr); /* 释放以前使用 System.Runtime.InteropServices.Marshal.AllocHGlobal(System.IntPtr) 从进程的非托管内存中分配的内存。 */ } } }
public static IntPtr libvlc_new(string[] arguments) { PointerToArrayOfPointerHelper argv = new PointerToArrayOfPointerHelper(); argv.pointers = new IntPtr[11]; for (int i = 0; i < arguments.Length; i++) { argv.pointers[i] = Marshal.StringToHGlobalAnsi(arguments[i]); } IntPtr argvPtr = IntPtr.Zero; try { int size = Marshal.SizeOf(typeof(PointerToArrayOfPointerHelper)); argvPtr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(argv, argvPtr, false); return libvlc_new(arguments.Length, argvPtr); } finally { for (int i = 0; i < arguments.Length + 1; i++) { if (argv.pointers[i] != IntPtr.Zero) { Marshal.FreeHGlobal(argv.pointers[i]); } } if (argvPtr != IntPtr.Zero) { Marshal.FreeHGlobal(argvPtr); } } }