Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.1 KiB

  1. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Module Name:
  3. loadprofile.cpp
  4. Abstract:
  5. test LoadUserProfile
  6. -----------------------------------------------------------------------------*/
  7. #define UNICODE 1
  8. #define _UNICODE 1
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <windows.h>
  12. #include <userenv.h>
  13. #include <ntsecapi.h>
  14. bool
  15. SetCurrentEnvironmentVariables(
  16. PWCHAR pchEnvironment
  17. )
  18. // Sets each environment variable in the block pchEnvironment into the
  19. // current process' environment block by calling WIN::SetEnvironmentVariable
  20. {
  21. WCHAR* pch = pchEnvironment;
  22. WCHAR* pchName;
  23. BOOL fStatus = TRUE;
  24. if (pch) {
  25. while (*pch) {
  26. // save pointer to beginning of name
  27. pchName = pch;
  28. // skip possible leading equals sign
  29. if (*pch == '=') {
  30. pch++;
  31. }
  32. // advance to equals sign separating name from value
  33. while (*pch != '=') {
  34. pch++;
  35. }
  36. // null-terminate name, overwriting equals sign
  37. *pch++ = 0;
  38. // set the value. pchName now points to the name and pch points to the value
  39. // fStatus = SetEnvironmentVariableW(pchName, pch);
  40. printf("%S=%S\n", pchName, pch);
  41. if ( ! fStatus ) {
  42. return false;
  43. }
  44. // advance over the value
  45. while (*pch++ != 0) {
  46. ;
  47. }
  48. // we're now positioned at the next name, or at the block's null
  49. // terminator and we're ready to go again
  50. }
  51. }
  52. return true;
  53. }
  54. DWORD
  55. __cdecl
  56. wmain (INT argc, WCHAR* argv[])
  57. {
  58. DWORD dwRet = -1;
  59. HANDLE hToken = NULL;
  60. PROFILEINFOW ProfileInfo = { 0 };
  61. TCHAR pwszUserName[MAX_PATH];
  62. DWORD dwSize = MAX_PATH - 1;
  63. PWCHAR pchSystemEnvironment;
  64. if ( ! OpenProcessToken (
  65. GetCurrentProcess(),
  66. TOKEN_ALL_ACCESS,
  67. &hToken
  68. ) )
  69. {
  70. printf("error: LogonUser - %d\n", GetLastError() );
  71. goto end;
  72. }
  73. dwRet = GetUserName(
  74. pwszUserName,
  75. &dwSize
  76. );
  77. if (!dwRet) {
  78. printf("error: GetUserName - %d\n", GetLastError() );
  79. goto end;
  80. }
  81. ProfileInfo.dwSize = sizeof ( ProfileInfo );
  82. ProfileInfo.dwFlags = PI_NOUI;
  83. ProfileInfo.lpUserName = pwszUserName;
  84. if ( ! LoadUserProfile (
  85. hToken,
  86. &ProfileInfo
  87. ) )
  88. {
  89. printf("error: LoadUserProfile - %d\n", GetLastError() );
  90. goto end;
  91. } else {
  92. printf("LoadUserProfile succeeded for user: %S.\n", pwszUserName);
  93. //
  94. // Load the user's environment block so we can inject it into their current
  95. // environment
  96. //
  97. if (CreateEnvironmentBlock((void**)&pchSystemEnvironment, hToken, FALSE)) {
  98. printf("Successfully Loaded environment block:\n");
  99. // set each machine environment variable into the current process's environment block
  100. SetCurrentEnvironmentVariables(pchSystemEnvironment);
  101. // we're done with the block so destroy it
  102. DestroyEnvironmentBlock(pchSystemEnvironment);
  103. } else {
  104. printf("error: Could not get environment block.");
  105. }
  106. }
  107. dwRet = 0;
  108. end:
  109. if ( hToken )
  110. {
  111. if ( ProfileInfo.hProfile )
  112. {
  113. UnloadUserProfile ( hToken, ProfileInfo.hProfile );
  114. }
  115. #if 0
  116. if ( pProfileBuffer )
  117. {
  118. LsaFreeReturnBuffer ( pProfileBuffer );
  119. }
  120. #endif
  121. CloseHandle ( hToken );
  122. }
  123. return dwRet;
  124. }