Click here to Skip to main content
15,903,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, I'm translating a library from C# to vb.net, it's a complex job due to the many differences but it's almost done. The only point where I can't is the following:

var CurrentWord = new StringBuilder();
char[] Buff = Text.ToCharArray();
fixed (char* c = &Buff[0])

especially "fixed (char* c = &Buff[0])".

I understand that vb.net does not have compatible in-memory pointers but I hope there is a solution even if it is convoluted.

Thank you all

Excuse my English as always...

What I have tried:

I tried converting with "https://converter.telerik.com/" and others but without success, System.NotSupportedException: UnsafeKeyword is not supported!
Posted
Comments
Dave Kreskowiak 15-Jan-24 11:19am    
The first question is why are you converting the C# library at all when you can just add a reference to it in VB.NET and use the existing library?
10071962 15-Jan-24 11:26am    
Because having the source in VB I can modify it and integrate it into my project which is a cryptographic editor and I am interested in some functions of this library that you find here. https://code.google.com/archive/p/alsing/downloads
Dave Kreskowiak 15-Jan-24 12:27pm    
OK, the problem is there is no Vb.NET equivalent. An idea would be to create a C# class library wrapping the functionality of allocating buffers and having "fixed" access to them, then reference that library in VB.NET.

No, I don't have examples of doing such. I just abandoned VB.NET entirely and went with C# for all my .NET development needs.
10071962 16-Jan-24 0:29am    
Thanks, I tried all your suggestions but without success; the closest one is "https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/strings/how-to-access-characters-in-strings" , but this isn't functional either. Dave Kreskowiak, I don't quite understand what you mean. Thank you all.
Jo_vb.net 15-Jan-24 12:40pm    
Perhaps this helps you
https://stackoverflow.com/questions/44976633/convert-fixed-and-byte-from-c-sharp-to-vb-net

 
Share this answer
 
v2
Quote:
I tried converting with "https://converter.telerik.com/" and others but without success, System.NotSupportedException: UnsafeKeyword is not supported!
Most of these are not well maintained or simply abandoned. What I find, as a better solution, not just for C# <-> VB, but any <-> any, is AOpen's ChatGPT[^]. When in a pinch, I find that it is very good at converting code.

Here is how to ask:

Convert the following C# to VB.NET:

var CurrentWord = new StringBuilder();
char[] Buff = Text.ToCharArray();
fixed (char* c = &Buff[0])

And it will give you an answer. But note: while it is very good at conversion, it's not the best at taking a statement and converting to code. Often, more than not, you will have to either ask it more questions, debug and modify the code that it gives you, or simply abandon and write it yourself.
 
Share this answer
 
Comments
10071962 17-Jan-24 0:39am    
Perfect! It also gives a simple explanation of the differences. As soon as I get to try it I'll update you. Thank you very much!
If I suggest you to fix like char* from string.

But I have already 2 different sbyte* and byte* formats from string. And I will release soon char* for Windows Users of WinAPI.

Check out my article!
If you use VB than you can use DeafMan1983.Conversion. But I am not sure for latest version of net 7.x or 8.x.

I hope you get my solution.
 
Share this answer
 
Comments
10071962 18-Jan-24 8:24am    
Thank you very much!
Fixed: To get a pointer to the first char (or whatever), use
'Marshal.UnsafeAddrOfPinnedArrayElement(Buff, 0)'

I believe that using GCHandle to pin 'Buff' isn't needed on a local variable,
but you'd better check it!;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900