Help with FFI structure pointer

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with FFI structure pointer

Ron Teitelbaum
All,

I'm sure someone saw this coming.  I could use some help.

Ok I built a structure but the use of the structure is kinda odd.

What I need to do is pass the structure in as null for the first call. Then
I need to pass the real structure in for subsequent calls.
Finally I need to free the structure through the API using the structure
pointer.

I handled the first two problems by implementing

<apicall: MyStruct 'MyAPICall' (long) module: 'my.dll'>  and sending in nil.
And
<apicall: MyStruct 'MyAPICall' (MyStruct) module: 'my.dll'>  <- this is so
cool!  I assume that Andreas wrote this code, it is beautiful!

Ok now here it the problem:

I have to call

<apicall: void 'FreeContext' (MyStruct *) module: 'my.dll'>  or something.  

The pointer needs to be sent to the apicall.  Did I do this correctly (using
MyStruct *) and it just doesn't work or is there another way to get the
handle of the structure.  The structure itself has a handle ivar but it's a
byteArray.

The result using MyStruct * is:  
---------------------------------------------------------------------
Tue Aug 01 14:28:54 2006

Exception code: C0000005
============
which appears to be a buffer overrun:
// MessageId: STATUS_ACCESS_VIOLATION
//
// MessageText:
//
//  The instruction at "0x%08lx" referenced memory at "0x%08lx". The memory
could not be "%s".
//
#define STATUS_ACCESS_VIOLATION          ((NTSTATUS)0xC0000005L)    // winnt
============
Exception addr: 77A8CE7E
Access violation (read access) at 0000000D
EAX:000C04E0 EBX:7C809794 ECX:00000000 EDX:00000001
ESI:00000001 EDI:000C04E0 EBP:0006FB80 ESP:0006FB74
EIP:77A8CE7E EFL:00210202
FP Control: FFFF037F
FP Status:  FFFF4000
FP Tag:     FFFFFFFF
VM Version: Squeak 3.7.1 (release) from Sep 23 2004
Compiler: gcc 2.95.2 19991024 (release)

Current byte code: 225
Primitive index: 120

Loaded plugins:
        SoundGenerationPlugin 23 September 2004 (i)
        SoundPlugin 23 September 2004 (i)
        crypt32.dll
        SqueakFFIPrims 23 September 2004 (e)
        ZipPlugin 23 September 2004 (i)
        SocketPlugin 23 September 2004 (i)
        DSAPrims 23 September 2004 (i)
        UUIDPlugin 23 September 2004 (i)
        LargeIntegers v1.3 23 September 2004 (i)
        Matrix2x3Plugin 23 September 2004 (i)
        FloatArrayPlugin 23 September 2004 (i)
        B2DPlugin 23 September 2004 (i)
        BitBltPlugin 23 September 2004 (i)
        SecurityPlugin 23 September 2004 (i)
        FilePlugin 23 September 2004 (i)
        MiscPrimitivePlugin 23 September 2004 (i)


Stack dump:

619600144 Win32FFICertificateStore>freeCertContext
619599740 Win32FFICertificateStore>?
.

Thank you for any help!

Ron Teitelbaum


Reply | Threaded
Open this post in threaded view
|

Re: Help with FFI structure pointer

Nicolas Cellier-3
I do not understand how this code works.

If external code is allocating the structure, shouldn't it return a pointer to
the structure rather than a structure by value, what i understand as a copy?

Wouldn't it be

<apicall: MyStruct * 'MyAPICall' (MyStruct *) module: 'my.dll'>

In which case, you can pass nil to the pointer and have a single MyApiCall
declared in Smalltalk.

If MyStruct is just an opaque handle you do not access but thru external code,
maybe you can write:

<apicall: long 'MyAPICall' (long) module: 'my.dll'>
<apicall: void 'FreeContext' (long) module: 'my.dll'>

Nicolas

Le Mardi 01 Août 2006 21:30, Ron Teitelbaum a écrit :

