Link to home
Start Free TrialLog in
Avatar of PHILFRED
PHILFRED

asked on

Calling a C++ DLL with multi passed parameters from within Delphi and returning back parameters

I have a bit of a problem....

here the C++ hdr's

int     ENTRYPOINT  PS_AddRecord(PSDEF *ps, unsigned long id,
                                            const char **buf,
                                            int count);

int     ENTRYPOINT  PS_GetRecord(PSDEF *ps, unsigned long *id,
                                            char **buf,
                                            int count,
                                            int first);

I have already got to grips with psdef *ps by passing it a ^pinteger (i think) ..... and can see that i need to use a integer for unsigned long id? but it's the const char **buf that is giving me major problems....

the c code follows that calls it!  - i need to match this routine into delphi to understand it.... i have already done some testing and now that the ipfld_count routine will always return 9.  FIELD_SIZE is 50.

So what i need to know is how i build the **buf structure with the required fields?

I am sorry if this is a big - or complex problem or usually out of scope for things like this.  I founnd expers-exchange today and have been struggling with this for some time now.

IF you can help me I would appreciate it.

Phil

char *p;
    char *ipflds[512];
    char *sort_input_record;
    int sort_input_record_sz, sort_fld_sz, i;
    int ipfld_count;

    /* how many inputs the sort expects */
    ipfld_count = PS_GetInputCount(ps);

    /* allocate memory for the sort input record */
    sort_fld_sz = FIELD_SIZE + 1;
    sort_input_record_sz = sort_fld_sz * ipfld_count;
    sort_input_record = (char *)malloc(sort_input_record_sz);
    if(!sort_input_record)
        return -1;

    memset(file_input_record, 0, sizeof(file_input_record));

    /* read the record */
    p = fgets(file_input_record, sizeof(file_input_record) - 1, ipfp);
    if(!p)
        goto eof;

    /* remove newline from the end */
    file_input_record[sizeof(file_input_record)-1] = '\0';

    input_record_length = strlen(file_input_record);

    chomp(file_input_record, 0);

    /* clear the sort input */
    memset(sort_input_record, 0, sort_input_record_sz);

    /* copy the fields into the sort input record */
    for (i = 0; i < NUM_INPUT_FIELDS && i < ipfld_count; i++)
    {
        memcpy(sort_input_record + (i * sort_fld_sz),
               file_input_record + (i * FIELD_SIZE),
               FIELD_SIZE);
        *(sort_input_record + ((i+1) * (sort_fld_sz))) = '\0';
    }

    /* set the pointer to the start of each string */
    for(i = 0; i < ipfld_count; i++) {
        ipflds[i] = sort_input_record + (i * sort_fld_sz);
        chomp(ipflds[i], 1);
    }

    if(PS_AddRecord(ps, record_number, (const char **)ipflds, ipfld_count)) {
        fprintf(stderr, "%s\n", PS_LastError(ps));
        goto error;
    }

    return 0;

Avatar of robert_marquardt
robert_marquardt

int     ENTRYPOINT  PS_AddRecord(PSDEF *ps, unsigned long id,
                                            const char **buf,
                                            int count);

function PS_AddRecord(ps: PPSDEF; id: Cardinal;
  buf: PPChar; count: Integer): Integer; stdcall;

int     ENTRYPOINT  PS_GetRecord(PSDEF *ps, unsigned long *id,
                                            char **buf,
                                            int count,
                                            int first);

function PS_GetRecord(ps: PPSDEF; var id: Cardinal;
  buf: PPChar; count: Integer; first: Integer): Integer; stdcall;

type
  PPSDEF = ^PSDEF;
  PPChar = ^PChar;

First the calling convention is probably stdcall. cdecl is not used often in DLLs.
From the layout of the parameters i guess that this functions take an array of PChar pointers with count telling how many.
In PSGetRecord it seems that id is filled in so it converts to var instead of pointer.

Use the functions like this:

var
  buffer: array [0..Whatever] of PChar;
  id: Cardinal;
  First: Integer;
  ps: PPSDEF;
begin
   // ps needs to be initialized. It is probably used like a handle.
  // fill buffer elements
  // first needs to be initialized
  PS_GetRecord(ps, id, @buffer[0], Whatever, First);

It is possible that First is in fact a boolean variable. Then the correct type is LongBool.
Avatar of PHILFRED

ASKER

Thanks for the information my main stumbling block is getting the information into addrecord to start with?

If I could get some guidance on that it would be excellent.

With thanks in advance.

Phil
ASKER CERTIFIED SOLUTION
Avatar of robert_marquardt
robert_marquardt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks on it's way...
Big Big Praise to robert.

He solved the problem sent me the code - and at last i can sleep! Thanks very very much a true gent and expert!