http://www.perlmonks.org?node_id=981427

xiaoyafeng has asked for the wisdom of the Perl Monks concerning the following question:

Hi, monks

I've been writing a watchdog with Win32::Process::List. the script itself is extremely simple, monitor processes running and spawn a new one if the process which was monitor quit.

the interesting is where I browse in the W::P::L XS code

.......... pe32.dwSize = sizeof( PROCESSENTRY32 ); if( !Process32First( hProcessSnap, &pe32 ) ) { printError(wszMsgBuff,&err ); sv_upgrade(perror,SVt_PVIV); sv_setpvn(perror, (char*)wszMsgBuff, strlen(wszMsgBuff)); sv_setiv(perror,(IV) err); SvPOK_on(perror); XPUSHs(sv_2mortal(newSViv(-1))); CloseHandle( hProcessSnap ); } else { do { sprintf(temp, "%d", pe32.th32ProcessID); if(debug==1) { printf("Temp: %s\n",pe32.szExeFile); } if(hv_store(rh,temp,strlen(temp),newSVpv(pe32.szExeF +ile, strlen(pe32.szExeFile)), 0)==NULL) //if(hv_store(rh,pe32.szExeFile,strlen(pe32.szExeFil +e),newSVuv(pe32.th32ProcessID), 0)==NULL) { printf("can not store %s in hash!\n", pe32.szExe +File); } } while( Process32Next( hProcessSnap, &pe32 ) ); CloseHandle( hProcessSnap ); .....
and here is the definition of structure PROCESSENTRY32
typedef struct tagPROCESSENTRY32 { DWORD dwSize; DWORD cntUsage; DWORD th32ProcessID; ULONG_PTR th32DefaultHeapID; DWORD th32ModuleID; DWORD cntThreads; DWORD th32ParentProcessID; LONG pcPriClassBase; DWORD dwFlags; TCHAR szExeFile[MAX_PATH]; } PROCESSENTRY32, *PPROCESSENTRY32;
As above szExeFile is a TCHAR arary, and actually, in my computer, it's a WCHAR array. the author of W::P::L put a wchar_t into a sv directly by sv_setpv. and it seems right when I check typemap under ExtUtils:
wchar_t * T_PV ....... ....... T_PV sv_setpv((SV*)$arg, $var);
But wield is, when I put the similar c code in VS2010, it complains:
: error C2664: 'Perl_sv_setpv' : cannot convert parameter 3 from 'WCHA +R [260]' to 'const char *const ' (I think it more makes sense tho)

So what is perl/XS does to make building this module correctly?





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