Call win32 CreateProfile() from C# managed code <p>Quick question (hopefully), how do I properly call the win32 function CreateProfile() from C# (managed code)? I have tried to devise a solution on my own with no avail.</p> <p><b>The syntax for CreateProfile() is:</b></p> <pre><code> HRESULT WINAPI CreateProfile( __in LPCWSTR pszUserSid, __in LPCWSTR pszUserName, __out LPWSTR pszProfilePath, __in DWORD cchProfilePath );</code></pre> <p>The supporting documents can be found in the <a href="http://msdn.microsoft.com/en-us/library/bb762271(v=VS.85).aspx" rel="nofollow">MSDN library</a>.</p> <p>The code I have so far is posted below.</p> <p><b>DLL Import:</b></p> <pre><code> [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); </code></pre> <p><b>Invoking the function:</b></p> <pre><code> /* 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); </code></pre> <p><br /> The problem is that no user profile is ever created and CreateProfile() returns an error code of: <em>0x800706f7</em>. Any helpful information on this matter is more than welcomed.</p> <p>Thanks,<br /> -Sean</p> <p><br /> <b>Update:</b> Solved! string buffer for pszProfilePath cannot have a length greater than 260.</p>