Click here to Skip to main content
16,013,322 members
Home / Discussions / C#
   

C#

 
GeneralRe: Drive Share account Pin
Heath Stewart19-Jun-04 7:03
protectorHeath Stewart19-Jun-04 7:03 
GeneralRe: Drive Share account Pin
Guinness4Strength19-Jun-04 11:15
Guinness4Strength19-Jun-04 11:15 
GeneralRe: Drive Share account Pin
Heath Stewart20-Jun-04 9:50
protectorHeath Stewart20-Jun-04 9:50 
GeneralRe: Drive Share account Pin
Guinness4Strength21-Jun-04 5:22
Guinness4Strength21-Jun-04 5:22 
GeneralRe: Drive Share account Pin
Heath Stewart21-Jun-04 5:26
protectorHeath Stewart21-Jun-04 5:26 
GeneralRe: Drive Share account Pin
Heath Stewart21-Jun-04 5:27
protectorHeath Stewart21-Jun-04 5:27 
GeneralRe: Drive Share account Pin
Guinness4Strength21-Jun-04 5:50
Guinness4Strength21-Jun-04 5:50 
GeneralRe: Drive Share account Pin
Heath Stewart21-Jun-04 6:26
protectorHeath Stewart21-Jun-04 6:26 
Because the last param is NetUseGetInfo is LPBYTE*, which is a typedef for unsigned char **. If it was just LPBYTE, your little shortcut of using ref structUSE_INFO_2 would work.

Instead, you have to pass an IntPtr (by the way, it should just be an out param, not a ref, though it doesn't hurt in this case) then use Marshal.PtrToStructure like so:
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

internal class GetInfo
{
  static void Main(string[] args)
  {
    if (args.Length != 1)
    {
      Console.Error.WriteLine("You must specify a drive letter.");
      Environment.Exit(1);
    }

    if (Environment.OSVersion.Platform != PlatformID.Win32NT)
    {
      Console.Error.WriteLine("Supported only on Windows NT.");
      Environment.Exit(2);
    }
 
    // Pay attention here
    IntPtr ptr = IntPtr.Zero;
    if (0 == NetUseGetInfo(null, args[0], 2, out ptr))
    {
      UseInfo2 info = (UseInfo2)Marshal.PtrToStructure(ptr,
        typeof(UseInfo2));

      Console.WriteLine("Username: " + info.username);
      Console.WriteLine("Domain: " + info.domainname);

      NetApiBufferFree(ptr);
    }
 
    else
    {
      int error = Marshal.GetLastWin32Error();
      Console.Error.WriteLine("Error " + error);
      Environment.Exit(error);
    }
  }

  [DllImport("netapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
  public static extern uint NetUseGetInfo
  (
    string server,
    string drive,
    uint level,
    out IntPtr info
  );

  [DllImport("netapi32.dll")]
  public static extern uint NetApiBufferFree(IntPtr ptr);
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
internal struct UseInfo2
{
  public string local;
  public string remote;
  public string password;
  public uint status;
  public uint type;
  public uint refcount;
  public uint usecount;
  public string username;
  public string domainname;
}


 

Microsoft MVP, Visual C#
My Articles
GeneralRe: Drive Share account Pin
Guinness4Strength21-Jun-04 8:13
Guinness4Strength21-Jun-04 8:13 
QuestionHow repaint the color of ColumnHead in ListView? Pin
fu019-Jun-04 1:31
fu019-Jun-04 1:31 
AnswerRe: How repaint the color of ColumnHead in ListView? Pin
Heath Stewart19-Jun-04 6:58
protectorHeath Stewart19-Jun-04 6:58 
Generalform at runtime Pin
Member 114126618-Jun-04 22:45
Member 114126618-Jun-04 22:45 
GeneralRe: form at runtime Pin
Heath Stewart19-Jun-04 7:00
protectorHeath Stewart19-Jun-04 7:00 
GeneralYahoo and C# Pin
MeterMan18-Jun-04 21:55
MeterMan18-Jun-04 21:55 
GeneralRe: Yahoo and C# Pin
Nick Parker19-Jun-04 4:41
protectorNick Parker19-Jun-04 4:41 
GeneralRe: Yahoo and C# Pin
MeterMan19-Jun-04 6:44
MeterMan19-Jun-04 6:44 
GeneralRe: Yahoo and C# Pin
Heath Stewart19-Jun-04 6:50
protectorHeath Stewart19-Jun-04 6:50 
GeneralRe: Yahoo and C# Pin
Heath Stewart19-Jun-04 6:48
protectorHeath Stewart19-Jun-04 6:48 
GeneralRe: Yahoo and C# Pin
MeterMan19-Jun-04 10:14
MeterMan19-Jun-04 10:14 
GeneralRe: Yahoo and C# Pin
eggie519-Jun-04 15:24
eggie519-Jun-04 15:24 
GeneralRe: Yahoo and C# Pin
eggie519-Jun-04 15:51
eggie519-Jun-04 15:51 
GeneralRe: Yahoo and C# Pin
MeterMan19-Jun-04 16:41
MeterMan19-Jun-04 16:41 
GeneralRe: Yahoo and C# Pin
eggie519-Jun-04 16:46
eggie519-Jun-04 16:46 
GeneralRe: Yahoo and C# Pin
Heath Stewart20-Jun-04 9:46
protectorHeath Stewart20-Jun-04 9:46 
GeneralRe: Yahoo and C# Pin
MeterMan20-Jun-04 10:43
MeterMan20-Jun-04 10:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.