Link to home
Start Free TrialLog in
Avatar of sapbucket
sapbucket

asked on

How to handle SCALAR memory address pointer?

Is the following supported by PERL?

$address = MEMORY_ADDRESS_AS_VALUE; # i.e.: "145489952" is the address scalar value

# now we treat $address as an array reference:
$address[0] = ...
$address[1] = ...
$address[2] = ...
etc.

Basically, if I know the ACTUAL PHYSICAL ADDRESS, as a scalar value, how can I use it to access an array that begins at that address?

I know that C can handle such a thing. In C, just set the pointer to a memory address and use it to reference starting at that address.

THanks!

sapbucket
Avatar of sapbucket
sapbucket

ASKER

or, put succinctly:

how to access a known memory location, represented as a scalar value, using PERL?
What a mess...

a. even at C you almost never know the "ACTUAL PHYSICAL ADDRESS" of a variable. You know the logical address of it, in the process virtual memory space.

b. the (wrong) equation "pointer=array" comes totally from c (in its roots in assmbly). To the best of my knowledge, in perl you can have no guarantee that members of an array will be in continuous memory. Even if so, the similiar syntax between pointers and arrays in C is just not there in Perl.

c. If I understand correctly, and this array/pointer comes from C function, and no one touches this array/pointer from when it's returned to your reading/writing it MAY be possible to manipulate it, by jumping the reference with sizeof(array_element). But, it is possible that your writing (from perl) will overwrite more than you know and want.
Avatar of ozo
You might be able to
  use PeekPoke qw(peek);
  my $val = peek($address);
But what are you actually trying to accomplish?
Perhaps there is a better way to achieve your goals
ozo,

  I have been trying to use an ActiveX control that provides methods and properties for a firewire camera that I purchased at www.imagingcontrol.com.

  I am using the camera for pattern recognition in an assembly line-like process.

  The activeX control manages an imageBuffer in RAM. I lose all performance capabilities if I have to write all images to file as .bmp, etc. So I am trying to use the ptr to the imagebufffer and do direct image processing in the system RAM.

  Through the functionality of the ActiveX control I can access the image buffer, perform image processing tasks, and save the buffer. However, this being an Microsoft OLE ActiveX control, it was designed specifically for VB6. Using any other language is a "work around."

  I have never learned VB6, and I have already written an image processing library in PERL. I could use Java, but I encounter the same problem of not being able to access the ActiveX control.

  In actuality, all my problems would go away if i wrote my whole application in VB :)

  But for me to switch I will have to learn a whole new IDE, language, mode of thinking, etc.

 
  You are right. There must be a better way to achieve my goals.



All of the following questions are related to this one: (all 500 points)
https://www.experts-exchange.com/questions/21223912/Need-help-understanding-references-urgent.html
https://www.experts-exchange.com/questions/21221422/Anyone-understand-the-Variant-datatype.html
https://www.experts-exchange.com/questions/21220762/PERL-Don't-understand-the-Variant-type-returned-by-COM-objects.html
 
If this activex has an OLE interface, you may be able to work through Win32::OLE.
I've had good and bad experience with some types of OLEs. So your mileage may vary.

Activestate has an OLE-browser, similiar to the one that MS has in VB. I would try to look there for the relevant functions.
If the camera API has no other appropriate methods, PeekPoke, or a similar C xs may be your only viable option in Perl.
OK,

   This is so simple in C.

   Can PERL do the following? (c-code example)

<START EXAMPLE>
   byte *pointer = ImageDataPtr();      # ImageDataPtr returns long (the location of the first byte)
   &pointer = 255 - &pointer;      // Change the content of the byte
   pointer++; // get the next byte
<END EXAMPLE>


ozo, I thought maybe using Inline C would work because I could embed the C functionality in a PERL script. (https://www.experts-exchange.com/questions/21201452/PERL-Using-Inline-C-for-Windows-Urgent.html). You think that would work? I'm not sure how to expose the function ImageDataPtr() using C, but I can with PERL: so, I will need to pass a value to the inline C. I read somewhere that this is possible...I'll look into it.

PERL doesn't seem to provide built-in functionality for handling pointers.

At my skill level I didn't realize that there is a difference between a PERL REFERENCE and a C POINTER. Not the same at all.


Thanks for any suggestions everyone. If you can impliment the 3-line C-example in this post using PERL I will be most happy.

use PeekPoke qw(peek poke);
$pointer = ImageDataPtr();
poke($pointer,255-peek($pointer));
$pointer++;


use Inline C;
C(ImageDataPtr());
__END__
__C__
int C(byte *pointer){
   *pointer = 255 - *pointer;
   pointer++;
}
ozo,

  byte* is not supported by lib/ExtUtils/typemap. Nor is long*.
 
  Unfortunately, long, is what I have for a return value from ImageDataPtr(). And long * is supported so I have to convert. So instead I pass in as char * and use atol() to convert to long. From there I use a type cast conversion into an unsigned char* (basically a byte) and can "see" the byte using printf.


TO ANSWER THIS:
you must look in your perl distribution directory under lib/ExtUtils/typemap to see if you are using supported types. Then it is not at all easy to proceed...

How to proceed AFTER that is here:
https://www.experts-exchange.com/questions/21238875/Perl-InlineC-not-working.html

 
I'm not sure if we should continue this question - there really isn't a good answer by anyone. Any arguments?
Is it ok if the C function calls ImageDataPtr() instead of it being passed from perl?
This problem has been solved,

   thanks for the help! Unfortunately none of the comments here satisfy the objective of the question.

   If there are no arguments I am going to close this question.
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial