C#转换为VB.Net的问题,似乎VB.Net无法实现,救命啊!!!

roger_xiong 2009-03-12 10:59:39
C#中定义一个 string 类型,new string有8个重载,而 VB.net 中的New String只有3个重载,导致一些C#写的代码根本无法转换为VB.net,比如:

string s = new string((sbyte*)p, 0, len, encoding);

用工具将上面的代码转换以后是这样的:

Dim s As New String(CSByte(p), 0, len, encoding)


可是编译不通过,提示错误。


有没有哪位大大帮忙啊!!!!!!救命啊!!!!!!
...全文
178 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
roger_xiong 2009-03-14
  • 打赏
  • 举报
回复
继续顶~~~~~
syc958 2009-03-14
  • 打赏
  • 举报
回复
用byref试一下
wanghui0380 2009-03-14
  • 打赏
  • 举报
回复
恩,只能用Marshal 类转了

看你那个函数本身的原型,不就是string类的构造函数嘛!
我看了一下msdn,上面是这样滴

Visual Basic(声明)
Visual Basic 不支持会使用或返回不安全类型的 API。

Visual Basic(用法)
Visual Basic 不支持会使用或返回不安全类型的 API。

不过我很纳闷,既然就是net本身的string,又不啥第三方的东西,为啥一定你要用这个构造,直接默认构造,然后赋值就是了。
如果非要用这个构造,只能用Marshal类封送到托管对象上玩,有多此一举的嫌疑说

dai78 2009-03-14
  • 打赏
  • 举报
回复
用转换工具试试
roger_xiong 2009-03-13
  • 打赏
  • 举报
回复
没有结果吗???呜呜呜
我姓区不姓区 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 roger_xiong 的回复:]
不好意思,上面贴的代码错了,正确的代码是:

00253            public static unsafe string HeapToString (IntPtr p, Encoding encoding)
00254            {
00255                  if (encoding == null)
00256                        return Marshal.PtrToStringAnsi (p);
00257
00258                  if (p == IntPtr.Zero)
00259                        return null;
00260
00261                 …
[/Quote]
我再试试……
roger_xiong 2009-03-12
  • 打赏
  • 举报
回复
StringToHeap用 http://www.developerfusion.com/tools/convert/csharp-to-vb/ 转换以后是正确的,没有用到什么特殊的代码。

但是 HeapToString 就不行了。

roger_xiong 2009-03-12
  • 打赏
  • 举报
回复
不好意思,上面贴的代码错了,正确的代码是:

00253 public static unsafe string HeapToString (IntPtr p, Encoding encoding)
00254 {
00255 if (encoding == null)
00256 return Marshal.PtrToStringAnsi (p);
00257
00258 if (p == IntPtr.Zero)
00259 return null;
00260
00261 // This assumes a single byte terminates the string.
00262
00263 int len = 0;
00264 while (Marshal.ReadByte (p, len) != 0)
00265 checked {++len;}
00266
00267 string s = new string ((sbyte*) p, 0, len, encoding);
00268 len = s.Length;
00269 while (len > 0 && s [len-1] == 0)
00270 --len;
00271 if (len == s.Length)
00272 return s;
00273 return s.Substring (0, len);
00274 }
我姓区不姓区 2009-03-12
  • 打赏
  • 举报
回复
看看能用不

Function StringToHeap(ByVal s As String, ByVal encoding As System.Text.Encoding) As IntPtr
If encoding Is Nothing Then
Return ""
End If
Dim min_byte_count As Integer = encoding.GetMaxByteCount(1)
Dim copy() As Char = s.ToCharArray()
Dim marshal(encoding.GetByteCount(copy) + min_byte_count) As Byte
Dim bytes_copied As Integer = encoding.GetBytes(copy, 0, copy.Length, marshal, 0)
If bytes_copied <> marshal.Length - min_byte_count Then
Throw New NotSupportedException("encoding.GetBytes() doesn't equal encoding.GetByteCount()!")
End If
Dim mem As IntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(marshal.Length)
If mem = IntPtr.Zero Then
Throw New OutOfMemoryException()
End If
Dim copied As Boolean = False
Try
System.Runtime.InteropServices.Marshal.Copy(marshal, 0, mem, marshal.Length)
copied = True
Finally
If copied = False Then
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(mem)
End If
End Try
Return mem
End Function
我姓区不姓区 2009-03-12
  • 打赏
  • 举报
回复
我试试写一个看看
我姓区不姓区 2009-03-12
  • 打赏
  • 举报
回复
为什么要用到这个重载
wuyi8808 2009-03-12
  • 打赏
  • 举报
回复
建议把这个用C#写的方法 StringToHeap() 单独编译成.dll文件,然后在 VB.NET 中调用之。
roger_xiong 2009-03-12
  • 打赏
  • 举报
回复
这个函数的作用是把一个heap转换为string

00222 public static IntPtr StringToHeap (string s, Encoding encoding)
00223 {
00224 if (encoding == null)
00225 return Marshal.StringToCoTaskMemAnsi (s);
00226
00227 int min_byte_count = encoding.GetMaxByteCount(1);
00228 char[] copy = s.ToCharArray ();
00229 byte[] marshal = new byte [encoding.GetByteCount (copy) + min_byte_count];
00230
00231 int bytes_copied = encoding.GetBytes (copy, 0, copy.Length, marshal, 0);
00232
00233 if (bytes_copied != (marshal.Length-min_byte_count))
00234 throw new NotSupportedException ("encoding.GetBytes() doesn't equal encoding.GetByteCount()!");
00235
00236 IntPtr mem = Marshal.AllocCoTaskMem (marshal.Length);
00237 if (mem == IntPtr.Zero)
00238 throw new OutOfMemoryException ();
00239
00240 bool copied = false;
00241 try {
00242 Marshal.Copy (marshal, 0, mem, marshal.Length);
00243 copied = true;
00244 }
00245 finally {
00246 if (!copied)
00247 Marshal.FreeCoTaskMem (mem);
00248 }
00249
00250 return mem;
00251 }
roger_xiong 2009-03-12
  • 打赏
  • 举报
回复
呜呜呜,关键是目前的一个项目是用Vb.net写的,只有一个函数用的是C#,只能把这个函数转换成C#亚,如果我可以选择的话,我肯定直接用C#呀,我哭。
oyljerry 2009-03-12
  • 打赏
  • 举报
回复
有些需要对应转换,没有直接对应的类型
starvii 2009-03-12
  • 打赏
  • 举报
回复
别转了,用用C#多好
wuyi8808 2009-03-12
  • 打赏
  • 举报
回复
VB.Net 中似乎没有指针吧?

111,097

社区成员

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

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

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