Win32::Process, SetProcessAffinityMask for an existing process =perl crash

Win32::Process, SetProcessAffinityMask for an existing process =perl crash

am 08.05.2006 00:22:02 von chacallot

Hi!

Was planning to use Win32::Process to set the ProcessAffinityMask of
some processes but this lead to pure and simple crash of perl.exe.

This is not a credential problem as I can set the process affinity if I
spawn a "process.exe" command to set the affinity.

Also, the process affinity is correctly set to the expected value...
Seems the crash occurs just after.

Here is a sample code causing a perl.exe crash (for example using
activeperl 5.6 or 5.8 (latest) on Windows 2000, XP or 2003) :
------------------
use strict;

use Win32::Process;
use Win32;

my $obj;
my $pid=2216;
my $iflags;


Win32::Process::Open($obj,$pid,$iflags) or die;


my $processAffinityMask;
my $systemAffinityMask;

$obj->GetProcessAffinityMask($processAffinityMask, $systemAffinityMask)
or die;

$obj->SetProcessAffinityMask($processAffinityMask) or die;
-------------------



I also experience strange behavior while using Win32::Daemon to make my
perl scripts act as an NT service (under Windows 2003 my script
unexpectedly says he receives SERVICE_STOP signals. And stops..).

Am feeling I should abandon perl to do system tasks on Windows systems.
Too bad cause I invested quite some time to get knowledgeable in perl...
And I must also improve in Java for weblogic stuff. So am not willing
to start learning yet another language like VB/.NET..

Rgds,
Chacallot.

Re: Win32::Process, SetProcessAffinityMask for an existing process = perl crash

am 08.05.2006 09:48:19 von Sisyphus

"chacallot"
..
>
> Here is a sample code causing a perl.exe crash (for example using
> activeperl 5.6 or 5.8 (latest) on Windows 2000, XP or 2003) :
> ------------------
> use strict;
>
> use Win32::Process;
> use Win32;
>
> my $obj;
> my $pid=2216;
> my $iflags;
>
>
> Win32::Process::Open($obj,$pid,$iflags) or die;
>
>
> my $processAffinityMask;
> my $systemAffinityMask;
>
> $obj->GetProcessAffinityMask($processAffinityMask, $systemAffinityMask)
> or die;
>
> $obj->SetProcessAffinityMask($processAffinityMask) or die;
> -------------------

That works fine for me on Windows2000 using MinGW-built perl 5.8.8 and
MinGW-built perl 5.6, but like you, I find it crashes with ActiveState perl
when SetProcessAffinityMask() gets called.

I don't know why it crashes with perl built using a Microsoft compiler (also
crashes with my VC7-built perl), and yet causes no problem with MinGW-built
perl.

If you don't find the answer here I would try posting to
comp.lang.perl.misc - and if that fails, try the perl-win32-users mailing
list hosted by ActiveState.

Cheers,
Rob

Re: Win32::Process, SetProcessAffinityMask for an existing process= perl crash

am 09.05.2006 15:36:16 von Reinhard Pagitsch

chacallot wrote:
> Hi!
>
> Was planning to use Win32::Process to set the ProcessAffinityMask of
> some processes but this lead to pure and simple crash of perl.exe.
>
> This is not a credential problem as I can set the process affinity if I
> spawn a "process.exe" command to set the affinity.
>
> Also, the process affinity is correctly set to the expected value...
> Seems the crash occurs just after.
>
> Here is a sample code causing a perl.exe crash (for example using
> activeperl 5.6 or 5.8 (latest) on Windows 2000, XP or 2003) :
> ------------------
> use strict;
>
> use Win32::Process;
> use Win32;
>
> my $obj;
> my $pid=2216;
> my $iflags;
>
>
> Win32::Process::Open($obj,$pid,$iflags) or die;
>
>
> my $processAffinityMask;
> my $systemAffinityMask;
>
> $obj->GetProcessAffinityMask($processAffinityMask, $systemAffinityMask)
> or die;
>
> $obj->SetProcessAffinityMask($processAffinityMask) or die;
> -------------------
>
>
>
> I also experience strange behavior while using Win32::Daemon to make my
> perl scripts act as an NT service (under Windows 2003 my script
> unexpectedly says he receives SERVICE_STOP signals. And stops..).
>
> Am feeling I should abandon perl to do system tasks on Windows systems.
> Too bad cause I invested quite some time to get knowledgeable in perl...
> And I must also improve in Java for weblogic stuff. So am not willing
> to start learning yet another language like VB/.NET..
>

