Problem in accessing DLL function from perl module.

Problem in accessing DLL function from perl module.

am 31.07.2007 10:52:01 von Sachin

Hi I am trying to access DLL function(Dll is developed in VC++).from
perl module.
Here is my code :
The following function is present in cfm.pm module.
sub CheckFileInTOC
{
my($FileIndex) = @_;
$GetHandle = new
Win32::API('perlfunction.dll','fnGetTOCContent','I','I');

# $GetHandle = new
Win32::API('clisocketsd.dll','fnInitCLISocket','I','I');
if(not defined $GetHandle) {
die "Can't import API fnGetTOCContent: $!\n";
}

$ReturnVal = $GetHandle->Call($FileIndex);

print("We are in cfm module now\n");
}

DLL function is as follows
int fnGetTOCContent(int nFileName )
{
printf("Hello you are in dll\n");
return 1;
}

When I call CheckFileInTOC function of cfm.pm, it calls function from
DLL and prints on console :Hello You are in dll.
But after this statement I get the following error.
The instruction at "0x28040110"refernced memory at "0x00000004".The
memory could not be "written".
When I debug this Application error in perl.exe I again get 1 message
box displaying
Unhandled exception in perl.exe(PERL58.DLL):0xC0000005:Access
Violation.


Can anyone tell me what is the solution for this problem.
Thanks in advance.

Re: Problem in accessing DLL function from perl module.

am 31.07.2007 14:47:42 von Sisyphus

"sachin" wrote in message
news:1185871921.807004.257560@k79g2000hse.googlegroups.com.. .
> Hi I am trying to access DLL function(Dll is developed in VC++).from
> perl module.
> Here is my code :
> The following function is present in cfm.pm module.
> sub CheckFileInTOC
> {
> my($FileIndex) = @_;
> $GetHandle = new
> Win32::API('perlfunction.dll','fnGetTOCContent','I','I');
>
> # $GetHandle = new
> Win32::API('clisocketsd.dll','fnInitCLISocket','I','I');
> if(not defined $GetHandle) {
> die "Can't import API fnGetTOCContent: $!\n";
> }
>
> $ReturnVal = $GetHandle->Call($FileIndex);
>
> print("We are in cfm module now\n");
> }
>
> DLL function is as follows
> int fnGetTOCContent(int nFileName )
> {
> printf("Hello you are in dll\n");
> return 1;
> }
>
> When I call CheckFileInTOC function of cfm.pm, it calls function from
> DLL and prints on console :Hello You are in dll.
> But after this statement I get the following error.
> The instruction at "0x28040110"refernced memory at "0x00000004".The
> memory could not be "written".

Looks to me that code should work.
Check that $FileIndex is in fact an integer.
If it is the number (eg) 9, that should be fine. But if it was the string
'9' (or a non-numeric string), that might cause problems. (I don't know if
Win32::API converts numeric strings to numbers as happily as perl does.)

Cheers,
Rob

Re: Problem in accessing DLL function from perl module.

am 31.07.2007 16:29:59 von Petr Vileta

Sisyphus wrote:
> "sachin" wrote in message
> news:1185871921.807004.257560@k79g2000hse.googlegroups.com.. .
>> Hi I am trying to access DLL function(Dll is developed in VC++).from
>> perl module.
>> Here is my code :
>> The following function is present in cfm.pm module.
>> sub CheckFileInTOC
>> {
>> my($FileIndex) = @_;
>> $GetHandle = new
>> Win32::API('perlfunction.dll','fnGetTOCContent','I','I');
>>
>> # $GetHandle = new
>> Win32::API('clisocketsd.dll','fnInitCLISocket','I','I');
>> if(not defined $GetHandle) {
>> die "Can't import API fnGetTOCContent: $!\n";
>> }
>>
>> $ReturnVal = $GetHandle->Call($FileIndex);
>>
>> print("We are in cfm module now\n");
>> }
>>
>> DLL function is as follows
>> int fnGetTOCContent(int nFileName )
>> {
>> printf("Hello you are in dll\n");
>> return 1;
>> }
>>
>> When I call CheckFileInTOC function of cfm.pm, it calls function from
>> DLL and prints on console :Hello You are in dll.
>> But after this statement I get the following error.
>> The instruction at "0x28040110"refernced memory at "0x00000004".The
>> memory could not be "written".
>
> Looks to me that code should work.
> Check that $FileIndex is in fact an integer.
> If it is the number (eg) 9, that should be fine. But if it was the
> string '9' (or a non-numeric string), that might cause problems. (I
> don't know if Win32::API converts numeric strings to numbers as
> happily as perl does.)
> Cheers,
> Rob
Every time I'm not sure if variable is numeric or string then I use some
like this

$ReturnVal = $GetHandle->Call(1 * $FileIndex);

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)

Re: Problem in accessing DLL function from perl module.

am 01.08.2007 01:35:30 von RobMay

On Jul 31, 9:52 am, sachin wrote:
> Hi I am trying to access DLL function(Dll is developed in VC++).from
> perl module.

>
> DLL function is as follows
> int fnGetTOCContent(int nFileName )
> {
> printf("Hello you are in dll\n");
> return 1;
>
> }
>
> When I call CheckFileInTOC function of cfm.pm, it calls function from
> DLL and prints on console :Hello You are in dll.
> But after this statement I get the following error.
> The instruction at "0x28040110"refernced memory at "0x00000004".The
> memory could not be "written".
> When I debug this Application error in perl.exe I again get 1 message
> box displaying
> Unhandled exception in perl.exe(PERL58.DLL):0xC0000005:Access
> Violation.

