WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Miscellaneous

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Create User Profile

Question:

When a new user is created on a system, the user's "profile" is not necessarily created until the user logs in interactively for the first time.

In some cases, such as in the creation of service accounts or when other initialization should be done for the user, it may be nice to predefine the users profile.

Answer:

That follows is a not necessarily completely debugged version of some WinBatch code that attempts to do this, but is offered as a starting point to some industrious person...

Please let us know how you fare with it, and a copy of the perfect version to replace this one would be nice.



;Create initial profile
KernelDLL=StrCat(DirWindows(1),"kernel32.dll")
hKernel=DllLoad(KernelDLL)


ProcID=DllCall(hKernel,long:"GetCurrentProcessId")

STANDARD_RIGHTS_REQUIRED   =  983040 ;      (0x000F0000L)
SYNCHRONIZE                = 1048576 ;      (0x00100000L)
PROCESS_ALL_ACCESS         = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE |  4095 ; 0xFFF

hProcess=DllCall(hKernel,long:"OpenProcess",long:PROCESS_ALL_ACCESS,long:0,long:ProcID)

AdvAPIDLL=StrCat(DirWindows(1),"advapi32.dll")
hAdvAPI=DllLoad(AdvAPIDLL)

bbhToken=BinaryAlloc(4)
BinaryEodSet(bbhToken,4)

;TOKEN_IMPERSONATE | TOKEN_DUPLICATE | TOKEN_QUERY
TOKEN_DUPLICATE           = 2
TOKEN_IMPERSONATE         = 4
TOKEN_QUERY               = 8

hFlag=DllCall(hAdvApi,long:"OpenProcessToken",long:hProcess,long:TOKEN_DUPLICATE|TOKEN_IMPERSONATE|TOKEN_QUERY,lpbinary:bbhToken)
If hFlag==0  ; eeep it failed
    DllFree(hKernel)
    DllFree(hAdvApi)
    BinaryFree(bbhToken)
    x=DllLastError()
    Message("OpenProcessToken Failed code = ",x)
    Exit
EndIf

hToken=BinaryPeek4(bbhToken,0)
BinaryFree(bbhToken)

;Build GetStructureInfo
AddExtender("wwwnt34i.dll")
username="TestUser" ; ********************************************************* USERID ***************
bbusername=BinaryAlloc(StrLen(username)+1)
BinaryPokeStr(bbUsername,0,username)
lpUserName=IntControl(42,bbusername,0,0,0)

bbhProfile=BinaryAlloc(4)
BinaryEodSet(bbhProfile,4)
hProfile = RegOpenKey(@REGCURRENT, "")
BinaryPoke4(bbhProfile,0, hProfile )
lphProfile=IntControl(42,bbhProfile,0,0,0)


;  typedef struct _PROFILEINFO {
;0       DWORD   dwSize;          // size of structure
;4       DWORD   dwFlags;         // flags
;8       LPTSTR  lpUserName;      // user name
;12       LPTSTR  lpProfilePath;   // roaming profile path
;16       LPTSTR  lpDefaultPath;   // default user profile path
;20       LPTSTR  lpServerName;    // validating domain controller name
;24       LPTSTR  lpPolicyPath;    // Windows NT 4.0-style policy file
;28       HANDLE  hProfile;        // registry key handle
;   } PROFILEINFO, FAR * LPPROFILEINFO;
PI_OFFSET_dwSize        =   0  ; size of structure
PI_OFFSET_dwFlags       =   4  ; flags
PI_OFFSET_lpUserName    =   8  ; user name
PI_OFFSET_lpProfilePath =  12  ; roaming profile path
PI_OFFSET_lpDefaultPath =  16  ; default user profile path
PI_OFFSET_lpServerName  =  20  ; validating domain controller name
PI_OFFSET_lpPolicyPath  =  24  ; Windows NT 4.0-style policy file
PI_OFFSET_hProfile      =  28  ; registry key handle
PI_SIZE                 =  32

PROFILEINFO=BinaryAlloc(PI_SIZE)
BinaryEodSet(PROFILEINFO,PI_SIZE)

BinaryPoke4(PROFILEINFO,PI_OFFSET_dwSize,         PI_SIZE)
BinaryPoke4(PROFILEINFO,PI_OFFSET_dwFlags,        0)
BinaryPoke4(PROFILEINFO,PI_OFFSET_lpUserName,      lpUserName)
BinaryPoke4(PROFILEINFO,PI_OFFSET_lpProfilePath,   0)
BinaryPoke4(PROFILEINFO,PI_OFFSET_lpDefaultPath,   0)
BinaryPoke4(PROFILEINFO,PI_OFFSET_lpServerName,    0)
BinaryPoke4(PROFILEINFO,PI_OFFSET_lpPolicyPath,    0)
BinaryPoke4(PROFILEINFO,PI_OFFSET_hProfile    ,    lphProfile)

USERENVDLL=StrCat(DirWindows(1),"userenv.dll")
hUserEnv=DllLoad(USERENVDLL)

hflag=DllCall(hUserEnv,long:"LoadUserProfileA",long:hToken,lpBinary:PROFILEINFO)
If hflag==0  ; eeep it failed
    DllFree(hKernel)
    DllFree(hAdvApi)
    BinaryFree(bbUserName)
    BinaryFree(bbhProfile)
    BinaryFree(PROFILEINFO)
    x=DllLastError()
    Message("LoadUserProfile Failed code = ",x)
    Exit
EndIf

BinaryFree(PROFILEINFO)
BinaryFree(bbUserName)

hProfile=BinaryPeek4(bbhProfile,0)
BinaryFree(bbhProfile)


RegCloseKey(hProfile)

DllFree(hKernel)
DllFree(hAdvApi)

Message("All","Done")





Article ID:   W16017
File Created: 2005:03:31:11:37:10
Last Updated: 2005:03:31:11:37:10