//托管内存中的数据 复制到非托管内存中 IntPtr ptrURL = Marshal.StringToHGlobalAnsi("123465"); //释放在非托管中分配的内存 Marshal.FreeHGlobal(ptrURL);
NET_DVR_StartRemoteConfig(0,0, ptrURL, //字符串在内存中的地址 "123465".Length,//长度 callback //回调函数 ,IntPtr.Zero ); } // 调用API /* 函数原型 LONG NET_DVR_StartRemoteConfig( LONG lUserID, // int DWORD dwCommand, // uint LPVOID lpInBuffer, // IntPtr DWORD dwInBufferLen, // Int32 fRemoteConfigCallback cbStateCallback, // RemoteConfigCallback LPVOID pUserData // IntPtr ); callback 原型 该原型 与委托的关联 public delegate void RemoteConfigCallback(uint dwType, IntPtr lpBuffer, uint dwBufLen, IntPtr pUserData); //声明函数类型 ??? typedef void(CALLBACK *fRemoteConfigCallback)( DWORD dwType, void *lpBuffer, DWORD dwBufLen, void *pUserData ); */ [DllImportAttribute(@"HCNetSDK.dll")] public static extern int NET_DVR_StartRemoteConfig( int lUserID, uint dwCommand, IntPtr lpInBuffer, Int32 dwInBufferLen, RemoteConfigCallback cbStateCallback, IntPtr pUserData );
//从非托管内存中读取一个32位带符号的整数 uint dwStatus = (uint)Marshal.ReadInt32(ptrURL); //解析包数据 ptrURL = Marshal.StringToHGlobalAnsi("123465"); /* 1B = 8bit ==1byte 1KB=1024B 1MB=1024KB */ byte[] bUserInfoSearch = new byte[1024 * 10]; //10kb大小 //将数据从非 托管内存指针 复制到托管数组中 Marshal.Copy( ptrURL,//指针地址 bUserInfoSearch, //数组 0, // 开始索引 bUserInfoSearch.Length //长度 ); //以utf8编码读取数组 string strUserInfoSearch = System.Text.Encoding.UTF8.GetString(bUserInfoSearch);