0
 Dim A As String = Chr(128)
  1. Put this line in a VB.Net DLL
  2. Call it from a VB program -->> it works fine.
  3. Call it from a C# program -->> "No data is available for encoding 1252. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method." err.Number 13

Vb accepts 0 to 255 as acceptable values for Chr() but C# breaks the code with a fatal error if the value is above 127.

Why? Why does C# break the code inside the VB.Net DLL?

What other VB functions are affected by C#?

========== Thanks to Charlyface for asking me to add the full exception message. I had been looking at the error the DLL was raising in C# but not the one in VB. The error has given me an idea for the solution...

Gary
  • 29
  • 4
  • "fatal error" what error? Please [edit] and add the full exception message – Charlieface Sep 05 '22 at 16:52
  • 3
    Looks like it calls `Encoding.GetEncoding(GetLocaleCodePage())` which might be causing some issue https://github.com/microsoft/referencesource/blob/5697c29004a34d80acdaf5742d7e699022c64ecd/Microsoft.VisualBasic/runtime/msvbalib/Strings.vb#L277 Quite why you want to use `Chr` in the first place is a different question, it's a holdover from VB6 – Charlieface Sep 05 '22 at 16:55
  • The following may be helpful: [Can I add a reference to a .NET Framework DLL from a .NET 6 project?](https://stackoverflow.com/questions/70005465/can-i-add-a-reference-to-a-net-framework-dll-from-a-net-6-project), [.NET Standard](https://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-1-0#select-net-standard-version), and [Convert.ToChar Method](https://learn.microsoft.com/en-us/dotnet/api/system.convert.tochar?view=net-6.0). – Tu deschizi eu inchid Sep 05 '22 at 21:08
  • What version of the framework are you using in the project? – Anderson Constantino Sep 14 '22 at 14:43

1 Answers1

1

Solution to problem.

Upgrade project to .NET 6 (https://dotnet.microsoft.com/en-us/platform/upgrade-assistant/tutorial/install-upgrade-assistant)

Add System.Text.Encoding.CodePages NuGet package to solution

Register encoding in startup.

Create a global variable (Encod in the sample) and assign to encoding in startup

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)
Encod = Encoding.GetEncoding(1252)
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Gary
  • 29
  • 4