1

Hello I am studying an internet code , and called my attention this part :

// ~ Your Dll Here ~ Ex : 'C:\MS10046.dll'
  SizeNameDll : integer = 28;
  Dllx  : ARRAY [1..28] OF Byte =  ($00,$43,$00,$3A,$00,$5C,$00,$4D,$00,$53,$00,$31,$00,$30,$00,$30,$00,$34,$00,$36,$00,$2E,$00,$64,$00  ,$6C,$00,$6C);

Supposedly the contents of the array has the value of "C: \ MS10046.dll " I do not understand two things , why SizeNameDll value is 28 and this is not the exact length of "C: \ MS10046.dll "? How I can change the value of " C: \ MS10046.dll " to all code by another route such as " c: /xampp/test.dll "?

Could someone help me?

0

2 Answers 2

5

The bytes represent the text 'C:\MS10046.dll' in big endian UTF-16 Unicode. In UTF-16, "characters" (or correctly called: code points) are made up of 16 bit wide code units, or in Delphi, WideChars. So each code unit is two bytes in size. In little-endian, the default for Intel-based platforms, this means that 'C' is encoded as $0043, which is a byte $43, followed by a byte $00. In big-endian, this is reversed, so 'C' (or $0043) is $00 followed by $43. The same for ':': this is $3A,$00 in little-endian, but $00,$3A in big-endian. So 14 "characters" result in the array you showed.

Since the array seems to start with a $00, this must be big-endian.

If the function you must use requires an array of bytes and a length, then you should probably call it like:

var
  Dllx: TBytes;
  str: string;
begin
  str := 'c:\xampp\test.dll'; // I removed the spaces - they are wrong
  Dllx := TEncoding.BigEndianUnicode.GetBytes(str);
  if Length(Dllx) > 0 then
    YourFunction(... PByte(Dllx), Length(Dllx), ...);

It would help if you showed how the array is supposed to be used, IOW, show a little more code. It is highly unusual that a DLL on Windows requires big-endian encoding.

It could well be that, if big-endian is not required at all, that you could do without the conversion:

if Length(str) > 0 then
  YourFunction(..., PChar(str), Length(str) * Sizeof(WideChar), ...);

or even (it all depends on the declaraton of the function):

if Length(str) > 0 then
  YourFunction(..., PChar(str), Length(str), ...);

The latter looks far more likely, but without more information, this is all guesswork, sorry.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the help , one last question , the " SizeNameDll " the original code would be equal to Length ( Dllx ) ?
Post the declaration of the function that requires this setup, and I can tell you. Just edit your question and add the information.
Oh, yes, SizeNameDll is equal to Length(Dllx). A proper declaration would have been: Dllx: array[0..SizeNameDll - 1] of Byte = (....etc.);. Am I right that this is taken from - badly written, IMO - example code for the DLL? Is there C sample code too?
4

These 28 bytes represent your 14 character string encoded as UTF-16BE. Each character is represented by a 16 bit character element. That's why a string of length 14 consumes 28 bytes.

To encode a general string as UTF-16BE you would write:

var
  Bytes: TBytes;
....
Bytes := TEncoding.BigEndianUnicode.GetBytes(str);

where str is your string.

5 Comments

The endianness of that string will be wrong, though - trick question?
@500-InternalServerError You are right. Let me correct.
Thanks for the explanation , as I show the result of " Bytes " as the code numbers " $ 00 , $ 43, $ 00" ?
IME, it is highly unusual that a DLL (presumably on Windows) requires big-endian text. It would not surprise me if this was from example code for the DLL, written by someone who did not really know Delphi (otherwise, they would probably not have used a start index of 1 for the array either). Perhaps they do not require big endian after all. But only NickFurry can tell us this. This could be some kind of XY problem.
@Rudy - without a wider context we don't know how this UTF-16BE encoded data is actually used. Perhaps the author of the code in this case was presenting an interface to a proprietary API, leaving it to the consumer of that API to perform any necessary conversions (a not unusual scenario). For example, in Apple Bonjour all Unicode strings are UTF-8. In MP4 metadata all UTF-16 is UTF-16BE. Not all API implementations perform convenient platform transcoding, especially not those which are 'language/platform portable'. Granted, the 1-based array is odd though. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.