Checking if a function exists at compile time: native function failed at run time
#1

I need to check if a function is defined(for an include). So I wrote this code:
pawn Код:
#if !defined fcreate
    stock fcreate(const filename[])
    {
        printf("Fcreatea: start");
        if(!fexist(filename))
        {
            printf("Fcreatea: the file does Not exist");
            new File:file = fopen(filename,io_write);
            printf("Fcreatea: file opened");
            return fclose(file);
        }
        return 0;
    }
#endif
And a backtrace from crashdetect:
pawn Код:
[18:49:49] [debug] Run time error 10: "Native function failed"
[18:49:49] [debug]  fread
[18:49:49] [debug] AMX backtrace:
[18:49:49] [debug] #0 00000c50 in public OnGameModeInit () from speed.amx
I used the function in OnGameModeInit hook:
pawn Код:
printf("File does NOT exist");
        fcreate(BAN_FILE_NAME);
I left those prints there for a reason. This is the last thing printed out before "Native function failed":
pawn Код:
[18:49:49] File does NOT exist
Which is just before calling the function "fcreate"

If I remove the #if !defined part, it runs just like it should. But this is not a sollution.


All ideas will be appreciated
Reply
#2

To check for the existance of a function, use https://sampwiki.blast.hk/wiki/Funcidx (works only for publics)
Reply
#3

That doesn't really help..(I think) I need to define it if it doesn't exist
Reply
#4

Unless you do it with #define:
pawn Код:
#if !defined fcreate
    #define fcreate(%0); if (!fexist(%0)) { new File:file = fopen(%0,io_write); if(file) fclose(file); }
#endif
Reply
#5

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Unless you do it with #define:
pawn Код:
#if !defined fcreate
    #define fcreate(%0); if (!fexist(%0)) { new File:file = fopen(%0,io_write); if(file) fclose(file); }
#endif
Well that works, thanks
Reply
#6

EDIT: Late! Ignore. Sorry, didn't actually read the post, just replied by the title! You want to check if a function exists somewhere else, and if not, then define it? Can you actually run a test with an empty print stock? A stock that simply prints. The problem may be in your stock. IIRC, file functions crash when the file handle is invalid, etc. So checks for that.. I can't view your code, sorry, on mobile.
Reply
#7

Quote:
Originally Posted by Rajat_Pawar
Посмотреть сообщение
EDIT: Late! Ignore. Sorry, didn't actually read the post, just replied by the title! You want to check if a function exists somewhere else, and if not, then define it? Can you actually run a test with an empty print stock? A stock that simply prints. The problem may be in your stock. IIRC, file functions crash when the file handle is invalid, etc. So checks for that.. I can't view your code, sorry, on mobile.
I did create a simple stock... All it did was print a string... It still crashed..
Reply
#8

bump
Reply
#9

Quote:
Originally Posted by dusk
Посмотреть сообщение
Well that works, thanks
Hi! Didn't you reply that it worked?
Reply
#10

I did... I guess bumping has become a habbit for me.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)