Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / February 2006

Tip: Looking for answers? Try searching our database.

DllImport for DeleteProfile results in the parmeter is incorrect

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris Martinez - 21 Feb 2006 18:35 GMT
Anyone that has an idea of how to invoke the DeleteProfile function
from UserEnv.dll through P/Invoke is greatly appreciated.  I cannot see
to find any example anywhere on the web of how to import this function.

Based on the original C prototype it seems like it should be very easy
to import, but I continuously get "The parameter is incorrect." (error
87), when the function is invoked.

The prototype in UserEnv.h is:

BOOL DeleteProfileA(
 LPCSTR lpSidString,
 LPCSTR lpProfilePath,
 LPCSTR lpComputerName );

The import declaration I used is as follows:

[DllImport( "userenv.dll", CharSet = CharSet.Ansi,
EntryPoint = "DeleteProfileA", SetLastError = true )]
public static extern bool DeleteProfile(
 string lpSidString,
 string lpProfilePath,
 string lpComputerName );

I have tried numerous variations, including the use of the Unicode
version, applying the MarshalAsAttribute with UnmanagedType.LPStr for
ANSI and UnmanagedType.LPWStr for Unicode, all of which have failed
with the same result.

The following is an example of invoking the function:

if ( !Win32.DeleteProfile( sid, null, Environment.MachineName ) )
 Console.WriteLine( new Win32Exception( Marshal.GetLastWin32Error()
).Message );

According the MSDN documentation the lpProfilePath can be NULL and will
be looked up in the Registry.  I've also tried specifying this
parameter with the correct path and it still fails.   I'm 100% sure
that the SID translation is correct because both of the functions I
have to translate account name to SID and vice versa both work
correctly.

I'm currently running this on Windows XP SP2, using C# 1.1, but this
will also have to work on Win2K and Win2K3 (which I don't know why it
wouldn't).

Anyone that could shed some light on the subject would be greatly
appreciated.

Thanks,

Chris
Chris Martinez - 24 Feb 2006 21:16 GMT
> Anyone that has an idea of how to invoke the DeleteProfile function
> from UserEnv.dll through P/Invoke is greatly appreciated.  I cannot see
[quoted text clipped - 48 lines]
>
> Chris

Unfortunately, I didn't get a reply from anyone, but I did finally
solve the problem.  Hopefully this will be useful to anyone who needs
to write anything that deletes user profiles.

The original DLL import signature is correct and there is not anything
special that needs to be done in order to invoke the function. The
problem seems to be related to something that is still loaded in
memory.

For example, if I login as "User A", then log off and login as "User
B", and try to delete the user profile for "User A", the functions
fails.  When inspecting the error code returned from "GetLastError",
the wonderfully ambiguous message (thanks MS!) reads "The parameter is
incorrect.".

When I thought I was going to have to abandon the use of
"DeleteProfile" and just delete the folders, etc. manually I began
receiving "Access Denied" errors, but not from ACLs (oddly I could
delete them from Windows Explorer).  This lead me to believe resources
were still being locked.

Sure enough, running "DeleteProfile" from a cold or warm start worked
like a charm.

I'm not sure why resources would still be locked after a user acccount
has logged off, but it may have to do with some of the Terminal
Services technology borrowed in Windows XP to support the "Quick User
Swithing" and faster log ons features.

If any one ever comes up with a way to run this function after a
profile has been loaded, I'd interested to know how they did it.  For
now, the best advice I can give is to run all of this type of code
during a cold or warm start.

Chris

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.