try making your DLL function look like this:

int STDCALL fnGetTOCContent(int nFileName )
{
printf("Hello you are in dll\n");
return 1;

}

See if that help.

Rob.

Re: Problem in accessing DLL function from perl module.

am 01.08.2007 03:19:08 von Sisyphus

"Robert May" wrote in message
..
..
> try making your DLL function look like this:
>
> int STDCALL fnGetTOCContent(int nFileName )
> {
> printf("Hello you are in dll\n");
> return 1;
>
> }
>

Yes - there are usually problems if the dll functions don't use the
__stdcall calling convention. From memory, however, the error that one
experiences in such situations is *not* a segfault. (I thought that, in such
situations, the Win32::API->new() call fails.) But my memory is often
faulty, so this is definitely worth a try.

However, does "STDCALL" actually work ? Faik it might - as I've never tried
it. I've always specified "__stdcall".

An alternative to explicitly inserting the "__stdcall" into each of the
functions is to simply provide the /Gz switch when compiling the dll.

Cheers,
Rob

Re: Problem in accessing DLL function from perl module.

am 01.08.2007 09:13:33 von Sachin

On Jul 31, 5:47 pm, "Sisyphus" wrote:
> "sachin" wrote in message
>
> news:1185871921.807004.257560@k79g2000hse.googlegroups.com.. .
>
>
>
>
>
> > Hi I am trying to access DLL function(Dll is developed in VC++).from
> > perl module.
> > Here is my code :
> > The following function is present in cfm.pm module.
> > sub CheckFileInTOC
> > {
> > my($FileIndex) = @_;
> > $GetHandle = new
> > Win32::API('perlfunction.dll','fnGetTOCContent','I','I');
>
> > # $GetHandle = new
> > Win32::API('clisocketsd.dll','fnInitCLISocket','I','I');
> > if(not defined $GetHandle) {
> > die "Can't import API fnGetTOCContent: $!\n";
> > }
>
> > $ReturnVal = $GetHandle->Call($FileIndex);
>
> > print("We are in cfm module now\n");
> > }
>
> > DLL function is as follows
> > int fnGetTOCContent(int nFileName )
> > {
> > printf("Hello you are in dll\n");
> > return 1;
> > }
>
> > When I call CheckFileInTOC function of cfm.pm, it calls function from
> > DLL and prints on console :Hello You are in dll.
> > But after this statement I get the following error.
> > The instruction at "0x28040110"refernced memory at "0x00000004".The
> > memory could not be "written".
>
> Looks to me that code should work.
> Check that $FileIndex is in fact an integer.
> If it is the number (eg) 9, that should be fine. But if it was the string
> '9' (or a non-numeric string), that might cause problems. (I don't know if
> Win32::API converts numeric strings to numbers as happily as perl does.)
>
> Cheers,
> Rob- Hide quoted text -
>
> - Show quoted text -

Well i have checked it , I am passing number only but still it is
getting crashed.

Re: Problem in accessing DLL function from perl module.

am 01.08.2007 09:32:43 von Sachin

On Aug 1, 6:19 am, "Sisyphus" wrote:
> "Robert May" wrote in message
>
> .
> .
>
> > try making your DLL function look like this:
>
> > int STDCALL fnGetTOCContent(int nFileName )
> > {
> > printf("Hello you are in dll\n");
> > return 1;
>
> > }
>
> Yes - there are usually problems if the dll functions don't use the
> __stdcall calling convention. From memory, however, the error that one
> experiences in such situations is *not* a segfault. (I thought that, in such
> situations, the Win32::API->new() call fails.) But my memory is often
> faulty, so this is definitely worth a try.
>
> However, does "STDCALL" actually work ? Faik it might - as I've never tried
> it. I've always specified "__stdcall".
>
> An alternative to explicitly inserting the "__stdcall" into each of the
> functions is to simply provide the /Gz switch when compiling the dll.
>
> Cheers,
> Rob

Thanks Rob I have used this /Gz switch and now my DLL is working fine.
Thanks a lot.

Re: Problem in accessing DLL function from perl module.

am 01.08.2007 12:30:34 von RobMay

On Aug 1, 2:19 am, "Sisyphus" wrote:
> "Robert May" wrote in message
> > try making your DLL function look like this:
>
> > int STDCALL fnGetTOCContent(int nFileName )
> > {
> > printf("Hello you are in dll\n");
> > return 1;
>
> > }
>
> Yes - there are usually problems if the dll functions don't use the
> __stdcall calling convention. From memory, however, the error that one
> experiences in such situations is *not* a segfault. (I thought that, in such
> situations, the Win32::API->new() call fails.) But my memory is often
> faulty, so this is definitely worth a try.

Current versions of Win32::API don't cope with any calling convention
other than __stdcall, and will segfault (or similar) due to stack
corruption when used to call a function compiled with other calling
conventions (esp. __cdecl, which is the default for most C compilers -
although as you point out later it is usually possible to change
this).

> However, does "STDCALL" actually work ? Faik it might - as I've never tried
> it. I've always specified "__stdcall".

My bad. WINAPI was what I meant, which is a macro defined in the
windows header files that expands to __stdcall.

> An alternative to explicitly inserting the "__stdcall" into each of the
> functions is to simply provide the /Gz switch when compiling the dll.

This is OK, so long as you don't ever expect anyone else to compile
you library. In general it's better to explicitly state the calling
convention in the source, and that way you're not dependent on
remembering to add the right switches to the compile-line.

Regards,
Rob.