Pass Delphi to an external Delphi function from C #

I am trying to call an external Delphi function from C # that takes Delphi as a parameter:

Delphi Code

type
  tStatus = (sIn, sOut, sAbsent, sSick);
  tStatusSet = set of tStatus;

function LoadEmployees(tStatusSet aStatusSet): tEmpList;

I need to output a C # array from enum values ​​(i.e. elements from tStatus) into a format that Delphi will read as a type tStatusSet:

C # code

tStatusSet lStatusSet = ConvertToDelphiSet(sIn, sOut);

tEmpList lEmpList = LoadEmployees(aStatusSet);

ConvertToDelphiSetIdeally, it should be a general solution that can handle any enumeration. We defined it as:

int ConvertToDelphiSet<T>(params T[] aArgs) {
  int lResult = 0;

  foreach (T lItem in aArgs)
  {
    int lValue = lItem.ToInt32();
    lValue = (int)Math.Pow(2, lValue);

    lResult |= lValue;
  }

But this does not return the correct value (for example, passing all four values tStatus, the Delphi result shows only the third value in the set).

, Delphi ? ? ? , ?

+3
3

, -, , , , "".

Delphi 256 255, , , :

  TEnum = (a, b, c=255);
  TSet  = set of TEnum;

TSet 256 , 3 . ( , "a" 0, NOT 1!)

, sizeof() , , , , .

  TEnum = (a, b, c=255);
  TSet  = set of TEnum;

  >>> sizeof(TSet) = 32


  TEnum = (a, b, c);
  TSet  = set of TEnum;

  >>> sizeof(TSet) = 1

, Delphi, , , #, Delphi, / - .

, , RTTI Delphi Delphi ( -1), GetEnumValue):

  enumValue := TEnum( GetEnumValue(TypeInfo(TEnum), sEnumMemberName) );
  if Ord(enumValue) = -1 then
     raise Exception.Create('Invalid enum value' + sEnumMemberName);

  Include(setVar, enumValue);

GetEnumValue(), GetEnumName() TypInfo.

, , # ( ), Delphi (, , ), , , , , n- .

, , # Delphi .

, = , , # Delphi, , , , :

:

     enumValues: array of Byte;
     setVar: TSet;  // set of TEnum

  for i := 0 to Length(enumValues) do
    Include(setVar, TEnum(enumValues[i]));
+10

Delphi, , , , DLL , . , API Windows, .

Delphi. , , .

DWORD 2.

+4

, , , , , , .

, Delphi , , . 256 , , 1 32 .

, Delphi, 4 .

+2

Source: https://habr.com/ru/post/1717233/


All Articles