Solved

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

Posted on 2004-08-11
5
190 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks on it's way...
0
 

Author Comment

by:PHILFRED
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now