Delphi LoadUserProfile

 type
    LPPROFILEINFOA  =   ^ _PROFILEINFOA ;
    _PROFILEINFOA  =   record
        dwSize         :   DWORD ;     // Set to sizeof(PROFILEINFO) before calling
        dwFlags        :   DWORD ;     // See flags above
        lpUserName     :  LPSTR ;     // User name (required)
        lpProfilePath  :  LPSTR ;     // Roaming profile path (optional, can be    NULL)
        lpDefaultPath  :  LPSTR ;     // Default user profile path (optional, can be NULL)
        lpServerName   :  LPSTR ;     // Validating domain controller name in netbios format (optional, can be NULL but group NT4 style policy won't be applied)
        lpPolicyPath   :  LPSTR ;     // Path to the NT4 style policy file (optional, can be NULL)
        hProfile       :   THandle ;   // Filled in by the function. Registry key handle open to the root.
     end ;

function  LoadUserProfile ( hToken  :   THandle ;   var  lpProfileInfo  :  _PROFILEINFOA ) :   BOOL ;   stdcall ;   external   'userenv.dll'  name  'LoadUserProfileA' ;
function  UnloadUserProfile ( hToken  :   THandle ;  hProfile  :   THandle ) :   BOOL ;   stdcall ;   external   'userenv.dll' ;

var
    LoadUserProfileLog :   String ;
    
     //userprofile handles
    h_Token    :   Cardinal ;
    h_Profile  :   Cardinal ;

implementation

uses  ComServ ,  SysUtils ;

class   function  TManager . LoadProfile ( )   :   Boolean ;
var
    h_Process      :   Cardinal ;
    lpProfileInfo  :  _PROFILEINFOA ;
    
     //username
    UserName     :   string ;
    UserNameLen  :   Dword ;

    debugFile    :   TextFile ;
    data         :   TDateTime ;

begin
    h_Token    : =   0 ;
    h_Profile  : =   0 ;

     try
        LoadUserProfileLog  : =   'Before GetCurrentProcess()' #13 #10 ;
        h_Process  : =  GetCurrentProcess ( ) ;
         if ( OpenProcessToken ( h_Process , TOKEN_QUERY  or  TOKEN_IMPERSONATE  or  TOKEN_DUPLICATE ,  h_Token ) )   then
         begin
            LoadUserProfileLog  : =  LoadUserProfileLog  +   #9 'After OpenProcessToken call' #13 #10 ;
            UserNameLen  : =   255 ;
             SetLength ( userName ,  UserNameLen )   ;
             If  GetUserName ( PChar ( UserName ) ,  UserNameLen )   Then
             begin
                LoadUserProfileLog  : =  LoadUserProfileLog  +   #9 'After GetUserName(): ' + Copy ( UserName , 1 , UserNameLen - 1 ) + #13 #10 ;
                 FillChar ( lpProfileInfo , SizeOf ( _PROFILEINFOA ) , #0 ) ;
                LoadUserProfileLog        : =  LoadUserProfileLog  +   #9 'Po FillChar' #13 #10 ;
                lpProfileInfo . dwSize       : =   SizeOf ( lpProfileInfo ) ;
                lpProfileInfo . dwFlags      : =  PI_NOUI ;
                lpProfileInfo . lpUserName   : =   PChar ( UserName ) ;

                LoadUserProfileLog  : =  LoadUserProfileLog  +   #9 'LoadUserProfile call' #13 #10 ;
                 if   not  LoadUserProfile ( h_Token , lpProfileInfo )   then
                 begin
                    LoadUserProfileLog  : =  LoadUserProfileLog  +   #9 'LoadUserProfile error: ' +   SysErrorMessage ( GetLastError ) + #13 #10 ;
                    h_Profile           : =  lpProfileInfo . hProfile ;
                    Result              : =   False ;
                 end
                 else
                 begin
                    LoadUserProfileLog  : =  LoadUserProfileLog  +   #9 'LoadUserProfile call success' #13 #10 ;
                    h_Profile  : =  lpProfileInfo . hProfile ;
                    Result  : =   True ;
                 end ;
             end ;
         end
     except   on  E : Exception  do
         begin
            LoadUserProfileLog  : =  LoadUserProfileLog  +   'Initialize component error: ' + E . Message + #13 #10 ;
         end ;
     end ;
end ;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值