NetUserGetInfo - problem with filling dir. path

P

piotrek

Hi all.
I'am forced to ask you for help.Why the code is not working the way it
should.
I wanna fill USER_INFO_1 struct. to obrain information about user windows
account.
Everything seems to work ok exepts password and directory fields. They
remain null;
The code explains the rest:

////CODE


....
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct USER_INFO_1
{
public string usri1_name;
public string usri1_password;
public int usri1_password_age;
public int usri1_priv;
public string usri1_home_dir;
public string comment;
public int usri1_flags;
public string usri1_script_path;
}


[DllImport("Netapi32.dll")]
extern static int
NetUserGetInfo([MarshalAs(UnmanagedType.LPWStr)]
string servername, [MarshalAs(UnmanagedType.LPWStr)] string

username, int level, out IntPtr bufptr);


[DllImport("Netapi32.dll")]
extern static int
NetUserSetInfo([MarshalAs(UnmanagedType.LPWStr)]
string servername, [MarshalAs(UnmanagedType.LPWStr)] string

username, int level, ref USER_INFO_1 buf, int error);


.....


public void test()
{
IntPtr bufPtr;
USER_INFO_1 User = new USER_INFO_1();

if (NetUserGetInfo(null, "test", 1, out bufPtr) != 0)
{
MessageBox.Show("Error Getting User Info", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

User = (USER_INFO_1)Marshal.PtrToStructure(bufPtr,
typeof(USER_INFO_1));
MessageBox.Show("Users Name: " + User.usri1_name + " Users
Comments: " + User.comment + " Dir: " + User.usri1_home_dir );
}


/// END OF CODE

Thanks for any suggestions.
Piotr Kolodziej
 
W

Willy Denoyette [MVP]

First you can't retrieve the password of a user account in Windows(check the
docs. for NetUserGetInfo), and if the home_dir is null, I guess it's because
there is no explicit homedir defined for this account.
Second, why are you calling low level C API's when managed classes in
System.DirectoryServices are available to achieve the same results?

Willy.
 
P

piotrek

Willy Denoyette said:
First you can't retrieve the password of a user account in Windows(check
the docs. for NetUserGetInfo), and if the home_dir is null, I guess it's
because there is no explicit homedir defined for this account.
Second, why are you calling low level C API's when managed classes in
System.DirectoryServices are available to achieve the same results?

Willy.

Its easy to say for MVP :]
Could you give me a tip whitch class should i apply to get infos, about user
and his rights?
i'd appreciate it
 
W

Willy Denoyette [MVP]

piotrek said:
Willy Denoyette said:
First you can't retrieve the password of a user account in Windows(check
the docs. for NetUserGetInfo), and if the home_dir is null, I guess it's
because there is no explicit homedir defined for this account.
Second, why are you calling low level C API's when managed classes in
System.DirectoryServices are available to achieve the same results?

Willy.

Its easy to say for MVP :]
Could you give me a tip whitch class should i apply to get infos, about
user and his rights?
i'd appreciate it

There are two namespaces that can be used to get user account info,
System.Management and System.Directory services, the first wraps WMI while
the latter wraps ADSI.

To retrieve user properties using DirectoryServices you need to create an
instance of the DirectoryEntry class specifying "WinNT" as ADSI provider and
the machine name to bind to.
Following sample retrieves all properties for administrator on machine
"piotr", some properties are array types so you will have to convert them to
their natural representation depending on the property type.
For more info search the ADSI docs. and the FCL on MSDN.


using System;
using System.DirectoryServices;
using System.Runtime.InteropServices;
class App {
public static void Main() {
ListUserProperties("Administrator");
}
private static void ListUserProperties(string UserName) {
using(DirectoryEntry groupEntry = new DirectoryEntry("WinNT://piotr/" +
UserName))
{
PropertyCollection pcoll = groupEntry.Properties;
foreach(string sc in pcoll.PropertyNames)
{
Console.WriteLine(sc + "\t\t" + pcoll[sc].Value);
}
}
}
}

Willy.
 
L

lugionline

WMI failed when you disable the WMI Service,

But NetUserGetInfo will still done it's work.
 
W

Willy Denoyette [MVP]

