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

Urbansound urbansound at sbcglobal.net
Thu Jul 28 08:06:46 CEST 2005


See bottom post
----- Original Message ----- 
From: "Urbansound" <urbansound at sbcglobal.net>
To: <fpc-pascal at lists.freepascal.org>
Sent: Wednesday, July 27, 2005 11:05 PM
Subject: C++ > C wrapper callback > Pascal Continues


>
> Reply Peter and Tomas, is below, each... Thread pruned.
>
>>   2. Re:  C++ > C wrapper callback > Pascal  Continues (Peter Vreman)
>>   3. Re:  C++ > C wrapper callback > Pascal  Continues (Tomas Hajny)
>
>> ----------------------------------------------------------------------
>> Message: 2
>> Date: Wed, 27 Jul 2005 14:26:57 +0200 (CEST)
>
>> It is an internal function implicitly called by the compiler.
>
> The s call in the message line was converted to a "pointer(s)".  I found 
> the FPC_INITIALIZEUNITS at Main, in the ASM output and understand now that 
> the compiler handles this fully and that FPC_INITIALIZEUNITS could not be 
> the issue.  Perhaps this thread will help others new to FPC to know this 
> too on the chance they may research it.  Thank you.  Present code is at 
> bottom to thin down the thread.
>
> Tomas below, may be onto it, as a threading issue.  Thanks still for your 
> help.  Mike
>
>>
>> Well, maybe I misunderstood the original post, but my understanding was
>> that it's about a _Pascal_ program calling some C function (which is in
>> fact just a wrapper for C++, but that shouldn't matter) and passing some
>> other Pascal function as a callback. If this is the case and the callback
>> function runs within the same thread as the block calling the C wrapper,
>> Pascal units should be initialized since the main Pascal program startup,
>> shouldn't they? If a new thread is started by the C++ program, you need 
>> to
>> perform (Pascal) initialization of the thread (probably on the first 
>> entry
>> to the callback function). Could you clarify this, please (link to the
>> complete sources or preferrably their simplified but still compilable
>> version would probably serve the best as already pointed out by Peter)?
>>
>> Tomas
>
> Thanks Tomas.
>
> 1) Current function is below, due to pruning the thread.
> 1) Clearly I was off course on FPC_INITIALIZEUNITS
> 2) You're understanding is correct C++api > C+wrpr > Pascal as callback
>     The wrapper is actually C code in a .cpp, for functionality
> 3) The API is multi-threaded, having dl'd a thread monitor, showing as 
> many as 5 threads spawened to the API and accompanied executable.  As 
> well, a call back occurs by default from the API, at runtime, suggesting 
> it fires off a single dummy callback event.
> 4) Can you suggest an approach on the Pasacl Initialization to the thread 
> and/or some of the areas I should be looking up toward doing the work? 
> Google is my friend. ;-)
>
> Thank you again.   Mike
>
> //------- Current function model below here
>
> function L1_Callback(quote : pointer) : integer; stdcall; export;
> var
>    bid_price : single = 0.0;
>    s : ansistring = '';
> begin
>    SendMessage(listbox, LB_ADDSTRING, 0, longint(Pchar('L1 Callback')));
>        // -- either commented below will cause crash
>    //SendMessage(listbox, LB_ADDSTRING, 0, longint(Get_secsym(quote)));
>    //bid_price := Get_l1_BidPrice(quote);
>    s := FloatToStr(bid_price);
>    SendMessage(ListBox,LB_ADDSTRING,0,Longint(pointer(s)))
> end;
>

Update:

Per Tomas suggestions:
Among our C equivelent of the Pascal program, we did the following, which 
confirms that the C+ callback and Pascal, are on the same, single thread, 
which was confirmed with a thread monitoring tool during operation.  (See 
comment at bottom).

//---------Thread compare
void ShowThreadInfo(char* id)
{
 char s[80];
 sprintf(s,"%s  Thread ID: %08X  Thread Handle: 
%08X",id,GetCurrentThreadId(),GetCurrentThread());
 SendMessage(listbox, LB_ADDSTRING, 0, (LPARAM)s);
}

int _stdcall L1_Callback(void* quote)
{
 //double bid_price;
 char s[100];

 ShowThreadInfo("Callback");
//------------------------------------------

Given that an equivelent C program, (built by modifying the Pascal program 
to suit C syntax/method), runs without a problem and yet there is no evident 
reason code-wise for the problem, what is the next step in resolving the 
matter.  I can use GDB, though I am new to it, as of this issue.  If someone 
is willing to give me a guide on what command/data gdb can afford us, I'd 
certainly make the effort here, unless there are other suggestions?  This is 
fpc 2.00, so I'm current w/stable. 





More information about the fpc-pascal mailing list