很简单的一个测试程序,就是搞不明白,哎
。。
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啊,哎,真搞败了,怎么也没搞明白,哪位高人指点指点指点,不胜感激!

解决方案 »

  1.   

    [DllImport("kernel32.dll", EntryPoint="GetComputerNameA")]
    private static extern int GetComputerName (string lpBuffer, int nSize);
      

  2.   

    哎,怎么C#做winform的人这么少~~
    搞 web 很有前途吗?  
    ===========my blog================http://www.monocn.com==================================
      

  3.   

    都习惯用VC++做WINFORM,这个例子我在VC++.net和C#.net试都是这个结果,看了winbase.h里的函数原型也是搞不懂
      

  4.   

    string computername1;//此段程序我放在一个按键点击事件响应子程序中了
    uint size1=new uint();
    size1=60;
    GetComputerName(ref computername1,size1);//此处调试时老提示未将对象引用设置到对象的实例异常
    textBox1.Text=computername1.ToString();
      

  5.   

    改了两个地方第一:没有初始化new第二:调用的时候加上ref你测试一下看看对不对
      

  6.   

    不行啊,提示无法从“ref string"转换为"string"
      

  7.   

    把 string 用 System.Text.StringBuilder 或者 ref System.Text.StringBuilder 代替看看。函数参数加了 ref,调用时变量前也要加 ref,如:void p(ref string str) { ... }void f()
    {
    ...
    string str;
    p(ref str);
    ...
    }
      

  8.   

    class Class1
    {
            
    //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));
    }
    }
      

  9.   

    如果不想使用unsafe,可以使用如下代码 class Class1
    {
            
    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();
    }
    }
      

  10.   

    回复楼上的两位,我根据你们的试了一下,是GetComputerName()函数的第二个参数,即nSize导致的问题,第一个参数没问题,但是用了它山之石的解决方法,倒是没报错了,但是buffer返回来显示的是System.byte[],还是没搞定。。,你用的Int32[]应该就是成定义32位符号数的数组了吧?要传递的一个32位无符号数
      

  11.   

    其中nSize的问题用楼上说的加ref方法能解决,不会报错了,但是返回来的也是System.byte[],或者System.char[],是返回值不对,还是。。我用的textBox1->Text=buffer.Tostring()来显示的
      

  12.   

    你在声明函数的时候,参数的类型不对
    把string 改为stringbuilder,例如:
    [DllImport("kernel32.dll", EntryPoint="GetComputerName")]
    public static extern bool GetComputerName(StringBuilder computername,uint size);
      

  13.   

    调用的时候,如下:
    StringBuilder strComName = new StringBuilder( 256 );//Define a string which length is 256
    GetCompterName( strComName, 256 );
      

  14.   

    }
            [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(), "机器名字是:");
                }
            }
      

  15.   

    哈哈,谢谢各位了,我也刚研究透了,终于搞定了,谢谢它山之石!谢谢愚翁!我彻底想明白了。看看这段话,最关键的就是.net中的字符串缓冲区问题,而不是字符类型转换的问题,兜了一大圈,一开始我的思路方向就错了,呵呵。
    字符串缓冲区  .NET 中的字符串类型是不可改变的类型,这意味着它的值将永远保持不变。对于要将字符串值复制到字符串缓冲区的函数,字符串将无效。这样做至少会破坏由封送拆收器在转换字符串时创建的临时缓冲区;严重时会破坏托管堆,而这通常会导致错误的发生。无论哪种情况都不可能获得正确的返回值。   要解决此问题,我们需要使用其他类型。StringBuilder 类型就是被设计为用作缓冲区的,我们将使用它来代替字符串。
        经典!
        用它山之石的Byte[]或愚翁的方法都可以解决字符串缓冲区的问题。