Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2005-07

C++ > C wrapper callback > Pascal

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Urbansound

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;
var
    bid_price : double;
    s : string;
begin
    SendMessage(listbox, LB_ADDSTRING, 0, longint('L1 Callback'));
    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

Reply by email

Michalis Kamburelis
to Urbansound
Urbansound wrote 24 lines
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.

Show quoted text 5 lines
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.
Show quoted text 20 lines
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

Reply by email