Re: [fpc-pascal] C++ > C wrapper callback > Pascal
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(pQuote)); if(m_hWndCallback) ::SendMessage(m_hWndCallback,1,0,reinterpret_cast(pQuote)); return 0; } Mike ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] C++ > C wrapper callback > Pascal Continues
Urbansound wrote: Hi List, 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 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, 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. Thank you to Michalis Kamburelis... clipped this down (and still too long)... Yes the integer sizes were confirmed = 32 (win32 gui) and the export was added simply attempting to force a change in memory allocation, along with pre-initializing variables. Among other things, Delphi mode was tried and we boiled the problem down to two lines of code in particular, commented below. As well, we hand converted the Pascal app to C, in order to test the C_wrapper, compiled on GCC and the same calling conventions and methods work fine in C-GCC, but fail in Pascal. VERY ODD. Wrapper works fine. Review: //-- PROBLEM IS BELOW and either comment line removed, will crash = RT217... I suspect the problem is pointer "quote", somehow is crashing registers. FUNCITON HEADER: function SetL1Callback(func_addr : pointer) : integer; stdcall; external 'DasRapper.dll' name 'SetL1Callback'; > where SetL1Callback(@L1_Callback); is set in the pascal main area.< FUNCTION CALL TO WRAPPER function L1_Callback(quote : pointer) : integer; stdcall; var bid_price : double = 0.0; s : ansistring = ''; begin SendMessage(listbox, LB_ADDSTRING, 0, longint(pchar('L1 Callback'))); SendMessage(listbox, LB_ADDSTRING, 0, longint(Get_secsym(quote))); bid_price := Get_l1_BidPrice(quote); //s := FloatToStr(bid_price); // This will crash! //SendMessage(listbox, LB_ADDSTRING, 0, longint(pchar(@s))); end; // PASCAL WRAPPER function being called is below DllExport void SetL1Callback(TL1Callback p) { DasApi.m_pL1Callback=p; }; SUMMARY: There seems to be nothing I can find that would cause a Runtime 217 in the layout and now finding C conversion of same is working. Can anyone else please confirm if this should work, or is a bug report be in order?? Thanks to all, whoever may have additional ideas. Mike ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] Doubts about QueryInterface
When I try to compile a unit in delphi mode with the following function function Implements(out AReference; const AObject: TObject; const AInterface: TGUID): Boolean; begin Result := (AObject is TInterfacedObject) and ((AObject as TInterfacedObject).QueryInterface(AInterface, AReference) = 0); end; I get: Utilities.pas(33,44) Error: identifier idents no member "QueryInterface" Utilities.pas(33,58) Fatal: Syntax error, ")" expected but "(" found Utilities.pas(33,58) Error: Compilation aborted What am I doing wrong? ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] C++ > C wrapper callback > Pascal Continues
>> Urbansound wrote: >>> Hi List, >>>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 >>> >>> 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, >> 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. > > Thank you to Michalis Kamburelis... > > clipped this down (and still too long)... > > Yes the integer sizes were confirmed = 32 (win32 gui) and the export was > added simply attempting to force a change in memory allocation, along with > pre-initializing variables. Among other things, Delphi mode was tried and > we boiled the problem down to two lines of code in particular, commented > below. As well, we hand converted the Pascal app to C, in order to test > the > C_wrapper, compiled on GCC and the same calling conventions and methods > work > fine in C-GCC, but fail in Pascal. VERY ODD. Wrapper works fine. > > Review: > //-- > PROBLEM IS BELOW and either comment line removed, will crash = RT217... > I suspect the problem is pointer "quote", somehow is crashing registers. > > FUNCITON HEADER: > function SetL1Callback(func_addr : pointer) : integer; stdcall; external > 'DasRapper.dll' name 'SetL1Callback'; > > where SetL1Callback(@L1_Callback); is set in the pascal main > area.< > > FUNCTION CALL TO WRAPPER > function L1_Callback(quote : pointer) : integer; stdcall; > var > bid_price : double = 0.0; > s : ansistring = ''; > begin > SendMessage(listbox, LB_ADDSTRING, 0, longint(pchar('L1 Callback'))); > SendMessage(listbox, LB_ADDSTRING, 0, longint(Get_secsym(quote))); > bid_price := Get_l1_BidPrice(quote); > > //s := FloatToStr(bid_price); // This will crash! > //SendMessage(listbox, LB_ADDSTRING, 0, longint(pchar(@s))); > end; > > // > PASCAL WRAPPER function being called is below > > DllExport void SetL1Callback(TL1Callback p) { DasApi.m_pL1Callback=p; }; > > SUMMARY: > There seems to be nothing I can find that would cause a Runtime 217 in > the > layout and now finding C conversion of same is working. > > Can anyone else please confirm if this should work, or is a bug report be > in > order?? A bug report is only in order if there are complete sources otherwise it can't be reproduced. > Thanks to all, whoever may have additional ideas. Mike Are the pascal units are initialized? A call to FPC_INITIALIZEUNITS is required otherwise the memorymanager etc. will not work. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal