Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4134707
  • 博文数量: 447
  • 博客积分: 1241
  • 博客等级: 中尉
  • 技术积分: 5786
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-27 06:48
个人简介

读好书,交益友

文章分类

全部博文(447)

文章存档

2023年(6)

2022年(29)

2021年(49)

2020年(16)

2019年(15)

2018年(23)

2017年(67)

2016年(42)

2015年(51)

2014年(57)

2013年(52)

2012年(35)

2011年(5)

分类: C/C++

2014-03-26 10:05:51

最近要用mingwin调用GetUserNameEx函数,发现没有lib,只要动态调用了。

点击(此处)折叠或打开

  1. #include <Windows.h>

  2. #define KERBEROS_DLL_NAME     "SECUR32.DLL"
  3. #define KRB5_MAX_NAMELEN 256
  4. HINSTANCE krb5_secdll_hInst = NULL;
  5. typedef enum {
  6.     NameUnknown = 0,
  7.     NameFullyQualifiedDN = 1,
  8.     NameSamCompatible = 2,
  9.     NameDisplay = 3,
  10.     NameUniqueId = 6,
  11.     NameCanonical = 7,
  12.     NameUserPrincipal = 8,
  13.     NameCanonicalEx = 9,
  14.     NameServicePrincipal = 10,
  15.     NameDnsDomain = 12
  16. } EXTENDED_NAME_FORMAT, *PEXTENDED_NAME_FORMAT;


  17. typedef BOOLEAN (WINAPI GETUSERNAMEEX)(EXTENDED_NAME_FORMAT NameFormat,LPTSTR lpNameBuffer,PULONG nSize);

  18. GETUSERNAMEEX     * fp_GetUserNameEx = NULL; /* GetUserNameEx()            */

  19. int _tmain(int argc, _TCHAR* argv[])
  20. {
  21.     krb5_secdll_hInst = LoadLibraryA(KERBEROS_DLL_NAME);
  22.     fp_GetUserNameEx = (GETUSERNAMEEX *) GetProcAddress(krb5_secdll_hInst, "GetUserNameExA");
  23.     /**************************************************************/
  24.     /* Check an memorize whether GetUserNameEx(NameUserPrincipal) */
  25.     /* works -- it only works when an Active Directory is present */
  26.     /**************************************************************/
  27.     {
  28.         char buf[KRB5_MAX_NAMELEN*2];
  29.         ULONG buf_size = 0;
  30.         EXTENDED_NAME_FORMAT ef=NameSamCompatible;
  31.         
  32.         if ((fp_GetUserNameEx)(ef,NULL, &buf_size)==FALSE ) {
  33.             
  34.         }
  35.         buf_size = sizeof(buf)-1;
  36.         if ((fp_GetUserNameEx)(ef,(LPTSTR) &(buf[0]), &buf_size)!=FALSE ) {
  37.             printf(buf);
  38.         }
  39.     }
  40.     
  41.     if ( krb5_secdll_hInst != NULL ) {
  42.         FreeLibrary( krb5_secdll_hInst );
  43.         krb5_secdll_hInst = NULL;
  44.     }
  45.     getchar();
  46.     return 0;
  47. }

需要注意的是,第一次调用GetUserNameEx,第二个参数为NULL,第三个参数一定要为0,否则出现内存访问错误。
java 可以使用jna
使用jna 3.3.0的版本

点击(此处)折叠或打开

  1. char[] name = new char[100];
  2. // Gets current user extended name
  3.         com.sun.jna.platform.win32.Secur32.INSTANCE.GetUserNameEx(
  4.                 com.sun.jna.platform.win32.Secur32.EXTENDED_NAME_FORMAT.NameSamCompatible,
  5.                 name, new com.sun.jna.ptr.IntByReference(name.length) );
  6.         String userName = new String(name).trim();
  7.         System.out.println(userName);


使用jna 4.1.0

点击(此处)折叠或打开

  1. String remoteUserInfo = Secur32Util
  2.                 .getUserNameEx(Secur32.EXTENDED_NAME_FORMAT.NameSamCompatible);
  3.         if (remoteUserInfo != null) {
  4.             String remoteDomain = null, remoteUser = null;
  5.             int atIdx =remoteUserInfo.indexOf("\\");
  6.             if (atIdx > -1) {
  7.                 remoteDomain = remoteUserInfo.substring(0, atIdx);
  8.                 remoteUser = remoteUserInfo.substring(atIdx + 1);
  9.             } else {
  10.                 remoteUser = remoteUserInfo;
  11.             }
  12.             System.out.println(remoteUser);

阅读(4732) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~