On 5 Mar 2005 at 1:35, Frank Heckenbach wrote:
Prof A Olowofoyeku (The African Chief) wrote:
- AFAIK even on Windows you need to specify correct calling
convention, otherwise if it is different then the default the program will crash
Yes, I am aware of that. My original example was simply to illustrate that, with gcc, you can simply declare something as "FARPROC foo", and then use "foo" in any manner you wish (including passing it an arbitrary number of parameters). The success of such an exercise naturally depends on everything else being correct.
`FARPROC' it not a standard C name. I suppose it's defined in some system header as something like:
int (far *) ()
or
int (far *) (...)
It is described as a pointer. This is how it is defined: #define WINAPI __stdcall typedef int (WINAPI *FARPROC)();
You can define variable argument functions in Pascal:
type fsi = function(...): integer;
Ok. I believe it used to be that there wasn't any way to use this (other than to import C library routines that have variable arguments). It seems now that it can be used.
Yes, that mostly corresponds to the C declaration above. Note that in both C and Pascal (GPC), the argument types and number are variable, but the return type must be given. (In C, if omitted in the declaration, it defaults to `int', which should be `CInteger' in GPC now, BTW.)
But this calling style is really only useful when the function really takes different kinds of arguments (like `printf' & co. do in C).
In this case, however, you want to import a specific routine from a dynamic library, and this routine usually does have a fixed number and types of arguments (unless it happens to be something like `printf', but that's the exception).
Therefore, it's better to declare the argument types in Pascal as Waldek showed in this first answer. The same would apply in C, BTW. Probably the C programmers are just a bit lazy, saving some declarations, but actually making the code less safe.
E.g., for the original example:
char *str;
str = foo ("foo", "bar", 24);
the correct corresponding GPC type would be:
type fsi = function (a, b: CString; c: CInteger): CString;
Yes, that is the best way. However, the lazy way is sometimes useful for some quick&dirty stuff ...
Best regards, The Chief -------- Prof. Abimbola A. Olowofoyeku (The African Chief) web: http://www.greatchief.plus.com/