The Problem is in the C/C++ source code. It uses a pointer to the
function defined in kernel32.dll. I changed the source to use the
SetProcessAffinityMask API directly and now there is no crash.

So I suggest to ask the owner of the module to change it.

regards,
Reinhard

Re: Win32::Process, SetProcessAffinityMask for an existing process= perl crash

am 10.05.2006 10:37:12 von Thomas Kratz

Reinhard Pagitsch wrote:
> chacallot wrote:

[ about Win32::Process::SetProcessAffinityMask crashing perl]

> The Problem is in the C/C++ source code. It uses a pointer to the
> function defined in kernel32.dll. I changed the source to use the
> SetProcessAffinityMask API directly and now there is no crash.

Could you please elaborate or provide a patch? As I read it (which could
of course be wrong), your statement suggests that there is a
'SetProcessAffinityMask' outside of kernel32.dll.

Thomas

--
$/=$,,$_=,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~...... >r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k ^.-

Re: Win32::Process, SetProcessAffinityMask for an existing process= perl crash

am 11.05.2006 11:37:03 von Reinhard Pagitsch

Hello Thomas,



Thomas Kratz wrote:
> Reinhard Pagitsch wrote:
>> chacallot wrote:
>
> [ about Win32::Process::SetProcessAffinityMask crashing perl]
>
>> The Problem is in the C/C++ source code. It uses a pointer to the
>> function defined in kernel32.dll. I changed the source to use the
>> SetProcessAffinityMask API directly and now there is no crash.
>
> Could you please elaborate or provide a patch? As I read it (which could
> of course be wrong), your statement suggests that there is a
> 'SetProcessAffinityMask' outside of kernel32.dll.

I think you missunderstood what I wrote. What I have done is not using
LoadLibrary() to import SetProcessAffinityMask function with
GetProcAddress(). I used the (API?) function as described in the
Platform SDK:
BOOL SetProcessAffinityMask( HANDLE hProcess, DWORD_PTR
dwProcessAffinityMask);

The patch:
In the Process.hpp:
class cProcess
{

.....

BOOL _SetProcessAffinityMask( DWORD dwProcessAffinityMask )
{
BOOL ret;
//if(pSetProcessAffinityMask)
ret = SetProcessAffinityMask( ph, dwProcessAffinityMask );
if(!ret) return GetLastError();
return ret;
//return FALSE;
}
....

};
(The original code is commented out with //.)


And in the Process.xs:
BOOL
SetProcessAffinityMask(cP,processAffinityMask)
cProcess *cP
DWORD processAffinityMask
CODE:
RETVAL = cP->_SetProcessAffinityMask(processAffinityMask);
OUTPUT:
RETVAL


Thats all, now I do not get a crash of my Perl v5.6.1.



regards,
Reinhard

Re: Win32::Process, SetProcessAffinityMask for an existing process= perl crash

am 11.05.2006 18:43:20 von Thomas Kratz

Hello Reinhard,

Reinhard Pagitsch wrote:
> I think you missunderstood what I wrote.

Yes, I was almost sure I did :-)

> What I have done is not using
> LoadLibrary() to import SetProcessAffinityMask function with
> GetProcAddress(). I used the (API?) function as described in the
> Platform SDK:
> BOOL SetProcessAffinityMask( HANDLE hProcess, DWORD_PTR
> dwProcessAffinityMask);

Ah, I see. I don't understand why the LoadLibrary is done at all. Every
extension is linked against kernel32.lib anyway, which results in the
functions being called dynamically through kernel32.dll.

Thanks,
Thomas

--
$/=$,,$_=,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~...... >r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k ^.-

Re: Win32::Process, SetProcessAffinityMask for an existing process = perl crash

am 17.05.2006 21:08:07 von chacallot

Thx for your answers.

Emailed info about this thread to the module author.
Hope he'll be able to notice it in all the spam he's probably getting.

His email is an @activestate.com. Lets see how active those are...

Regards,
Chacallot.