示例#1
0
        private void button2_Click(object sender, EventArgs e)
        {
            LogonActiveServer tInfo = new LogonActiveServer();

            tInfo.iSize     = Marshal.SizeOf(tInfo);
            tInfo.cUserName = new char[16];
            Array.Copy(textUser.Text.ToCharArray(), tInfo.cUserName, textUser.Text.Length);
            tInfo.cUserPwd = new char[16];
            Array.Copy(textPwd.Text.ToCharArray(), tInfo.cUserPwd, textPwd.Text.Length);
            tInfo.cProductID = new char[32];

            Array.Copy("ID0000801941341231130644".ToCharArray(), tInfo.cProductID, "ID0000801941341231130644".Length);
            //IntPtr intptr = Marshal.AllocCoTaskMem(tInfo.iSize);
            //Marshal.StructureToPtr(tInfo, intptr, true);//false容易造成内存泄漏

            //LogonPara tInfo = new LogonPara();
            //tInfo.iSize = Marshal.SizeOf(tInfo);
            //tInfo.iNvsPort = Int32.Parse(textPort.Text);
            //tInfo.cNvsIP = new char[32];
            //Array.Copy(textIP.Text.ToCharArray(), tInfo.cNvsIP, textIP.Text.Length);
            //tInfo.cUserName = new char[16];
            //Array.Copy(textUser.Text.ToCharArray(), tInfo.cUserName, textUser.Text.Length);
            //tInfo.cUserPwd = new char[16];
            //Array.Copy(textPwd.Text.ToCharArray(), tInfo.cUserPwd, textPwd.Text.Length);

            IntPtr intptr = Marshal.AllocCoTaskMem(tInfo.iSize);

            Marshal.StructureToPtr(tInfo, intptr, true);//false容易造成内存泄漏
            Int32 iLogonId = NVSSDK.NetClient_Logon_V4(4, intptr, tInfo.iSize);

            Marshal.FreeHGlobal(intptr);//释放分配的非托管内存。
            if (iLogonId < 0)
            {
                MessageBox.Show("logon failed!");
                return;
            }
        }
示例#2
0
        private static void LogonDevice(int _iLogonType)
        {
            string strIP;
            string strUserName;
            string strPassword;
            string strProductID;

            Console.WriteLine("Please input user name: ");
            strUserName = Console.ReadLine();

            Console.WriteLine("Please input password: "******"Please input ProductID: ");
                    strProductID  = Console.ReadLine();
                    tActive       = new LogonActiveServer();
                    tActive.iSize = Marshal.SizeOf(tActive);
                    Array.Copy(Encoding.ASCII.GetBytes(strUserName), tActive.btUserName, strUserName.Length);
                    Array.Copy(Encoding.ASCII.GetBytes(strPassword), tActive.btUserPwd, strPassword.Length);
                    Array.Copy(Encoding.ASCII.GetBytes(strProductID), tActive.btProductID, strProductID.Length);
                    pvPara = Marshal.AllocHGlobal(Marshal.SizeOf(tActive));
                    Marshal.StructureToPtr(tActive, pvPara, true);
                    iBufLen = Marshal.SizeOf(tActive);

                    DsmOnline tOnline = new DsmOnline();
                    int       length  = Marshal.SizeOf(tOnline);
                    Array.Copy(Encoding.ASCII.GetBytes(strProductID), tOnline.btProductID, strProductID.Length);
                    IntPtr ptOnline = IntPtr.Zero;
                    try
                    {
                        ptOnline = Marshal.AllocHGlobal(length);
                        Marshal.StructureToPtr(tOnline, ptOnline, true);
                        NVSSDK.NetClient_GetDsmRegstierInfo(SDKConstMsg.DSM_CMD_GET_ONLINE_STATE, ptOnline, Marshal.SizeOf(tOnline));
                        int iOutTime = 0;
                        while (SDKTypes.DSM_STATE_ONLINE != tOnline.iOnline)
                        {
                            if (iOutTime >= 20)
                            {
                                Console.WriteLine("Device not register!\n");
                                break;
                            }
                            Thread.Sleep(1000);
                            NVSSDK.NetClient_GetDsmRegstierInfo(SDKConstMsg.DSM_CMD_GET_ONLINE_STATE, ptOnline, Marshal.SizeOf(tOnline));
                            iOutTime++;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(ptOnline);
                    }
                }
                else
                {
                    string strCharSet = "UTF-8";
                    Console.WriteLine("Please input server IP: ");
                    strIP            = Console.ReadLine();
                    tNormal          = new LogonPara();
                    tNormal.iSize    = Marshal.SizeOf(tNormal);
                    tNormal.iNvsPort = 3000;
                    Array.Copy(Encoding.ASCII.GetBytes(strIP), tNormal.btNvsIP, strIP.Length);
                    Array.Copy(Encoding.ASCII.GetBytes(strUserName), tNormal.btUserName, strUserName.Length);
                    Array.Copy(Encoding.ASCII.GetBytes(strPassword), tNormal.btUserPwd, strPassword.Length);
                    Array.Copy(Encoding.ASCII.GetBytes(strCharSet), tNormal.btCharSet, strCharSet.Length);
                    iBufLen = Marshal.SizeOf(tNormal);
                    pvPara  = Marshal.AllocHGlobal(Marshal.SizeOf(tNormal));
                    Marshal.StructureToPtr(tNormal, pvPara, true);
                }

                g_iLogonID = NVSSDK.NetClient_Logon_V4(_iLogonType, pvPara, iBufLen);
                if (g_iLogonID < 0)
                {
                    Console.WriteLine("[NetClient_Logon_V4] fail! " + g_iLogonID + "\n");
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Marshal.FreeHGlobal(pvPara);
            }
        }