Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: wchar_t*, char* and perl XS

by BrowserUk (Patriarch)
on Jul 13, 2012 at 06:18 UTC ( [id://981563]=note: print w/replies, xml ) Need Help??


in reply to wchar_t*, char* and perl XS

With a couple of minor changes, your code compiles and runs for me:

///#include "stdafx.h" #include <windows.h> #include <tlhelp32.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "perl.h" #include "EXTERN.h" #include "XSUB.h" PerlInterpreter *my_perl; BOOL GetProcessList( ); int main(int argc, TCHAR* argv[]) { PERL_SYS_INIT3(NULL, NULL, NULL); my_perl = perl_alloc(); perl_construct(my_perl); GetProcessList( ); perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); return 0; } BOOL GetProcessList( ) { HANDLE hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS +, 0 ); PROCESSENTRY32 pe32; pe32.dwSize = sizeof( PROCESSENTRY32 ); while( Process32Next( hProcessSnap, &pe32 ) ) { SV* name_sv = newSV(0); printf("process name is %s \n", pe32.szExeFile); sv_setpv((SV*)name_sv, pe32.szExeFile); } CloseHandle( hProcessSnap ); return( TRUE ); }

Compile & link:

C:\test>cl /W3 /I C:\perl64\lib\CORE junk.c C:\perl64\lib\CORE\perl510 +.lib Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64 Copyright (C) Microsoft Corporation. All rights reserved. junk.c Microsoft (R) Incremental Linker Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved. /out:junk.exe junk.obj C:\perl64\lib\CORE\perl510.lib

And run:

C:\test>junk.exe process name is [System Process] process name is System process name is smss.exe process name is csrss.exe process name is wininit.exe process name is csrss.exe process name is services.exe process name is lsass.exe process name is lsm.exe process name is svchost.exe process name is winlogon.exe process name is svchost.exe process name is svchost.exe process name is svchost.exe process name is svchost.exe process name is audiodg.exe process name is SLsvc.exe process name is svchost.exe process name is svchost.exe process name is spoolsv.exe process name is dwm.exe process name is taskeng.exe process name is Core Temp.exe process name is speedfan.exe process name is taskeng.exe process name is procexp64.exe process name is SearchIndexer.exe process name is svchost.exe process name is cmd.exe process name is mobsync.exe process name is TextPad.exe process name is LogonUI.exe process name is cmd.exe process name is cmd.exe process name is calc.exe process name is opera.exe process name is explorer.exe process name is TOTALCMD.EXE process name is firefox.exe process name is cmd.exe process name is junk.exe

See how you get on with my version?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Replies are listed 'Best First'.
Re^2: wchar_t*, char* and perl XS
by xiaoyafeng (Deacon) on Jul 13, 2012 at 13:13 UTC
    AH!! I use cl.exe in command prompt, it compiles fine! So it means visual studio does many things underground?! Besides, I find using wcstombs can eliminate this error (in Visual studio).




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

      I've never tried use the VS GUI to compile a XS DLL since VS doesn't know what xsubpp is, plus there are many macros that are defined at the cmd line. You can compile on the command line then debug the DLL in the VS GUI. In the makefile.pl put "XSOPT => ' -nolinenumbers '," in the %config EUMM hash so you see the original C file in the debugger and not the XS file which isn't valid C code.

      On second thought, you are not using XS and are not making a DLL and have no makefile, you are embedding the Perl Interp. Choice 1, switch the project in the VS GUI from UNICODE mode to MBCS mode . See http://www.d3dcoder.net/Data/Book2/Book2Setup.pdf#5

      Choice 2, is to manually define a PROCESSENTRY32A that MS never created. From my around 2003 platform sdk.
      typedef struct tagPROCESSENTRY32W { DWORD dwSize; DWORD cntUsage; DWORD th32ProcessID; // this process ULONG_PTR th32DefaultHeapID; DWORD th32ModuleID; // associated exe DWORD cntThreads; DWORD th32ParentProcessID; // this process's parent process LONG pcPriClassBase; // Base priority of process's thre +ads DWORD dwFlags; WCHAR szExeFile[MAX_PATH]; // Path } PROCESSENTRY32W; typedef PROCESSENTRY32W * PPROCESSENTRY32W; typedef PROCESSENTRY32W * LPPROCESSENTRY32W; ......................... typedef struct tagPROCESSENTRY32 { DWORD dwSize; DWORD cntUsage; DWORD th32ProcessID; // this process ULONG_PTR th32DefaultHeapID; DWORD th32ModuleID; // associated exe DWORD cntThreads; DWORD th32ParentProcessID; // this process's parent process LONG pcPriClassBase; // Base priority of process's thre +ads DWORD dwFlags; CHAR szExeFile[MAX_PATH]; // Path } PROCESSENTRY32; typedef PROCESSENTRY32 * PPROCESSENTRY32; typedef PROCESSENTRY32 * LPPROCESSENTRY32; #ifdef UNICODE #define Process32First Process32FirstW #define Process32Next Process32NextW #define PROCESSENTRY32 PROCESSENTRY32W #define PPROCESSENTRY32 PPROCESSENTRY32W #define LPPROCESSENTRY32 LPPROCESSENTRY32W #endif // !UNICODE
      Choice 3 is to undef those last 3 lines and use the A postfix functions. Choice 4, if you HAVE to have use the wide APIs, and pass unicode into Perl language, that is a completely different topic, I suggest doing exactly what is done here https://github.com/jandubois/win32/blob/841409b37dea45266c2afb68919d80c4e5dea657/Win32.xs#L174 for converting UTF16 into "Perl Unicode".

      You can also rethink your design and make a XS module instead of embedding Perl into your own application. There has to be some Perl language code you want to run right?

        Thanks bulk88!!

        Your reply makes me clean up! VS GUI default set code to UNICODE(wide char), but gcc or cl.exe won't. So to me, choice 1 is the best, coz it keeps embeding perl(ANSI) and C CODE consistent.

        BTW. for choice 4, personally, I think it's a standard solution of converting wide chars to perl. why nobody modify it a little to suit for importing to perlAPI other than just stay in win32.xs?





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

      So it means visual studio does many things underground?!

      Probably. I don't know because I've never used it.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

        I've never used it.

        Sadly, I have to, I can't imagine how code C# etc without VS, and company paid money for that.... lol





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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://981563]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found