Hi ,

I have been trying to use the win32::API:Struct for a while and mostly it works fine :)
However i have ran into trouble with using structs within structs ... I dont know how to retrieve values from the nested struct ..

example:

Win32::API::Struct->typedef('MDS_Forwarding', qw(
	char Address[257];
	char Host[64];
	char SendAs[128];
	char Port[8];
)) or die "Typedef error $!\n";

Win32::API::Struct->typedef('MDS_Block', qw(
	MDS_Forwarding User;
	char Name[64];
)) or die "Typedef error $!\n";

if i define a MDS_Block struct like this:

my $Block = Win32::API::Struct->new( 'MDS_Block' );

How do i retrieve the nested Address value for the $Block struct??

Thanks in advance!
Patrik K

Recommended Answers

All 5 Replies

I'm pretty sure that if you tie the first struct, you can reference it as a hash, and then it will be like having a hash of structs. I could be wrong, but have you looked at:
http://search.cpan.org/~acalpini/Win32-API-0.41/Struct.pm

Read the section near the bottom that discusses using tied structs, and hopefully that will help. Let me know how it turns out :)

Hi Comatose and thanks for answering! :)

im not really sure what you mean, what im trying to do is the following:

the structs are defined in API documentation for an application i want to control by perl. A pointer to this struct is sent in an api call and the application fills the struct with data. I then want to read and/or change the data in the struct as applicable , then another dll call submits the edited struct to the application.

If I understood you correctly you suggested to tie the first (the one that is nested inside the second) but I never instance the first struct .. it is only defined so that the struct i want to use for the dll call can be properly defined.

I mistakenly thought something like this would work to access it..:)

$value=$Block->{User}->{Address};
or with tie:
$value=$Block{User}->{Address};

next i tried this:

foreach my $K (keys($Block{User})) {print "\n$K";}

but no luck...
if you have any more tips im all ears :)
/P

and this is what a dump shows :

$VAR1 = 'User';
$VAR2 = bless( {
                 'typedef' => [
                                [
                                  'Address',
                                  'c*257',
                                  'char'
                                ],
                                [
                                  'Host',
                                  'c*64',
                                  'char'
                                ],
                                [
                                  'SendAs',
                                  'c*128',
                                  'char'
                                ],
                                [
                                  'Port',
                                  'c*8',
                                  'char'
                                ]
                              ],
                 '__typedef__' => 'MDS_Forwarding'
               }, 'Win32::API::Struct' );
$VAR3 = 'Name';
$VAR4 = undef;

I'm out of ideas on this one. I know that I was hoping that using tie would would allow you to access the elements of the child struct by using the parent struct as a hash-like system. I'm sorry, but I don't know how to do what you want. If you figure out a solution, please post so that I, and others who are dumbfounded by this, have the solution available. I would normally have suggested the use of a parent hash, with a child struct, but I know that wouldn't work for your project. Please let us know if you find the solution.

Hi again,

Ok ill keep trying on this one .. if i find a solution i will post it here
thanks for trying though :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.