in reply to Re: Re: Re: Re: Win32 Shared Memory
in thread Win32 Shared Memory

The problem is your passing a string constant for the last parameter of OpenFileMapping(). Seems like it would be a safe bet I know, given that the prototype is defined as LPCTSTR.

The problem is that the C code underlying the API is expecting strings to be null terminated and Perls strings aren't by default. To correct this, Win32::API has to add a null before passing them along. Rather than copying the string you pass into a temporary variable and then appending the null, it simply tacks a null on the end of the string you pass--but if it is a constant, it's read-only and you get the error.

The upshot is, if you assign your constant to a variable, and then pass the variable, then null can be added successfully and the error goes away.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Win32 Shared Memory
by nornagon (Acolyte) on May 16, 2004 at 00:17 UTC
    Ok, cool, that worked :D

    One problem: I now get an error from Windows.

    Here's the code:

    #!/usr/bin/perl use warnings; use strict; use Win32::API; my $STANDARD_RIGHTS_REQUIRED = (0x000F0000); my $SECTION_QUERY = 0x0001; my $SECTION_MAP_WRITE = 0x0002; my $SECTION_MAP_READ = 0x0004; my $SECTION_MAP_EXECUTE = 0x0008; my $SECTION_EXTEND_SIZE = 0x0010; my $SECTION_ALL_ACCESS = ($STANDARD_RIGHTS_REQUIRED|$SECTION_Q +UERY| $SECTION_MAP_WRITE |$SECTION_MAP +_READ| $SECTION_MAP_EXECUTE |$SECTION_EXT +END_SIZE); Win32::API->Import('kernel32', 'HANDLE OpenFileMapping(DWORD dwDesired +Access, BOOL bInheritHandle, LPCTSTR lpName)'); Win32::API->Import('kernel32', 'LPVOID MapViewOfFile(HANDLE hFileMappi +ngObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFile +OffsetLow, SIZE_T dwNumberOfBytesToMap)'); my $C3_mem = "Creatures 3_mem"; my $mem_handle = OpenFileMapping($SECTION_ALL_ACCESS, 0, $C3_mem); my $mem_ptr = MapViewOfFile($mem_handle, $SECTION_ALL_ACCESS, 0, 0, 0) +;

    WinXP just throws a 'This program encountered an error' blah blah blah error.

      Did you check to see whether anything is being returned by OpenFileHandle()?

      Try

      my $mem_handle = OpenFileMapping( $SECTION_ALL_ACCESS, 0, $C3_mem ) or die "OpenFileHandle failed with: $^E";

      See $EXTENDED_OS_ERROR for the explanation of $^E.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
        Ok, now it's saying that the system could not find the file specified... I have the game running, so the file should exist...
Re: Re: Re: Re: Re: Re: Win32 Shared Memory
by ysth (Canon) on May 16, 2004 at 05:07 UTC
    It's a shame that Win32::API does this blindly, since perl strings do almost always have a null character already on the end. Certainly constant strings do. It would be easy enough for it to check before modifying...

    Does it in fact add the null and increase the length of the string? Or just add the null and leave length unchanged?

      I agree. Funnily enough, I think that this maybe a new feature as I don't recall not being able to use constant strings previously.

      The line of code in question from Win32::API::Type.pm(195) is

      $_[1] = unpack("Z*", $_[1]);

      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
        Ah, I'd thought this was XS...from Perl there's no good way to inspect the string buffer beyond the actual end of the perl string. But it does look like it unintentionally modifies the caller's parameter; probably not a good thing. Is there a better way to unalias one parameter than:
        splice(@_, 1, 1, unpack("Z*", $_[1]))