Question about CDECL

General FreeBASIC programming questions.
VANYA
Posts: 1245
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Question about CDECL

Postby VANYA » Aug 27, 2017 17:09

Hi All!

If the program has a function CDECL , but this function causes another function, then whether it is necessary to place the second function CDECL ?

For example:

Code: Select all

sub two CDECL (p as any ptr) ' is it necessary to indicate here CDECL?



end sub

sub one CDECL (p as any ptr)

      dim as any ptr pTemp

      two(pTemp)

end sub
St_W
Posts: 1056
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Question about CDECL

Postby St_W » Aug 27, 2017 18:12

If don't specify CDECL for sub two the method will use the default calling convention. In any case it will be called accordingly from sub one.

So to answer your question: it is basically not necessary to make sub two use CDECL in your example, but it probably depends on why you want to use CDECL at all. Please give some more information why you are using CDECL. Then you could also decide yourself whether those requirements are necessary for both or only one function. You don't have to use CDECL for a method which is called by a CDECL method. In case that was your question: no, that is not a valid reason for using CDECL.
fxm
Posts: 7269
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: Question about CDECL

Postby fxm » Aug 27, 2017 18:21

Yes, for nested procedures, there is no propagation of the calling convention from the procedure calling to the called procedure.
VANYA
Posts: 1245
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Question about CDECL

Postby VANYA » Aug 27, 2017 18:33

St_W wrote:If don't specify CDECL for sub two the method will use the default calling convention. In any case it will be called accordingly from sub one.

So to answer your question: it is basically not necessary to make sub two use CDECL in your example, but it probably depends on why you want to use CDECL at all. Please give some more information why you are using CDECL. Then you could also decide yourself whether those requirements are necessary for both or only one function. You don't have to use CDECL for a method which is called by a CDECL method. In case that was your question: no, that is not a valid reason for using CDECL.


CDECL used for the procedure CALLBACK libraries IUP.

Real example:

Code: Select all

#include "inc/iup.bi"

sub _init_ constructor
  IupOpen(0,0)
end sub

sub _exit_ destructor
  IupClose()
end sub

Sub  necessary() '  necessary cdecl?
   
   
   
End Sub


function mlaction cdecl (self as Ihandle ptr, c as integer,after as zstring ptr) as integer
  if (c = K_g) Then
     
     necessary() ' CALL necessary()
     
    return IUP_IGNORE
  else
    return IUP_DEFAULT
  end if
end function

'
' main
'
dim as Ihandle ptr dlg, ml

ml = IupMultiLine(NULL)
IupSetCallback(ml, "ACTION", cast(Icallback,@mlaction))
IupSetAttribute(ml, "EXPAND", "YES")
IupSetAttribute(ml, "VALUE",  !"I ignore the \"g\" key!")
IupSetAttribute(ml, "BORDER", "YES")

dlg = IupDialog(ml)
IupSetAttribute(dlg, "TITLE", "IupMultiline")
IupSetAttribute(dlg, "SIZE",  "QUARTERxQUARTER")

IupShow(dlg)
IupMainLoop()
St_W
Posts: 1056
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Question about CDECL

Postby St_W » Aug 27, 2017 19:06

VANYA wrote:CDECL used for the procedure CALLBACK libraries IUP.

So as long as you don't want to use "necessary()" as callback directly you don't need CDECL. As I wrote above the "necessary" method won't use cdecl in that case, but that is okay as long as it is not called by a method expecting that (like IUP as it seems). FB methods will "know" the calling convention for the "necessary" method and call it accordingly.
Last edited by St_W on Aug 28, 2017 7:19, edited 1 time in total.
VANYA
Posts: 1245
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Question about CDECL

Postby VANYA » Aug 28, 2017 3:15

Clear, thanks a lot for answers!

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest