Call win32 CreateProfile () desde C # código administrado

Pregunta rápida (con suerte), ¿cómo llamo correctamente a la función win32 CreateProfile () desde C # (código administrado)? He intentado idear una solución por mi cuenta sin resultado.

La sintaxis para CreateProfile () es:


HRESULT WINAPI CreateProfile(
  __in   LPCWSTR pszUserSid,
  __in   LPCWSTR pszUserName,
  __out  LPWSTR pszProfilePath,
  __in   DWORD cchProfilePath
);

Los documentos de respaldo se pueden encontrar enMSDN library.

El código que tengo hasta ahora se publica a continuación.

mportación @DLL:


[DllImport("userenv.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int CreateProfile(
                      [MarshalAs(UnmanagedType.LPWStr)] string pszUserSid,
                      [MarshalAs(UnmanagedType.LPWStr)] string pszUserName,
                      [Out][MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszProfilePath,
                      uint cchProfilePath);

Invocar la función:


/* Assume that a user has been created using: net user TestUser password /ADD */

// Get the SID for the user TestUser
NTAccount acct = new NTAccount("TestUser");
SecurityIdentifier si = (SecurityIdentifier)acct.Translate(typeof(SecurityIdentifier));
String sidString = si.ToString();

// Create string buffer
StringBuilder pathBuf = new StringBuilder(260);
uint pathLen = (uint)pathBuf.Capacity;

// Invoke function
int result = CreateProfile(sidString, "TestUser", pathBuf, pathLen);


El problema es que nunca se crea un perfil de usuario y CreateProfile () devuelve un código de error de: 0x800706f7. Cualquier información útil sobre este asunto es más que bienvenida.

Gracias
-Sean


Actualizar Resuelto! el búfer de cadena para pszProfilePath no puede tener una longitud superior a 260.

Respuestas a la pregunta(2)

Su respuesta a la pregunta