Solved

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

Posted on 2004-08-11
5
193 Views
Last Modified: 2010-04-04
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;

0
Comment
Question by:PHILFRED
  • 3
  • 2
5 Comments
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 11780598
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.
0
 

Author Comment

by:PHILFRED
ID: 11781590
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
0
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 500 total points
ID: 11786243
Best send me the C header you converted, your conversion and a source which contains your problem to robert_marquardt att gmx dott de
0
 

Author Comment

by:PHILFRED
ID: 11786713
thanks on it's way...
0
 

Author Comment

by:PHILFRED
ID: 11791962
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!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question