Solved

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

Posted on 2004-08-11
5
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month8 days, 11 hours left to enroll

615 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