> All,
>
> I'm sure someone saw this coming.  I could use some help.
>
> Ok I built a structure but the use of the structure is kinda odd.
>
> What I need to do is pass the structure in as null for the first call. Then
> I need to pass the real structure in for subsequent calls.
> Finally I need to free the structure through the API using the structure
> pointer.
>
> I handled the first two problems by implementing
>
> <apicall: MyStruct 'MyAPICall' (long) module: 'my.dll'>  and sending in
> nil. And
> <apicall: MyStruct 'MyAPICall' (MyStruct) module: 'my.dll'>  <- this is so
> cool!  I assume that Andreas wrote this code, it is beautiful!
>
> Ok now here it the problem:
>
> I have to call
>
> <apicall: void 'FreeContext' (MyStruct *) module: 'my.dll'>  or something.
>
> The pointer needs to be sent to the apicall.  Did I do this correctly
> (using MyStruct *) and it just doesn't work or is there another way to get
> the handle of the structure.  The structure itself has a handle ivar but
> it's a byteArray.
>
> The result using MyStruct * is:
> ---------------------------------------------------------------------
> Tue Aug 01 14:28:54 2006
>
> Exception code: C0000005
> ============
> which appears to be a buffer overrun:
> // MessageId: STATUS_ACCESS_VIOLATION
> //
> // MessageText:
> //
> //  The instruction at "0x%08lx" referenced memory at "0x%08lx". The memory
> could not be "%s".
> //
> #define STATUS_ACCESS_VIOLATION          ((NTSTATUS)0xC0000005L)    //
> winnt ============
> Exception addr: 77A8CE7E
> Access violation (read access) at 0000000D
> EAX:000C04E0 EBX:7C809794 ECX:00000000 EDX:00000001
> ESI:00000001 EDI:000C04E0 EBP:0006FB80 ESP:0006FB74
> EIP:77A8CE7E EFL:00210202
> FP Control: FFFF037F
> FP Status:  FFFF4000
> FP Tag:     FFFFFFFF
> VM Version: Squeak 3.7.1 (release) from Sep 23 2004
> Compiler: gcc 2.95.2 19991024 (release)
>
> Current byte code: 225
> Primitive index: 120
>
> Loaded plugins:
>  SoundGenerationPlugin 23 September 2004 (i)
>  SoundPlugin 23 September 2004 (i)
>  crypt32.dll
>  SqueakFFIPrims 23 September 2004 (e)
>  ZipPlugin 23 September 2004 (i)
>  SocketPlugin 23 September 2004 (i)
>  DSAPrims 23 September 2004 (i)
>  UUIDPlugin 23 September 2004 (i)
>  LargeIntegers v1.3 23 September 2004 (i)
>  Matrix2x3Plugin 23 September 2004 (i)
>  FloatArrayPlugin 23 September 2004 (i)
>  B2DPlugin 23 September 2004 (i)
>  BitBltPlugin 23 September 2004 (i)
>  SecurityPlugin 23 September 2004 (i)
>  FilePlugin 23 September 2004 (i)
>  MiscPrimitivePlugin 23 September 2004 (i)
>
>
> Stack dump:
>
> 619600144 Win32FFICertificateStore>freeCertContext
> 619599740 Win32FFICertificateStore>?
> .
>
> Thank you for any help!
>
> Ron Teitelbaum


Reply | Threaded
Open this post in threaded view
|

RE: Help with FFI structure pointer

Ron Teitelbaum
Thanks Nicolas,

That was a very good suggestion.  I should have realized that the pointer
could be used on the structure.  I saw the comment about accessing external
structures through the primitive if the handle is external address.  

I tried it and got some odd results.  The handle came back as zero.  Trying
to access the data at zero was ineffective as expected, but freeing the
context using the zero pointer didn't blow up!

I feel like I'm walking through a strange room blindfolded.  

I do get data results if I use the structure as the return instead of the
pointer.  Looking at the data it appears that I've done something wrong in
fields, so I'm going to double check that.

Also when I tried using MyStruct * as the parameter sending nil came back
with can not coerce arguments.  

Thanks for the suggestion it was a good tip!  

Ron

