[英]How to call NetUserModalsGet() from C#.NET?
EDIT: followup at NetUserModalsGet() returns strings incorrectly for C#.NET 编辑: NetUserModalsGet()的后续操作为C#.NET错误地返回字符串
I'm struggling with the DLL declarations for this function: 我为此功能的DLL声明而苦苦挣扎:
NET_API_STATUS NetUserModalsGet(
__in LPCWSTR servername,
__in DWORD level,
__out LPBYTE *bufptr
);
(Reference: http://msdn.microsoft.com/en-us/library/aa370656%28VS.85%29.aspx ) (参考: http : //msdn.microsoft.com/en-us/library/aa370656%28VS.85%29.aspx )
I tried this: 我尝试了这个:
private string BArrayToString(byte[] myArray)
{
string retVal = "";
if (myArray == null)
retVal = "Null";
else
{
foreach (byte myByte in myArray)
{
retVal += myByte.ToString("X2");
}
}
return retVal;
}
...
[DllImport("netapi32.dll")]
public static extern int NetUserModalsGet(
string servername,
int level,
out byte[] bufptr
);
[DllImport("netapi32.dll")]
public static extern int NetApiBufferFree(
byte[] bufptr
);
...
int retVal;
byte[] myBuf;
retVal = NetUserModalsGet("\\\\" + tbHost.Text, 0, out myBuf);
myResults.Text += String.Format("retVal={0}\nBuffer={1}\n", retVal, BArrayToString(myBuf));
retVal = NetApiBufferFree(myBuf);
I get a return value of 1231 (Network Location cannot be reached) no matter if I use an IP address or a NetBIOS name of a machine that's undoubtedly online, or even my own. 无论使用的是无疑是在线的机器的IP地址或NetBIOS名称,还是什至是我自己的机器,我的返回值都为1231(无法到达网络位置)。 On edit: this happens even if I don't put a "\\\\" in front of the hostname. 编辑时:即使我没有在主机名前加上“ \\\\”,也会发生这种情况。
I'm doing things wrong, I know, and let's not even get started on how to declare that blasted return buffer (which can have a number of different lengths, ewww). 我知道我做错了,我们什至不开始介绍如何声明爆发的返回缓冲区(可以有许多不同的长度,ewww)。
From pinvoke.net (they also have some sample code on how to use it): 从pinvoke.net (他们还有一些有关如何使用它的示例代码):
[DllImport("netapi32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
static extern uint NetUserModalsGet(
string server,
int level,
out IntPtr BufPtr);
It would be better to use System.Text.Encoding.ASCII.GetBytes(somestring_param)
and System.Text.Encoding.ASCII.GetString(byte_array)
instead of your own routine BArrayToString
. 最好使用System.Text.Encoding.ASCII.GetBytes(somestring_param)
和System.Text.Encoding.ASCII.GetString(byte_array)
而不是您自己的例程BArrayToString
。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.