-1

Sorry in advance if you have a duplicate or a simple question! I can't find the answer.

I'm working with a dll made in Delphi. Data can be sent to the device using a DLL. However, at the time the data is sent, some strings are not accepted or are written blank. The data sent to the device is stored in a txt file. It was generated using txt file third party program. That is, I think the string is in an indefinite format. If I send in utf-8 format, it receives all the information. But some strings at the time ???? ???? remains. Many of my texts are in the Cyrillic alphabet. What I did:

// string that send to device
[MarshalAsAttribute(UnmanagedType.LPStr, SizeConst = 36)]
  public string Name;

When I did this, the device received only 10 out of 100 data. If i encoding with UTF-8:

byte[] bytes = Encoding.Default.GetBytes(getDvsName[1].ToString());
string res = Encoding.UTF8.GetString(bytes);

Got all the data this way but too many strings are became as ??? ????. Also i tried like this:

static private string Win1251ToUTF8(string source)
{
  Encoding utf8 = Encoding.GetEncoding(«utf-8»);
  Encoding win1251 = Encoding.GetEncoding(«windows-1251»);
  byte[] utf8Bytes = win1251.GetBytes(source);
  byte[] win1251Bytes = Encoding.Convert(win1251, utf8, utf8Bytes);
  source = win1251.GetString(win1251Bytes);
  return source;
}

All of the above methods did not help. How can I receive incoming information in the correct format? Are there other ways?

  • If you're trying to get a string representation of the byte[], you would want to use `Convert.ToBase64String`. – jimnkey Apr 22 '21 at 05:32
  • Have you tried Encoding.Unicode? Does the DLL documentation indicate what kind of encoding it uses? – B.O.B. Apr 22 '21 at 05:33
  • It sounds like you should be communicating with the DLL via binary data (`byte[]`) rather than as a string. "Re-encoding" a string is *always* a lost cause, as by the time you've got the broken string, you've usually lost data. – Jon Skeet Apr 22 '21 at 05:39
  • @B.O.B. there nothing about encode in documentation. In doc contains as `public string Name; // Product Name, 36 characters` only. – MinorDestroyer Apr 22 '21 at 05:40
  • Well without knowing anything about what API you are trying to interact with, not sure we can offer much more help. Only one thing I can think to ask without more info about the API: are you sure it's LPSTR? (https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/3f6cc0e2-1303-4088-a26b-fb9582f29197) If you're getting gobbled up characters it might be something else like LPWSTR (https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/50e9ef83-d6fd-4e22-a34a-2c6b4e3c24f3). When working with non .Net DLLs in .Net there numerous different types of strings... – B.O.B. Apr 22 '21 at 05:50
  • ...know which type the API and the encoding being used by the API is crucial. If such information is not any API documentation, you are left with two choices: trial and error until you get something that comes out right, or contacting the API developers and asking. – B.O.B. Apr 22 '21 at 05:51
  • I've never tried to work with a Delphi DLL (as far as I know). But alot of pinvoke stuff can work with StringBuilder instead of string. https://stackoverflow.com/questions/16170360/returning-a-string-from-delphi-dll-to-c-sharp-caller-in-64-bit <- that thread there seems to suggest using StringBuilder with a Delphi DLL. It also has the [DLLImport] using Unicode. So that might give you some clues as to how to maybe do it with your DLL. – B.O.B. Apr 22 '21 at 05:56
  • @B.O.B. I tried all the ways you said. But they all have the same problem. LPStr and LPWStr didn't help either, they work almost the same. The third-party program generates a file in the ANSI-1251 format. But it doesn't matter if I switch from 1251 to UTF-8 ???? is returning. – MinorDestroyer Apr 22 '21 at 06:16

1 Answers1

0

hi there here is what went wrong you did encode the string to default instead of utf8.

string tom = "ටොම් හැන්ක්ස්";

byte[] bytes = Encoding.UTF8.GetBytes(tom);
string res = Encoding.UTF8.GetString(bytes);
EL Khayar
  • 11
  • 4