Board index » delphi » C -> Delphi help!!

C -> Delphi help!!

I have been pulling my hair out trying to use a C API in delphi.  I
would greatly appreciate it if someone could take a look at the C code
below and give me a Delphi equivalent.  Let me know if more info is
needed.  I'm almost out of hair!  :)

From the C header:

        typedef struct TheStruct_S  
        {
                unsigned long   fld1;                          
                char*           fld2;          
                char*           fld3;    
                void*           fld4;                                  
                char*           fld5;                  
                char*           fld6;                  
                char*           fld7;  
                char*           fld8;          
                void*           fld9;                  
        } TheStruct, *pTheStruct;

        typedef const TheStruct* TheData;

From 'main':

        TheStruct TheOne;

        memset(&TheOne, 0, sizeof(TheStruct));
        TheOne.fld1 = sizeof(TheStruct);
        TheOne.fld4 = HKEY_CURRENT_USER;                        
        TheOne.fld8 = "Whatever";

        if(!IS_OK(TheFunction(&TheOne)))
                return error_exit("Failed!!");

 

Re:C -> Delphi help!!


On 10 Jul 2001 13:07:45 -0700, gammabe...@hotmail.com (Tom) wrote:

Quote
>I have been pulling my hair out trying to use a C API in delphi.  I
>would greatly appreciate it if someone could take a look at the C code
>below and give me a Delphi equivalent.  Let me know if more info is
>needed.  I'm almost out of hair!  :)

>From the C header:

>    typedef struct TheStruct_S  
>    {
>            unsigned long   fld1;                          
>            char*           fld2;          
>            char*           fld3;    
>            void*           fld4;                                  
>            char*           fld5;                  
>            char*           fld6;                  
>            char*           fld7;  
>            char*           fld8;          
>            void*           fld9;                  
>    } TheStruct, *pTheStruct;

>    typedef const TheStruct* TheData;

Unusual structure, it consists mostly of pointers.  char* translates
to a PChar in Delphi, void* is an untyped pointer.

There was no unsigned long integer prior to D5, but considering how
this appears to be used I don't think that's likely to be a problem.

The field names (not to mention the name of the structure) are
absurdly cryptic, the programmer who wrote that ought to be flogged
with a photo of Niklaus Wirth.

In any case I'd translate it thusly:

type
  PTheStruct = ^TheStruct;
  TheStruct = packed record
    fld1 : LongInt;
    fld2 : PChar;
    fld3 : PChar;
    fld4 : pointer;
    fld5 : PChar;
    fld6 : PChar;
    fld7 : PChar;
    fld8 : PChar;
    fld9 : pointer;
  end;

Quote
>From 'main':

>    TheStruct TheOne;

>    memset(&TheOne, 0, sizeof(TheStruct));
>    TheOne.fld1 = sizeof(TheStruct);
>    TheOne.fld4 = HKEY_CURRENT_USER;
>    TheOne.fld8 = "Whatever";

>    if(!IS_OK(TheFunction(&TheOne)))
>            return error_exit("Failed!!");

var
{declare an instance of the structure}
  TheOne : TheStruct;
begin

{zero the memory, essentially sets all the pointers to nil}
  FillChar(TheOne, SizeOf(TheStruct), #0);

{first field is evidently supposed to take the size of the stucture}
  TheOne.fld1 := SizeOf(TheStruct);

{fill the fourth field with the constant for the HKCU registry tree?}
{I gather this has something to do with the registry then...}
  TheOne.fld4 := pointer(HKEY_CURRENT_USER);

{point field 8 to a constant string}
  TheOne.fld8 := 'Whatever';

{ check the result of passing the structure to some function}
  if not IS_OK(TheFunction(@TheOne)) then begin
     Result := error_exit('Failed!!');
     Exit;
  end;

Now, some of the above is very "knee-jerk" translation and may well
not work depending on what all this is actually supposed to be doing.

In particular I'm presuming:

1) you have implementations of TheFunction() and IS_OK() and
error_exit() to actually call.  

2) TheFunction() takes a PTheStruct parameter

3) TheFunction() will work with a constant string as passed in fld8,
if it's expecting to write something to that field, you'll probably
get a pretty spectacular AV from this.

Does this help any?

Stephen Posey
slpo...@concentric.net

Other Threads