参考
http://www.cnblogs.com/liefeng123/articles/533743.html

解决方案 »

  1.   

    又没有指针
    ------------
    这句是不对的哦~
    http://www.cnblogs.com/panjun-Donet/archive/2008/08/01/1258312.html
      

  2.   

    http://topic.csdn.net/u/20080108/11/161ac971-967e-43bb-a76b-fdbba3c21b2e.html
      

  3.   

    http://forums.devshed.com/net-development-87/lptstr-in-c-to-stringbuilder-in-c-problem-520117.html
    http://topic.csdn.net/u/20070716/20/d0273e40-9dbd-47dd-ab39-e92d1b1d024a.html
    http://topic.csdn.net/t/20060604/16/4799496.html
      

  4.   

    也就是说这样也不行吗?===》[MarshalAs(UnmanagedType.LPTStr)]string targetString
      

  5.   

    LPTSTR对应于StringBuilder类型,可变的字符串,通常都可以,可能你这是特殊情况~
      

  6.   


    msdn查的...不要在意说法啦...
      

  7.   

    不行,这样写还是不能访问第一个\0之后的内容,说OutOfRangeException
      

  8.   

    谢谢楼上的关于指针的文章,我在C#中用指针尝试过了,发现不论是string(包括MarshalAs LPTSTR的string)还是StringBuilder,作为参数传到这个API中,返回的都只是第一个\0之前的内容,第一个\0之后的内容其实根本没有返回,所以即使用指针也取不到。
    看来唯一的办法是写个C++类封装一下了。
    Anyway,谢谢各位!
      

  9.   

    msdn查的...不要在意说法啦...恰是你的说法不对引起的, \0 是字符串分割符你需要用自己的算法, 把传入的东西当作字节数组来自己分析, 标准方式的各种字符串函数只会在第一个 \0 下停下来.因此问你说法来自何处.
      

  10.   

    对应StringBuilder,String,Byte[],char[]都行,后两者在知道长度的情况下是没问题的,你这没有,但应该也行,写法如下:StringBuilder:
    [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="SCardListReadersW")]
    public static extern  int SCardListReadersW(uint hContext, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string mszGroups, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] System.Text.StringBuilder mszReaders, ref uint pcchReaders) ;string:
    [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="SCardListReadersW")]
    public static extern  int SCardListReadersW(uint hContext, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string mszGroups,
    [System.Runtime.InteropServices.OutAttribute()][System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string mszReaders, ref uint pcchReaders) ;"<Unknown>", 换成你的DLL希望对你有用。
      

  11.   

    有点乱哦,我改改:
    对应StringBuilder,String,Byte[],char[]都行,后两者在知道长度的情况下是没问题的,你这没有,但应该也行,写法如下:StringBuilder:
    [DllImport("<Unknown>", EntryPoint="SCardListReadersW")]
    public static extern int SCardListReadersW(uint hContext, [In,MarshalAs(UnmanagedType.LPWStr)] string mszGroups, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder mszReaders, ref uint pcchReaders) ;string:
    [DllImport("<Unknown>", EntryPoint="SCardListReadersW")]
    public static extern int SCardListReadersW(uint hContext, [In, MarshalAs(UnmanagedType.LPWStr)] string mszGroups,
    [Out,MarshalAs(UnmanagedType.LPWStr)]string mszReaders, ref uint pcchReaders) ;"<Unknown>", 换成你的DLL
    In,可以省略希望对你有用。
      

  12.   

    LPTSTR 对应 System.String 或 System.StringBuilder , 用 ANSI 修饰。
      

  13.   

    LPTSTR 对应  System.StringBuilder 的我用过,可以的
      

  14.   

    stringbuilder,或者你用byte[]也行~