> -----Original Message-----
> From: [hidden email] [mailto:squeak-dev-
> [hidden email]] On Behalf Of nicolas cellier
> Sent: Tuesday, August 01, 2006 4:05 PM
> To: [hidden email]
> Subject: Re: Help with FFI structure pointer
>
> I do not understand how this code works.
>
> If external code is allocating the structure, shouldn't it return a
> pointer to
> the structure rather than a structure by value, what i understand as a
> copy?
>
> Wouldn't it be
>
> <apicall: MyStruct * 'MyAPICall' (MyStruct *) module: 'my.dll'>
>
> In which case, you can pass nil to the pointer and have a single MyApiCall
> declared in Smalltalk.
>
> If MyStruct is just an opaque handle you do not access but thru external
> code,
> maybe you can write:
>
> <apicall: long 'MyAPICall' (long) module: 'my.dll'>
> <apicall: void 'FreeContext' (long) module: 'my.dll'>
>
> Nicolas
>
> Le Mardi 01 Août 2006 21:30, Ron Teitelbaum a écrit :
> > All,
> >
> > I'm sure someone saw this coming.  I could use some help.
> >
> > Ok I built a structure but the use of the structure is kinda odd.
> >
> > What I need to do is pass the structure in as null for the first call.
> Then
> > I need to pass the real structure in for subsequent calls.
> > Finally I need to free the structure through the API using the structure
> > pointer.
> >
> > I handled the first two problems by implementing
> >
> > <apicall: MyStruct 'MyAPICall' (long) module: 'my.dll'>  and sending in
> > nil. And
> > <apicall: MyStruct 'MyAPICall' (MyStruct) module: 'my.dll'>  <- this is
> so
> > cool!  I assume that Andreas wrote this code, it is beautiful!
> >
> > Ok now here it the problem:
> >
> > I have to call
> >
> > <apicall: void 'FreeContext' (MyStruct *) module: 'my.dll'>  or
> something.
> >
> > The pointer needs to be sent to the apicall.  Did I do this correctly
> > (using MyStruct *) and it just doesn't work or is there another way to
> get
> > the handle of the structure.  The structure itself has a handle ivar but
> > it's a byteArray.
> >
> > The result using MyStruct * is:
> > ---------------------------------------------------------------------
> > Tue Aug 01 14:28:54 2006
> >
> > Exception code: C0000005
> > ============
> > which appears to be a buffer overrun:
> > // MessageId: STATUS_ACCESS_VIOLATION
> > //
> > // MessageText:
> > //
> > //  The instruction at "0x%08lx" referenced memory at "0x%08lx". The
> memory
> > could not be "%s".
> > //
> > #define STATUS_ACCESS_VIOLATION          ((NTSTATUS)0xC0000005L)    //
> > winnt ============
> > Exception addr: 77A8CE7E
> > Access violation (read access) at 0000000D
> > EAX:000C04E0 EBX:7C809794 ECX:00000000 EDX:00000001
> > ESI:00000001 EDI:000C04E0 EBP:0006FB80 ESP:0006FB74
> > EIP:77A8CE7E EFL:00210202
> > FP Control: FFFF037F
> > FP Status:  FFFF4000
> > FP Tag:     FFFFFFFF
> > VM Version: Squeak 3.7.1 (release) from Sep 23 2004
> > Compiler: gcc 2.95.2 19991024 (release)
> >
> > Current byte code: 225
> > Primitive index: 120
> >
> > Loaded plugins:
> >  SoundGenerationPlugin 23 September 2004 (i)
> >  SoundPlugin 23 September 2004 (i)
> >  crypt32.dll
> >  SqueakFFIPrims 23 September 2004 (e)
> >  ZipPlugin 23 September 2004 (i)
> >  SocketPlugin 23 September 2004 (i)
> >  DSAPrims 23 September 2004 (i)
> >  UUIDPlugin 23 September 2004 (i)
> >  LargeIntegers v1.3 23 September 2004 (i)
> >  Matrix2x3Plugin 23 September 2004 (i)
> >  FloatArrayPlugin 23 September 2004 (i)
> >  B2DPlugin 23 September 2004 (i)
> >  BitBltPlugin 23 September 2004 (i)
> >  SecurityPlugin 23 September 2004 (i)
> >  FilePlugin 23 September 2004 (i)
> >  MiscPrimitivePlugin 23 September 2004 (i)
> >
> >
> > Stack dump:
> >
> > 619600144 Win32FFICertificateStore>freeCertContext
> > 619599740 Win32FFICertificateStore>?
> > .
> >
> > Thank you for any help!
> >
> > Ron Teitelbaum
>
>



