在下面的代码中, pcp_Out 应该以 ANSI 格式返回系统日期。
返回系统日期,但前面有一些垃圾字符?
AllocHGlobal 是初始化IntPtr的正确方法吗?
[DllImport("Open32env.dll", CharSet = CharSet.Ansi, ExactSpelling = false, EntryPoint = "CallOPLFunction", CallingConvention = CallingConvention.StdCall)] static extern int CallOPLFunction(long pl_Instr, IntPtr pcp_In, out IntPtr pcp_Out, out IntPtr pcp_ErrorMessage); static void Main(string[] args) { IntPtr OutPtr = Marshal.AllocHGlobal(0); IntPtr ErrorPtr = Marshal.AllocHGlobal(0); IntPtr inPtr = Marshal.StringToHGlobalAnsi(""); long invalue = 0; int ret = CallOPLFunction(invalue, inPtr, out OutPtr, out ErrorPtr); string Outstring = Marshal.PtrToStringAnsi(OutPtr,30); Marshal.FreeHGlobal(OutPtr); Marshal.FreeHGlobal(ErrorPtr); Marshal.FreeHGlobal(inPtr); }
输出
Outstring = "h\0Qr\0\0\0\0Ä<g\a?\004/22/13 10:25"