用户登录
1 初始化sdk
2 加密密码
3 登录
4 定阅推送功能
5 注册推送信息处理的回调函数
6 用户注销
/// <summary>
/// 多用户登录方法
/// </summary>
/// <param name="usrLoginName">用户名</param>
/// <param name="usrLoginPsw">密码</param>
/// <param name="srvIpAddr">服务器的ip</param>
/// <param name="cltIpAddr">用户所在客户端的ip</param>
/// <returns>用户登录代出信息</returns>
public LOGIN_INFO_S LoginMethod(String usrLoginName, String usrLoginPsw, String srvIpAddr, String cltIpAddr)
{
UInt32 ulRet = 0;
uint srvPort = 8800;
//1.初始化
ulRet = IMOSSDK.IMOS_Initiate("N/A", srvPort, 1, 1);
if (0 != ulRet)
{
MessageBox.Show("初始化失败!" + ulRet.ToString());
}
//2.加密密码
//分配加密后的数据缓存(非托管内存中分配,需要手动释放)
IntPtr ptr_MD_Pwd = Marshal.AllocHGlobal(sizeof(char) * IMOSSDK.IMOS_PASSWD_ENCRYPT_LEN);
ulRet = IMOSSDK.IMOS_Encrypt(usrLoginPsw, (UInt32)usrLoginPsw.Length, ptr_MD_Pwd);
if (0 != ulRet)
{
MessageBox.Show("加密密码失败!" + ulRet.ToString());
Application.Exit();//退出应用
}
//把字符串指针变为c#的string
String MD_PWD = Marshal.PtrToStringAnsi(ptr_MD_Pwd);
//释放分配内存
Marshal.FreeHGlobal(ptr_MD_Pwd);
//3.登录方法 IMOS_Login(只支持一个用户登录)
IntPtr ptrLoginInfo = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(LOGIN_INFO_S)));
//多用户登录
ulRet = IMOSSDK.IMOS_LoginEx(usrLoginName, //用户名
MD_PWD,//密码
srvIpAddr,//服务器的ip
cltIpAddr,//用户所在客户端的ip
ptrLoginInfo);//用户登录代出信息
if (0 != ulRet)
{
MessageBox.Show("IMOS_Login" + ulRet.ToString());
Application.Exit();
}
//序列化操作,将数据从非托管内存封送到指定托管对象
stLoginInfo = (LOGIN_INFO_S)Marshal.PtrToStructure(ptrLoginInfo, typeof(LOGIN_INFO_S));
//释放内存
Marshal.FreeHGlobal(ptrLoginInfo);
return stLoginInfo;
}
//4 订阅推送
ulResult = IMOSSDK.IMOS_SubscribePushInfo(ref stLoginInfo, //用户标识
(uint)SUBSCRIBE_PUSH_TYPE_E.SUBSCRIBE_PUSH_TYPE_ALL);//订阅类型
//5 注册回调
ulResult = IMOSSDK.IMOS_RegCallBackPrcFunc(ref stLoginInfo,//用户标识
ptrCB);//回调处理函数指针
//6 用户注销
IMOSSDK.IMOS_LogoutEx(ref stLoginInfo.stUserLoginIDInfo);
用户登录/注销所调用的sdk方法
1
/// <summary>
/// 对外初始化sdk
/// </summary>
/// <param name="strServerIP">服务器地址</param>
/// <param name="ulServerPort">服务器端口</param>
/// <param name="bUIFlag">sdk在UI侧的启动标记</param>
/// <param name="bXPFlag">是否包含有xp的标记</param>
/// <returns></returns>
[DllImport("imos_sdk.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern UInt32 IMOS_Initiate(String strServerIP, UInt32 ulServerPort, UInt32 bUIFlag, UInt32 bXPFlag);
2
/// <summary>
/// 加密密码
/// </summary>
/// <param name="strInput">密码</param>
/// <param name="ulInLen">密码长度</param>
/// <param name="ptrOutput">加密后的数据缓存</param>
/// <returns></returns>
[DllImport("imos_sdk.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern UInt32 IMOS_Encrypt(String strInput, UInt32 ulInLen, IntPtr ptrOutput);
3
/// <summary>
/// 多用户登录
/// </summary>
/// <param name="strUserLoginName">用户名</param>
/// <param name="strPassword">密码</param>
/// <param name="srvIpAddr">服务器ip</param>
/// <param name="cltIpAddr">客户端ip</param>
/// <param name="ptrSDKLoginInfo">用户登录代出信息</param>
/// <returns></returns>
[DllImport("imos_sdk.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern UInt32 IMOS_LoginEx(String strUserLoginName, String strPassword, String srvIpAddr, String cltIpAddr, IntPtr ptrSDKLoginInfo);
4
/// <summary>
/// 完成订阅推送功能
/// </summary>
/// <param name="stUserLoginIDInfo">登入信息</param>
/// <param name="ulSubscribePushType">订阅类型</param>
/// <returns></returns>
[DllImport("imos_sdk.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern UInt32 IMOS_SubscribePushInfo(ref USER_LOGIN_ID_INFO_S stUserLoginIDInfo, UInt32 ulSubscribePushType);
5
/// <summary>
/// 注册推送信息处理的回调函数
/// </summary>
/// <param name="pstUserLoginIDInfo">登入信息</param>
/// <param name="ptrCallBack">回调处理函数指针</param>
/// <returns></returns>
[DllImport("imos_sdk.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern UInt32 IMOS_RegCallBackPrcFunc(ref USER_LOGIN_ID_INFO_S stUserLoginIDInfo, IntPtr pfnCallBackProc);
6
//用户退出
IMOS_EXPORT VOID STDCALL IMOS_LogoutEx ( IN USER_LOGIN_ID_INFO_S * pstUserLogIDInfo ) //用户信息标识
所用到结构体
/// <summary>
/// 用户登录信息结构体
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct LOGIN_INFO_S
{
/** 用户登录ID信息 */
public USER_LOGIN_ID_INFO_S stUserLoginIDInfo;
/** 用户所属组织编码 */
[MarshalAs(UnmanagedType.ByValArray, SizeConst = IMOSSDK.IMOS_DOMAIN_CODE_LEN)]
public byte[] szOrgCode;
/** 用户所属域名称 */
[MarshalAs(UnmanagedType.ByValArray, SizeConst = IMOSSDK.IMOS_NAME_LEN)]
public byte[] szDomainName;
/** 用户所属域类型, 取值为#MM_DOMAIN_SUBTYPE_LOCAL_PHYSICAL和#MM_DOMAIN_SUBTYPE_LOCAL_VIRTUAL */
public UInt32 ulDomainType;
}
/// <summary>
/// 用户登录ID信息结构
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct USER_LOGIN_ID_INFO_S
{
/** 用户编码 */
[MarshalAs(UnmanagedType.ByValArray, SizeConst = IMOSSDK.IMOS_USER_CODE_LEN)]
public byte[] szUserCode;
/** 用户登录ID,是用户登录后服务器分配的,它是标记一次用户登录的唯一标识 */
[MarshalAs(UnmanagedType.ByValArray, SizeConst = IMOSSDK.IMOS_RES_CODE_LEN)]
public byte[] szUserLoginCode;
/** 用户登录的客户端IP地址 */
[MarshalAs(UnmanagedType.ByValArray, SizeConst = IMOSSDK.IMOS_IPADDR_LEN)]
public byte[] szUserIpAddress;
}