Solved

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

Posted on 2004-08-11
5
192 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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