Delphi封装Novell LDAP底层API, 提供给C#.Net开发

  Delphi封装Novell LDAP底层API, 提供给C#.Net开发

Delphi封装Novell LDAP底层API, 提供给C#.Net开发


C# Novell LDAP


前段时间研究Novell LDAP, 从网上搜集到一堆无用的代码,全部用不了,网上的资源也少,幸好手头有一套Delphi版的API源码, 于是基于Delphi封装了一个标准的DLL库NovellLdapImp.dll给C#调用。




///*************************************************************************/
///*
///* 文件名 :NovellLdapTools.cs
///
///* 程序说明 : Delphi封装Novell LDAP底层API, 由C#.Net程序调用
///
///* 原创作者 :孙中吕
///* Copyright 2010-2011 C/S框架网 www.csframework.com
///*
///**************************************************************************/




/// <summary>
/// Delphi封装Novell LDAP底层API, 由C#.Net程序调用
/// </summary>
public class NovellLdapTools
{
   const string LINE_SEPERATOR_1 = "\r\n";
   const string LINE_SEPERATOR = "||";
   const string COLUMN_SEPERATOR = "\t";
   const string DLL_NAME = "NovellLdapImp.dll";
   
   const int CHAR_LENGTH = 100000;
   
   /// <summary>
   /// 调用Delphi编译的DLL
   /// </summary>
   private class LDAP_DLL_DelphiComplied
   {
      [DllImport(DLL_NAME, EntryPoint = "NovellGetGroups", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
      public static extern int NovellGetGroups(ref string CurrentAccount, StringBuilder ADestBuffer, int ADestSize);
      
      [DllImport(DLL_NAME, EntryPoint = "NovellIsMembership", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
      public static extern string NovellIsMembership(string GroupName, ref string CurrentAccount);
      
      [DllImport(DLL_NAME, EntryPoint = "NovellHasClient", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
      public static extern string NovellHasClient();
      
      [DllImport(DLL_NAME, EntryPoint = "NovellGetGroupByAccount", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
      public static extern int NovellGetGroupByAccount(string CurrentAccount, StringBuilder ADestBuffer, int ADestSize);
      
      [DllImport(DLL_NAME, EntryPoint = "NovellGetObjects", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
      public static extern int NovellGetObjects(string NovellPath, string ClassName, StringBuilder ADestBuffer, int ADestSize, int only3);
      
      [DllImport(DLL_NAME, EntryPoint = "NovellGetObjectProperties", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
      public static extern int NovellGetObjectProperties(string NovellPath, string ClassName, StringBuilder ADestBuffer, int ADestSize);
      
      [DllImport(DLL_NAME, EntryPoint = "NovellGetAllUsers", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
      public static extern int NovellGetAllUsers(string Country, int ReturnPath, StringBuilder ADestBuffer, int ADestSize);
      
      [DllImport(DLL_NAME, EntryPoint = "NovellWhoAmI", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
      public static extern string NovellWhoAmI();
      
   }
   
   /// <summary>
   /// 获取当前登录的Novell用户
   /// </summary>
   /// <returns></returns>
   public static string NovellWhoAmI()
   {
      try
      {
         return LDAP_DLL_DelphiComplied.NovellWhoAmI();
      }
      catch
      {
         return string.Empty;
      }
   }
   
   /// <summary>
   /// 获取组织单元内所有用户
   /// </summary>
   /// <param name="Country">组织单元(.com .cn 等等)</param>
   /// <param name="ReturnPath">是否包含完整路径</param>
   /// <returns></returns>
   public static DataTable NovellGetAllUsers(string Country, bool ReturnPath)
   {
      try
      {
         StringBuilder sb = new StringBuilder(CHAR_LENGTH);
         int count = LDAP_DLL_DelphiComplied.NovellGetAllUsers(Country, ReturnPath ? 1 : 0, sb, CHAR_LENGTH);
         return GetStringTable(sb.ToString(), LINE_SEPERATOR_1, COLUMN_SEPERATOR, ReturnPath == false);
      }
      catch
      {
         return GetStringTable(" \t ", LINE_SEPERATOR_1, COLUMN_SEPERATOR, true);
      }
   }
   
   /// <summary>
   /// 获取Novell对象的属性
   /// </summary>
   /// <param name="NovellPath">Novell目录路径, 如:.edp.CSFRAMEWORK.mo 或 .CSFRAMEWORK.mo</param>
   /// <param name="ClassName">类名,如:Organizational Unit,Group,User</param>
   /// <returns></returns>
   public static DataTable NovellGetObjectProperties(string NovellPath, string ClassName)
   {
      try
      {
         StringBuilder sb = new StringBuilder(CHAR_LENGTH);
         int count = LDAP_DLL_DelphiComplied.NovellGetObjectProperties(NovellPath, ClassName, sb, CHAR_LENGTH);
         return GetStringTable(sb.ToString(), LINE_SEPERATOR, COLUMN_SEPERATOR, false);
      }
      catch
      {
         return GetStringTable(" \t ", LINE_SEPERATOR, COLUMN_SEPERATOR, false);
      }
   }
   
   /// <summary>
   /// 获取Novell网对象
   /// </summary>
   /// <param name="NovellPath">有效路径.如: .edp.CSFRAMEWORK.mo 或 .CSFRAMEWORK.mo</param>
   /// <param name="ClassName">取对象类型: Organizational Unit,Group,User </param>
   /// <returns></returns>
   public static DataTable NovellGetObjects(string NovellPath, string ClassName, bool only3)
   {
      try
      {
         StringBuilder sb = new StringBuilder(CHAR_LENGTH);
         int count = LDAP_DLL_DelphiComplied.NovellGetObjects(NovellPath, ClassName, sb, CHAR_LENGTH, only3 ? 1 : 0);
         return GetStringTable(sb.ToString(), LINE_SEPERATOR, COLUMN_SEPERATOR, false);
      }
      catch
      {
         return GetStringTable(" \t ", LINE_SEPERATOR, COLUMN_SEPERATOR, false);
      }
   }
   
   /// <summary>
   /// 调用API返回的字符串转换为DataTable
   /// </summary>
   /// <param name="content">调用API返回的字符串</param>
   /// <param name="lineSeperator">行分隔符</param>
   /// <param name="columnSeperator">列分隔符</param>
   /// <param name="onlyLine">仅返回行数据,转换DataTable时只处理行</param>
   /// <returns></returns>
   private static DataTable GetStringTable(string content, string lineSeperator, string columnSeperator, bool onlyLine)
   {
      //创建表结构
      DataTable dt = new DataTable();
      dt.Columns.Add("Name", typeof(string));
      dt.Columns.Add("Value", typeof(string));
      
      string[] lines = content.Split(new string[] { lineSeperator }, StringSplitOptions.RemoveEmptyEntries); //分开行
      if (lines.Length == 0) return dt;
      
      foreach (string line in lines)
      {
         if (onlyLine)
         {
            DataRow temp = dt.NewRow();
            temp[0] = line;
            temp[1] = line;
            dt.Rows.Add(temp);
         }
         else
         {
            string[] items = line.Split(new string[] { columnSeperator }, StringSplitOptions.None); //分开列
            if (items.Length >= 2)
            {
               DataRow temp = dt.NewRow();
               temp[0] = items[0];
               temp[1] = items[1];
               dt.Rows.Add(temp);
            }
         }
      }
      
      return dt;
   }
   
   /// <summary>
   /// 获取当前Novell用户及所在的组列表
   /// </summary>
   /// <param name="CurrentAccount">返回当前用户</param>
   /// <returns>返回所在的组列表</returns>
   public static string[] NovellGetGroups(ref string CurrentAccount)
   {
      StringBuilder sb = new StringBuilder(CHAR_LENGTH);
      int count = LDAP_DLL_DelphiComplied.NovellGetGroups(ref CurrentAccount, sb, sb.Capacity);
      string groups = sb.ToString();
      string[] groupList = groups.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
      return groupList;
   }
   
   /// <summary>
   /// 获取指定Novell帐号的用户组
   /// </summary>
   /// <param name="CurrentAccount">Novell帐号</param>
   /// <returns></returns>
   public static string[] NovellGetGroupByAccount(string CurrentAccount)
   {
      StringBuilder sb = new StringBuilder(CHAR_LENGTH);
      int count = LDAP_DLL_DelphiComplied.NovellGetGroupByAccount(CurrentAccount, sb, sb.Capacity);
      string groups = sb.ToString();
      string[] groupList = groups.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
      return groupList;
   }
   
   /// <summary>
   /// 当前登录的用户是否属于指定的组
   /// </summary>
   /// <param name="GroupName">指定的组</param>
   /// <returns></returns>
   public static bool NovellIsMembership(string GroupName, ref string CurrentAccount)
   {
      string result = LDAP_DLL_DelphiComplied.NovellIsMembership(GroupName, ref CurrentAccount);
      return result.ToLower() == "yes";
   }
   
   /// <summary>
   /// 是否安装Novell客户端
   /// </summary>
   /// <returns></returns>
   public static bool NovellHasClient()
   {
      string result = LDAP_DLL_DelphiComplied.NovellHasClient();
      return result.ToLower() == "yes";
   }
   
}

// 来源:www.CSFramework.com, C/S结构框架学习网




C/S框架网|原创精神.创造价值.打造精品


扫一扫加作者微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务

版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
C/S框架网
发表评论

评论内容
昵称:
关联文章

Delphi封装Novell LDAP底层API, 提供C#.Net开发
C#.Net 调用Delphi 编译的ActiveX COM组件(原)
什么是Web Api? ASP.NET Web Api体系架构
Delphi程序调用C#.Net编译的DLL并打开窗体(详解)
C#调用Delphi编译的DLL函数库返回大文本数据
C#数据转换类ConvertEx,封装.Net的Convert对象
C/S框架网提供基于C#.NET前端Winform开发框架及后端WebApi开发框架
C#.NET短信通用接口设计-调用短信API案例|C/S框架网
CSFramework.DB基于ADO.NET多数据库底层组件包(MSSQL+Oracle+MySQL)
WebApi接口安全机制:API接口限流防止恶意访问 ThrottlingHandler消息处理机制
C/S框架网提供原创开源.NET开发架构.NET管理系统框架
C#底层键盘勾子类
.Net后端框架|WebApi服务端开发框架|C/S框架网原创作品
WebApi快速开发框架,C# ASP.NET后端开发平台
WebAPI 开发框架 C#.NET WebApi开发平台
C/S框架网提供MES开发平台或MES开发框架
C#.Net适合开发C/S结构的系统吗?
C/S快速开发框架能提供进销存源码参考吗?
VS.NET后端框架,C#.NET开发的服务器端开发框架 - C/S框架网
C#使用低级别的Windows API勾子阻止个别按键