很简单的一个测试程序,就是搞不明白,哎
。。
using System.Runtime.InteropServices;
。。
[DllImport("kernel32.dll", EntryPoint="GetComputerName")]
public static extern bool GetComputerName(string computername,uint size);
string computername1=new string('a',60);//此段程序我放在一个按键点击事件响应子程序中了
uint size1=new uint();
size1=60;
GetComputerName(computername1,size1);//此处调试时老提示未将对象引用设置到对象的实例异常
textBox1.Text=computername1.ToString();
我把string改为字符串类型也还是同样的错误,跟踪变量computername1和size1数据也都没错是60和60个字符a啊,哎,真搞败了,怎么也没搞明白,哪位高人指点指点指点,不胜感激!
。。
using System.Runtime.InteropServices;
。。
[DllImport("kernel32.dll", EntryPoint="GetComputerName")]
public static extern bool GetComputerName(string computername,uint size);
string computername1=new string('a',60);//此段程序我放在一个按键点击事件响应子程序中了
uint size1=new uint();
size1=60;
GetComputerName(computername1,size1);//此处调试时老提示未将对象引用设置到对象的实例异常
textBox1.Text=computername1.ToString();
我把string改为字符串类型也还是同样的错误,跟踪变量computername1和size1数据也都没错是60和60个字符a啊,哎,真搞败了,怎么也没搞明白,哪位高人指点指点指点,不胜感激!
解决方案 »
- 这个怎么简单化 用什么方法呢
- webbrowser性能问题
- table里面的动态控件取值问题???
- 问个字符串的问题,高手进...
- treeview问题,新手问题,请大家路过的帮看下
- E:\Class1.cs(13): The type or namespace name 'w' does not exist in the class or namespace 'x.y' (are y
- ~~~~~~~~~~~~~~~~求TFTP SERVER 原代码~~~~~~~~~~~~~
- c# 随机函数
- 高分求救!!!!!!水晶报表中分页计算的问题
- Visual C# .NET 2003的改变
- 关于eWebEditor的问题!(为什么我的内容不能添加到数据库去)
- 象一般的扑克牌游戏大厅是怎样实现的
private static extern int GetComputerName (string lpBuffer, int nSize);
搞 web 很有前途吗?
===========my blog================http://www.monocn.com==================================
uint size1=new uint();
size1=60;
GetComputerName(ref computername1,size1);//此处调试时老提示未将对象引用设置到对象的实例异常
textBox1.Text=computername1.ToString();
{
...
string str;
p(ref str);
...
}
{
//private const int MAX_COMPUTERNAME_LENGTH = 31; [DllImport("Kernel32")]
private static extern unsafe bool GetComputerName(byte* lpBuffer,long* nSize); [STAThread]
static void Main()
{
byte[] buffor = new byte[512];
long size = buffor.Length;
unsafe
{
long* pSize = &size;
fixed (byte* pBuffor = buffor)
{
GetComputerName(pBuffor,pSize);
}
}
System.Text.Encoding textEnc = new System.Text.ASCIIEncoding();
System.Console.WriteLine("Computer name: {0}",textEnc.GetString(buffor));
}
}
{
private const int MAX_COMPUTERNAME_LENGTH = 31; [DllImport("Kernel32")]
static extern bool GetComputerName(byte[] lpBuffer,Int32[] nSize); /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
byte[] buffer = new byte[MAX_COMPUTERNAME_LENGTH + 1];
Int32[] size = new Int32[1]{MAX_COMPUTERNAME_LENGTH + 1}; GetComputerName(buffer,size); System.Text.Encoding textEnc = new System.Text.ASCIIEncoding();
System.Console.WriteLine("Computer name: {0}",textEnc.GetString(buffer)); System.Console.ReadLine();
}
}
把string 改为stringbuilder,例如:
[DllImport("kernel32.dll", EntryPoint="GetComputerName")]
public static extern bool GetComputerName(StringBuilder computername,uint size);
StringBuilder strComName = new StringBuilder( 256 );//Define a string which length is 256
GetCompterName( strComName, 256 );
[DllImport("kernel32", CharSet = CharSet.Unicode)]
public static extern int GetComputerName(
string lpBuffer,
ref int nSize
);
private void button3_Click(object sender, EventArgs e)
{
int ok = 256;
string aa="";
int a = GetComputerName( aa, ref ok);
MessageBox.Show(aa.Trim(), "机器名字是:");
}
}
字符串缓冲区 .NET 中的字符串类型是不可改变的类型,这意味着它的值将永远保持不变。对于要将字符串值复制到字符串缓冲区的函数,字符串将无效。这样做至少会破坏由封送拆收器在转换字符串时创建的临时缓冲区;严重时会破坏托管堆,而这通常会导致错误的发生。无论哪种情况都不可能获得正确的返回值。 要解决此问题,我们需要使用其他类型。StringBuilder 类型就是被设计为用作缓冲区的,我们将使用它来代替字符串。
经典!
用它山之石的Byte[]或愚翁的方法都可以解决字符串缓冲区的问题。