如题

解决方案 »

  1.   

    static void Main(string[] args)
     2  {
     3      string str = "Hello world";
     4      IntPtr bstr = Marshal.StringToBSTR(str);
     5      IntPtr p = bstr;
     6      char * c;
     7      while (p != (IntPtr)((long)bstr + sizeof(char) * str.Length))
     8      {
     9          c = (char*)p;
    10          Console.WriteLine(*c);
    11          p = (IntPtr)((long)p + sizeof(char));
    12      }
    13      Marshal.FreeBSTR(bstr);
    14  }
      

  2.   

    TO:avisnet(第十维度) 编译通不过,提示
    错误 1 指针和固定大小缓冲区只能在不安全的上下文中使用 E:\Dicos_code\PDA\code\PrintTest\PrintTest\POSPrint.cs 92 15 PrintTest
      

  3.   

    project properties->build->allow unsafe codestatic void Main(string[] args)
    {
    string str = "Hello world";
    IntPtr bstr = Marshal.StringToBSTR(str);
    IntPtr p = bstr;
    unsafe
    {
    char* c;
    while(p != (IntPtr)((long)bstr + sizeof(char) * str.Length))
    {
    c = (char*)p;
    Console.WriteLine(*c);
    p = (IntPtr)((long)p + sizeof(char));
    }
    }
    Marshal.FreeBSTR(bstr);
    }