More FFI Help with long buffers

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

More FFI Help with long buffers

Ron Teitelbaum
Hello all,

I've been having some trouble with an api call.  I need to send in a long
value as a buffer.  

I've tried a few things.
1: just using an external address allocated to 4 bytes using an integer
address.

MyExtFunc >> withBuffer: aBuffer

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

------------------

MyExtFunc >> callWithBuffer

aBuffer := ExternalAddress allocate: 4.

self withBuffer: aBuffer getHandle asInteger.

-------------------

2: Using a pointer to ExternalAddress
MyExtFunc >> withBuffer: aBuffer

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

--------------------

MyExtFunc >> callWithBuffer

aBuffer := ExternalAddress allocate: 4.

self withBuffer: aBuffer

-------------------
3: Creating external structure that holds a long

MyExtFunc >> withBuffer: aBuffer

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

-------------------

MyExtFunc >> callWithBuffer

aBuffer := Win32LongBuffer new buffer: 0; yourself
aBuffer setHandle: (ExternalAddress allocate: 4).

self withBuffer: aBuffer


-------------------
4: I tried the Win32LongBuffer as (long) with aBuffer getHandle asInteger
-------------------
5: I tried ExternalData using Cees method:

argBuf _ ByteArray new: 4.
argBuf longAt: 1 put: 0 bigEndian: false.
argPtr _ ExternalData fromHandle: argBuf type: ExternalType long
asPointerType.

-------------------
6: extData := ExternalData fromHandle: (ptr := ExternalAddress allocate: 4)
type: ExternalType long [asPointerType]

Sending in the handle of the extData or the ptr and I tried both as integer.


So far nothing is working.  I believe that the problem is the buffer.  Does
anyone have any suggestions of other things I could try?

The Error I'm receiving is consistent:
//
// MessageId: ERROR_ALREADY_EXISTS
//
// MessageText:
//
//  Cannot create a file when that file already exists.
//
#define ERROR_ALREADY_EXISTS             183L

I'm not sure what that means.  Googleing it didn't help much.

Does anyone have some suggestions on how to send in a long buffer for the
api to fill?

Any help would be greatly appreciated!

Thanks,

Ron Teitelbaum


Reply | Threaded
Open this post in threaded view
|

Re: More FFI Help with long buffers

Andreas.Raab
I might be able to help you if instead of describing your attempted
solutions you'd spend a paragraph or two on describing the problem. "I
need to send in a long value as a buffer" isn't even remotely enough
information to give any useful advice.

Cheers,
   - Andreas

Ron Teitelbaum wrote:

> Hello all,
>
> I've been having some trouble with an api call.  I need to send in a long
> value as a buffer.  
>
> I've tried a few things.
> 1: just using an external address allocated to 4 bytes using an integer
> address.
>
> MyExtFunc >> withBuffer: aBuffer
>
> <apicall: bool 'MyAPICall' (long) module: 'my.dll'>
>
> ------------------
>
> MyExtFunc >> callWithBuffer
>
> aBuffer := ExternalAddress allocate: 4.
>
> self withBuffer: aBuffer getHandle asInteger.
>
> -------------------
>
> 2: Using a pointer to ExternalAddress
> MyExtFunc >> withBuffer: aBuffer
>
> <apicall: bool 'MyAPICall' (ExternalAddress*) module: 'my.dll'>
>
> --------------------
>
> MyExtFunc >> callWithBuffer
>
> aBuffer := ExternalAddress allocate: 4.
>
> self withBuffer: aBuffer
>
> -------------------
> 3: Creating external structure that holds a long
>
> MyExtFunc >> withBuffer: aBuffer
>
> <apicall: bool 'MyAPICall' (Win32LongBuffer*) module: 'my.dll'>
>
> -------------------
>
> MyExtFunc >> callWithBuffer
>
> aBuffer := Win32LongBuffer new buffer: 0; yourself
> aBuffer setHandle: (ExternalAddress allocate: 4).
>
> self withBuffer: aBuffer
>
>
> -------------------
> 4: I tried the Win32LongBuffer as (long) with aBuffer getHandle asInteger
> -------------------
> 5: I tried ExternalData using Cees method:
>
> argBuf _ ByteArray new: 4.
> argBuf longAt: 1 put: 0 bigEndian: false.
> argPtr _ ExternalData fromHandle: argBuf type: ExternalType long
> asPointerType.
>
> -------------------
> 6: extData := ExternalData fromHandle: (ptr := ExternalAddress allocate: 4)
> type: ExternalType long [asPointerType]
>
> Sending in the handle of the extData or the ptr and I tried both as integer.
>
>
> So far nothing is working.  I believe that the problem is the buffer.  Does
> anyone have any suggestions of other things I could try?
>
> The Error I'm receiving is consistent:
> //
> // MessageId: ERROR_ALREADY_EXISTS
> //
> // MessageText:
> //
> //  Cannot create a file when that file already exists.
> //
> #define ERROR_ALREADY_EXISTS             183L
>
> I'm not sure what that means.  Googleing it didn't help much.
>
> Does anyone have some suggestions on how to send in a long buffer for the
> api to fill?
>
> Any help would be greatly appreciated!
>
> Thanks,
>
> Ron Teitelbaum
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: More FFI Help with long buffers

Andreas.Raab
In reply to this post by Ron Teitelbaum
Oh, one more thing:
> So far nothing is working.  I believe that the problem is the buffer.  Does
> anyone have any suggestions of other things I could try?
>
> The Error I'm receiving is consistent:
> //
> // MessageId: ERROR_ALREADY_EXISTS

Since the error isn't signaled by the FFI itself but rather an error
reported by the windows function GetLastError() in response to a
CreateFile() request I'm almost certain that your conclusion that "the
problem is the buffer" is wrong. If the buffer were improperly
initialized I would either expect Squeak to crash (as data is
read/written) or its contents to be more or less random. The chances
that the random contents just so matches an existing file that just so
happens to be opened in the call seems ... unlikely.

Cheers,
   - Andreas