[fpc-pascal] C++ > C wrapper callback > Pascal

Michalis Kamburelis michalis at camelot.homedns.org
Tue Jul 26 09:29:43 CEST 2005

Urbansound wrote:
> Hi List,
>    I'm probably over my head on this one and some of the FPC forum 
> entries talk about limited functionality toward C++, but here goes.
>    A Windows pascal application is recieving data from a C++, VisC API, 
> which is set up as a class, and a conventional C wrapper, attempting to 
> extend functionality into Pascal.  C++ and C are vis 4.2 MFC
>    The application crashes on the callback shown below.  The odd thing 
> is that it crashes, simply using a string allocation in pascal.  Tried 
> ansistring and others.  Assumption, is that the callback is possibly not 
> configuring memory or address pointer correctly and then the String 
> allocation is landing off location by register and stomping on a 
> relative memory location.
>    Is this a valid implementaiton or outside the scope of FPC?
>    Thanks in advance for any suggestions.
> THE PASCAL is called back to, in the following:
> function L1_Callback(quote : pointer) : integer; stdcall; export;

One thing that comes into my mind is that you should make sure that 
"integer" above really means what you want. "integer" is either 16-bit 
or 32-bit, depending on FPC $mode. You may want to change this to 
"LongInt" or "c_int", to be sure you're using 32-bit integer. If you're 
mistakenly using 16-bit integer here, then a lot of mysterious problems 
may happen.

Also, do you really want to put "export" here ? It's harmless, but 
unnecessary, if you're only passing a pointer to L1_Callback to some 
other C routine.

> var
>    bid_price : double;
>    s : string;
> begin
>    SendMessage(listbox, LB_ADDSTRING, 0, longint('L1 Callback'));

I would change this to
   TempString := 'L1 Callback';
   SendMessage(listbox, LB_ADDSTRING, 0, longint(Pointer(TempString)));
to be on the safe side.

>    SendMessage(listbox, LB_ADDSTRING, 0, longint(Get_secsym(quote)));
>    bid_price := Get_l1_BidPrice(quote);
>    //s := 'test message';                             // This will crash!
>    //s := FloatToStr(bid_price);                 // Intended call crashes!
>    //SendMessage(listbox, LB_ADDSTRING, 0, longint(s));
> end;
> THE C WRAPPER has the following:
> int CDasApiEx::OnQuote(st_L1Quotes *pQuote)
> {
> if(m_pL1Callback) (*m_pL1Callback)(reinterpret_cast<void*>(pQuote));
> if(m_hWndCallback) 
> ::SendMessage(m_hWndCallback,10000,0,reinterpret_cast<LPARAM>(pQuote));
> return 0;
> }
> Mike

More information about the fpc-pascal mailing list