C++/C# 最基本的Marshal和Ptr

Vidyo32.VidyoClientInEventLogin Login = new Vidyo32.VidyoClientInEventLogin();

 

            Login.portalUri = this.tbxIP.Text.Trim(); //"http://kaunas.vidyo.scandihealth.net";

            Login.userName = this.tbxID.Text.Trim(); //"test-sa3";

            Login.userPass = this.tbxPwd.Text.Trim(); //"test-sa3";

 

 

 

            int size = Marshal.SizeOf(Login);

            IntPtr ptr = Marshal.AllocCoTaskMem(size);

            Marshal.StructureToPtr(Login, ptr, false);

 

            Int32 test = Vidyo32.VidyoClientSendEvent(Vidyo32.VidyoClientInEvent.VIDYO_CLIENT_IN_EVENT_LOGIN, ptr, size);

            Login = (Vidyo32.VidyoClientInEventLogin)Marshal.PtrToStructure(ptr, typeof(Vidyo32.VidyoClientInEventLogin));

 

 

在此总结一句,C++结构体和C#结构体转换过程中,一定要注意内存大小一致

posted @ 2015-11-10 14:49  acdyf  阅读(1752)  评论(0编辑  收藏  举报