请精通C#与VB.net的大虾帮忙翻译一下
szch 2003-03-10 09:08:21 翻译完请一并帮助测试一下结果与原版是否一致,我已折腾了两个星期了。
先谢谢了。
using System;
using System.Runtime.InteropServices;
namespace EnumNetPcs
{
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public class NetResource
{
public UInt32 Scope;
public UInt32 RType;
public UInt32 Display;
public UInt32 Usage;
[MarshalAs(UnmanagedType.LPTStr)]
public string LocalName;
[MarshalAs(UnmanagedType.LPTStr)]
public string RemoteName;
[MarshalAs(UnmanagedType.LPTStr)]
public string Comment;
[MarshalAs(UnmanagedType.LPTStr)]
public string Provider;
}
class Test
{
[DllImport("mpr.dll", CharSet=CharSet.Auto)]
private static extern Int32 WNetOpenEnum
(
UInt32 scope,
UInt32 type,
UInt32 usage,
[In] NetResource rsc,
out IntPtr handle
);
[DllImport("mpr.dll", CharSet=CharSet.Auto)]
private static extern Int32 WNetEnumResource
(
IntPtr handle,
ref Int32 count,
IntPtr buffer,
ref Int32 size
);
[DllImport("mpr.dll", CharSet=CharSet.Auto)]
private static extern Int32 WNetCloseEnum(IntPtr handle);
[STAThread]
static void Main()
{
Int32 result = -1;
IntPtr handle = IntPtr.Zero;
result = WNetOpenEnum(0x00000005, 0x00000000, 0x00000004, null, out handle);
if(result != 0) return;
Int32 entries = 0;
Int32 memory = 16300;
IntPtr buffer = Marshal.AllocHGlobal(memory);
Type type = typeof(NetResource);
Int32 size = Marshal.SizeOf(type);
NetResource rsc = null;
Int32 currect = 0;
while(true)
{
entries = -1;
memory = 16300;
result = WNetEnumResource(handle, ref entries, buffer, ref memory);
if(result >= 259)
{
// Finished.
break;
}
else if(result != 0 || entries < 1)
{
// Failed.
break;
}
currect = (Int32) buffer;
for(Int32 i = 0; i < entries; i++)
{
rsc = (NetResource) Marshal.PtrToStructure((IntPtr) currect, type);
if(rsc != null)
if(rsc.RemoteName != null)
if(rsc.RemoteName.Length > 2)
if(rsc.RemoteName.Substring(0,2).Equals(@"\\"))
Console.WriteLine("RemoteName: {0}", rsc.RemoteName.Substring(2));
currect += size;
}
}
Marshal.FreeHGlobal(buffer);
result = WNetCloseEnum(handle);
Console.Read();
}
}
}