Same goes for lsass service if you disable it NetUserGetInfo will fail also.
WMI is a system service it shouldn't be disabled, there are more things that
will fail when you disable the WMI service,

Willy.
 
G

Guest

I have been trying both NetUserGetInfo (in .NET) and now I've tried the .NET
approach that you have outlined here.

In both cases, the fullname property comes back with my username. I tried
the NetUserGetInfo function in a simple VS 6 Win32 console app and the full
name comes back with my full name and not my username.

In the case of the former, I figure that I was just marshalling something
wrong by misdeclaring the USER_INFO structure, but now I see that the
System.Directory library isn't even working.

Do you think I might still be missing something?

Willy Denoyette said:
piotrek said:
Willy Denoyette said:
First you can't retrieve the password of a user account in Windows(check
the docs. for NetUserGetInfo), and if the home_dir is null, I guess it's
because there is no explicit homedir defined for this account.
Second, why are you calling low level C API's when managed classes in
System.DirectoryServices are available to achieve the same results?

Willy.

Its easy to say for MVP :]
Could you give me a tip whitch class should i apply to get infos, about
user and his rights?
i'd appreciate it

There are two namespaces that can be used to get user account info,
System.Management and System.Directory services, the first wraps WMI while
the latter wraps ADSI.

To retrieve user properties using DirectoryServices you need to create an
instance of the DirectoryEntry class specifying "WinNT" as ADSI provider and
the machine name to bind to.
Following sample retrieves all properties for administrator on machine
"piotr", some properties are array types so you will have to convert them to
their natural representation depending on the property type.
For more info search the ADSI docs. and the FCL on MSDN.


using System;
using System.DirectoryServices;
using System.Runtime.InteropServices;
class App {
public static void Main() {
ListUserProperties("Administrator");
}
private static void ListUserProperties(string UserName) {
using(DirectoryEntry groupEntry = new DirectoryEntry("WinNT://piotr/" +
UserName))
{
PropertyCollection pcoll = groupEntry.Properties;
foreach(string sc in pcoll.PropertyNames)
{
Console.WriteLine(sc + "\t\t" + pcoll[sc].Value);
}
}
}
}

Willy.
 
G

Guest

Nevermind..

The DirectoryServices technique does seem to be working. I think I wasn't
entering domain and account information properly.

Thank you for the original post. It was very helpful.

Alfetta159 said:
I have been trying both NetUserGetInfo (in .NET) and now I've tried the .NET
approach that you have outlined here.

In both cases, the fullname property comes back with my username. I tried
the NetUserGetInfo function in a simple VS 6 Win32 console app and the full
name comes back with my full name and not my username.

In the case of the former, I figure that I was just marshalling something
wrong by misdeclaring the USER_INFO structure, but now I see that the
System.Directory library isn't even working.

Do you think I might still be missing something?

Willy Denoyette said:
piotrek said:
First you can't retrieve the password of a user account in Windows(check
the docs. for NetUserGetInfo), and if the home_dir is null, I guess it's
because there is no explicit homedir defined for this account.
Second, why are you calling low level C API's when managed classes in
System.DirectoryServices are available to achieve the same results?

Willy.


Its easy to say for MVP :]
Could you give me a tip whitch class should i apply to get infos, about
user and his rights?
i'd appreciate it

There are two namespaces that can be used to get user account info,
System.Management and System.Directory services, the first wraps WMI while
the latter wraps ADSI.

To retrieve user properties using DirectoryServices you need to create an
instance of the DirectoryEntry class specifying "WinNT" as ADSI provider and
the machine name to bind to.
Following sample retrieves all properties for administrator on machine
"piotr", some properties are array types so you will have to convert them to
their natural representation depending on the property type.
For more info search the ADSI docs. and the FCL on MSDN.


using System;
using System.DirectoryServices;
using System.Runtime.InteropServices;
class App {
public static void Main() {
ListUserProperties("Administrator");
}
private static void ListUserProperties(string UserName) {
using(DirectoryEntry groupEntry = new DirectoryEntry("WinNT://piotr/" +
UserName))
{
PropertyCollection pcoll = groupEntry.Properties;
foreach(string sc in pcoll.PropertyNames)
{
Console.WriteLine(sc + "\t\t" + pcoll[sc].Value);
}
}
}
}

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top