未将对象引用设置到对象的实例,怎么解决??

晒屁屁 2002-10-18 08:24:20
int re=0;
string buf=new String('\0',1024);
int siz=1024;
re=WinApi.GetComputerName(buf,siz);//在这句时出错
buf.Substring(0,buf.IndexOf('\0'));
Debug.WriteLine(buf);
...全文
61 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fushuming 2002-10-18
  • 打赏
  • 举报
回复
gz
saucer 2002-10-18
  • 打赏
  • 举报
回复
1. you need to declare GetComputerName as static
2. GetComputerName will fill the name in the first parameter you pass in, so you need to give it a StringBuilder or a byte[]
3. it also changes the last parameter to let you know how many long the name is
slayersun 2002-10-18
  • 打赏
  • 举报
回复
等待答案
晒屁屁 2002-10-18
  • 打赏
  • 举报
回复
试成功了,能讲讲为什么吗,还有为什么经常会有这种错误呢?我是指在别的情况下,这种错误一般是怎么回事呢?谢谢~
saucer 2002-10-18
  • 打赏
  • 举报
回复
try

using System;
using System.Text;
using System.Runtime.InteropServices;

[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
[return:MarshalAs(UnmanagedType.Bool)]
public static extern bool GetComputerName(
StringBuilder lpBuffer,
[MarshalAs(UnmanagedType.U4)] ref int lpnSize);

int re=0;
StringBuilder buf = new StringBuilder("",1024);
int siz=1024;
bool bOK = GetComputerName(buf,ref siz);
Debug.WriteLine(buf.ToString());

111,119

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