Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

what do PERL_SYS_INIT3() and PERL_SYS_TERM() do?

by xiaoyafeng (Chaplain)
on Dec 30, 2011 at 21:21 UTC ( #945683=perlquestion: print w/ replies, xml ) Need Help??
xiaoyafeng has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I've tried to embed perl into a winform program written in C++/CLI recently. but got stuck with an conflict between managed style main function and PERL_SYS_INIT3 macro. the issue is compiler always complain if I put PERL_SYS_INIT3 into managed main body.(of course, it can't find any argc,argv adress in CLI). However what make me surprised, if I comment it everything seems OK. I mean, program could be compiled, and run correctly. Below is partial codes:

int main(array<System::String ^> ^args) { //PERL_SYS_INIT3(0,'ss','dd'); my_perl = perl_alloc(); perl_construct(my_perl); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; // Enabling Windows XP visual effects before any controls are crea +ted Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); // Create the main window and run it Application::Run(gcnew Form1()); // perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); return 0; }

here is said in perl api:

The macros PERL_SYS_INIT3() and PERL_SYS_TERM() provide system-specific tune up of the C runtime environment necessary to run Perl interpreters; they should only be called once regardless of how many interpreters you create or destroy. Call PERL_SYS_INIT3() before you create your first interpreter, and PERL_SYS_TERM() after you free your last interpreter. So what these 2 macros do behind? it does tune up? will it bring some issues when I comments it?

thanks in advance!

In addition, perl_destruct(my_perl) can't be uncommented too,otherwise it will throw:

First-chance exception at 0x7c84cd02 in perl_form.exe: 0xC0000005: Acc +ess violation writing location 0x00000014. A first chance exception of type 'System.AccessViolationException' occ +urred in perl_form.exe An unhandled exception of type 'System.AccessViolationException' occur +red in perl_form.exe Additional information: Attempted to read or write protected memory. T +his is often an indication that other memory is corrupt.

UPDATE:

thank patcat88, it's like that PERL_SYS_INIT3 JUST run InitializeCriticalSection (&(*Perl_Gperlio_mutex_ptr (0))); So I pass NULL to it and by far everything seems ok. I'll do more tests.

int main(array<System::String ^> ^args) { PERL_SYS_INIT3((int *)NULL,(char ***)NULL,(char ***)NULL); my_perl = perl_alloc(); perl_construct(my_perl); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; // Enabling Windows XP visual effects before any controls are crea +ted Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); // Create the main window and run it Application::Run(gcnew Form1()); perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); return 0; }




I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Comment on what do PERL_SYS_INIT3() and PERL_SYS_TERM() do?
Select or Download Code
Re: what do PERL_SYS_INIT3() and PERL_SYS_TERM() do?
by patcat88 (Deacon) on Dec 31, 2011 at 05:30 UTC
    #define PERL_SYS_INIT3(argc, argv, env) Perl_sys_init3(argc, argv, +env)
    void Perl_sys_init3 (int *argc, char ***argv, char ***env) { extern int Perl___notused; (void) ((argc) || (_assert ("argc", "perl.c", 145), 0)); (void) ((argv) || (_assert ("argv", "perl.c", 145), 0)); (void) ((env) || (_assert ("env", "perl.c", 145), 0)); ((void) argc); ((void) argv); ((void) env); Perl_win32_init (argc, argv); InitializeCriticalSection (&(*Perl_Gperlio_mutex_ptr (0))); }
    void Perl_win32_init (int *argcp, char ***argvp) { HMODULE module; _control87 (0x0008001f, 0x0008001f); #line 4840 "win32.c" ; InitCommonControls (); module = GetModuleHandleA ("ntdll.dll"); if (module) { *(FARPROC *) & pfnZwQuerySystemInformation = GetProcAddress (mod +ule, "ZwQuerySystemInformation"); } module = GetModuleHandleA ("kernel32.dll"); if (module) { *(FARPROC *) & pfnCreateToolhelp32Snapshot = GetProcAddress (mod +ule, "CreateToolhelp32Snapshot"); *(FARPROC *) & pfnProcess32First = GetProcAddress (module, "Proc +ess32First"); *(FARPROC *) & pfnProcess32Next = GetProcAddress (module, "Proce +ss32Next"); } g_osver.dwOSVersionInfoSize = sizeof (g_osver); GetVersionExA (&g_osver); ansify_path (); }
    #define PERL_SYS_TERM() Perl_sys_term()
    void Perl_sys_term () { extern int Perl___notused; if (!(*Perl_Gveto_cleanup_ptr (0))) { Perl_win32_term (); } }
    before
    void Perl_win32_term(void) { dTHX; HINTS_REFCNT_TERM; OP_REFCNT_TERM; PERLIO_TERM; MALLOC_TERM; }
    after
    void Perl_win32_term (void) { PerlInterpreter *my_perl = ((PerlInterpreter *) Perl_get_context ()) +; DeleteCriticalSection (&(*Perl_Ghints_mutex_ptr (0))); DeleteCriticalSection (&(*Perl_Gop_mutex_ptr (0))); do { PerlIO_teardown (); DeleteCriticalSection (&(*Perl_Gperlio_mutex_ptr (0))); } while (0); ; }
    I agree, terrible terrible description in perlapi. Perl 5.12.2 on windows above. Use the -P flag, and then a code formater. On some makefiles (XS/makemaker ones, not perl interpreter makefile) you can just type "nmake somecfile.i" and it will make the post preprocessed file, you still need to run it through a code formatter for it to be remotely readable. The Perl interpreter is the first interpreted programing language written completely in preprocessor macros jk jk

    It seems to me like you have to call then once per process. They set up process global, not ithread global (or is that local?), things perl uses.

    Also watch out for insane Perls taken your c standard lib hostage problems, Perl's headers redefine all of C standard library to its own emulations, see fakesdio.h in perl.git and XSUB.h#l563 in perl.git and win32/win32iop-o.h#l292 in perl.git. It will cause endless crashes and headaches when trying to interface Perl with anything else that is written in C. Make sure you have tons of undefs to return you back to your normal c standard library.

    Why Perl takes over your cstdlib? Who knows, perlclib has the Perl specific names for Perl's clib.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://945683]
Approved by Corion
Front-paged by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2014-10-22 08:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (114 votes), past polls