Reply | Threaded
Open this post in threaded view
|

Re: Help with FFI structure pointer

Andreas.Raab
In reply to this post by Ron Teitelbaum
Ron Teitelbaum wrote:
> <apicall: MyStruct 'MyAPICall' (long) module: 'my.dll'>  and sending in nil.
> And
> <apicall: MyStruct 'MyAPICall' (MyStruct) module: 'my.dll'>  <- this is so
> cool!  I assume that Andreas wrote this code, it is beautiful!

I did write the code but credits where credits are due: The design has
been *heavily* influenced by John Sarkela at the time who happened to
work with the customer and had done a similar design before (I think it
was for V but I don't quite remember).

Anyway, your two calls above should probably just one with:

myApiCall: myStruct
   <apicall: MyStruct* 'MyAPICall' (MyStruct*) module: 'my.dll'>

since you can pass nil in places where pointers are expected (nil is
coerced to NULL in such situations), like here:

   "call myApiCall with nil and record the result"
   myStruct := self myApiCall: nil.

   "call myApiCall with myStruct (ignoring the result)"
   self myApiCall: myStruct.


> Ok now here it the problem:
>
> I have to call
>
> <apicall: void 'FreeContext' (MyStruct *) module: 'my.dll'>  or something.  
>
> The pointer needs to be sent to the apicall.  Did I do this correctly (using
> MyStruct *) and it just doesn't work or is there another way to get the
> handle of the structure.  The structure itself has a handle ivar but it's a
> byteArray.

This is likely because the missing pointer declaration in the first call
(e.g., using "MyStruct" instead of "MyStruct*").

Cheers,
   - Andreas

Reply | Threaded
Open this post in threaded view
|

Master Sarkela (was: Help with FFI structure pointer)

Ron Teitelbaum

> From: Andreas Raab
> Sent: Wednesday, August 02, 2006 8:26 AM
>
> Ron Teitelbaum wrote:
> > <apicall: MyStruct 'MyAPICall' (long) module: 'my.dll'>  and sending in
> nil.
> > And
> > <apicall: MyStruct 'MyAPICall' (MyStruct) module: 'my.dll'>  <- this is
> so
> > cool!  I assume that Andreas wrote this code, it is beautiful!
>
> I did write the code but credits where credits are due: The design has
> been *heavily* influenced by John Sarkela at the time who happened to
> work with the customer and had done a similar design before (I think it
> was for V but I don't quite remember).
>

I interviewed John a number of years back and then fought like you wouldn't
believe to get him hired on.  He didn't want to move and the company was
foolish enough to think that mattered.  He agreed to 1 week a month which I
thought was great.  

I was very impressed with his comments about code mining, refactoring and
packaging.  His concept was a code miner or a team of miners should be
responsible for reviewing all the code that goes into a system, that there
are a number of hidden jewels that should be mined, cleaned up, reinserted
if the same problem has been solved multiple times, and then communicated to
the development team.  He believed that reliable complex programs could be
built by better organization and communication during the development
lifecycle.  He believe that the resulting stability, the expert review of
core components, and the increased productivity from reuse as the system
developed would pay for the code miners.  Having built some very large
programs myself, and seeing the code that has gone in by others, and
spending nights fixing problems on live systems I know he is right.  The
concept would have made a great book and would have been more productive
then pair programming!  (Don't get me started)

I'm really sorry I didn't get the chance to work with him.  Where is he now?

Ron Teitelbaum
President / Principal Software Engineer
US Medical Record Specialists
[hidden email]



Reply | Threaded
Open this post in threaded view
|

Re: Master Sarkela (was: Help with FFI structure pointer)

Nicolas Cellier-3

i agree that this must increase reuse and productivity.
We all would like to do such code mining job: the pressure must be lower
because productivity is indirect. Pleasure to program without constraints...
If John really convinced someone, don't expect him to change job soon.

Nicolas


Le Mercredi 02 Août 2006 23:19, Ron Teitelbaum a écrit :

> > From: Andreas Raab
> > Sent: Wednesday, August 02, 2006 8:26 AM
> >
> > Ron Teitelbaum wrote:
> > > <apicall: MyStruct 'MyAPICall' (long) module: 'my.dll'>  and sending in
> >
> > nil.
> >
> > > And
> > > <apicall: MyStruct 'MyAPICall' (MyStruct) module: 'my.dll'>  <- this is
> >
> > so
> >
> > > cool!  I assume that Andreas wrote this code, it is beautiful!
> >
> > I did write the code but credits where credits are due: The design has
> > been *heavily* influenced by John Sarkela at the time who happened to
> > work with the customer and had done a similar design before (I think it
> > was for V but I don't quite remember).
>
> I interviewed John a number of years back and then fought like you wouldn't
> believe to get him hired on.  He didn't want to move and the company was
> foolish enough to think that mattered.  He agreed to 1 week a month which I
> thought was great.
>
> I was very impressed with his comments about code mining, refactoring and
> packaging.  His concept was a code miner or a team of miners should be
> responsible for reviewing all the code that goes into a system, that there
> are a number of hidden jewels that should be mined, cleaned up, reinserted
> if the same problem has been solved multiple times, and then communicated
> to the development team.  He believed that reliable complex programs could
> be built by better organization and communication during the development
> lifecycle.  He believe that the resulting stability, the expert review of
> core components, and the increased productivity from reuse as the system
> developed would pay for the code miners.  Having built some very large
> programs myself, and seeing the code that has gone in by others, and
> spending nights fixing problems on live systems I know he is right.  The
> concept would have made a great book and would have been more productive
> then pair programming!  (Don't get me started)
>
> I'm really sorry I didn't get the chance to work with him.  Where is he
> now?
>
> Ron Teitelbaum
> President / Principal Software Engineer
> US Medical Record Specialists
> [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Master Sarkela (was: Help with FFI structure pointer)

stéphane ducasse-2
In reply to this post by Ron Teitelbaum
Hi ron

john is working now (since a year or two) at cincom on the new  
virtual machine for mac os x.
he did not reply to my emails so I do not know more
[hidden email] may be try this one or contact eliot

Stef

On 2 août 06, at 23:19, Ron Teitelbaum wrote:

>
>> From: Andreas Raab
>> Sent: Wednesday, August 02, 2006 8:26 AM
>>
>> Ron Teitelbaum wrote:
>>> <apicall: MyStruct 'MyAPICall' (long) module: 'my.dll'>  and  
>>> sending in
>> nil.
>>> And
>>> <apicall: MyStruct 'MyAPICall' (MyStruct) module: 'my.dll'>  <-  
>>> this is
>> so
>>> cool!  I assume that Andreas wrote this code, it is beautiful!
>>
>> I did write the code but credits where credits are due: The design  
>> has
>> been *heavily* influenced by John Sarkela at the time who happened to
>> work with the customer and had done a similar design before (I  
>> think it
>> was for V but I don't quite remember).
>>
>
> I interviewed John a number of years back and then fought like you  
> wouldn't
> believe to get him hired on.  He didn't want to move and the  
> company was
> foolish enough to think that mattered.  He agreed to 1 week a month  
> which I
> thought was great.
>
> I was very impressed with his comments about code mining,  
> refactoring and
> packaging.  His concept was a code miner or a team of miners should be
> responsible for reviewing all the code that goes into a system,  
> that there
> are a number of hidden jewels that should be mined, cleaned up,  
> reinserted
> if the same problem has been solved multiple times, and then  
> communicated to
> the development team.  He believed that reliable complex programs  
> could be
> built by better organization and communication during the development
> lifecycle.  He believe that the resulting stability, the expert  
> review of
> core components, and the increased productivity from reuse as the  
> system
> developed would pay for the code miners.  Having built some very large
> programs myself, and seeing the code that has gone in by others, and
> spending nights fixing problems on live systems I know he is  
> right.  The
> concept would have made a great book and would have been more  
> productive
> then pair programming!  (Don't get me started)
>
> I'm really sorry I didn't get the chance to work with him.  Where  
> is he now?
>
> Ron Teitelbaum
> President / Principal Software Engineer
> US Medical Record Specialists
> [hidden email]
>
>
>