c-ffi question involving c-struct 
Author Message
 c-ffi question involving c-struct

I'm sure this has come up before but I can't remember what the
solution was - can anyone help?

I want to call an API function that takes a pointer to some structures
and a count of the number of structures. So I use make(...) to create
a number of my structures:

define C-struct <INPUT>
  slot type-value :: <DWORD>;
  pointer-type-name: <LPINPUT>;
end C-struct <INPUT>;

let key-input = make(<LPINPUT*>, element-count: 3);
key-input[0].type-value := 0; // error here

I get an error on the indicated line saying 'no next method'. Any
ideas how I achieve this sort of thing?

Chris.
--
http://www.*-*-*.com/



Fri, 28 Mar 2003 03:00:00 GMT  
 c-ffi question involving c-struct

Quote:

> I get an error on the indicated line saying 'no next method'. Any
> ideas how I achieve this sort of thing?

BTW, this is the workaround I'm using:

let key-input = make(<LPINPUT*>, element-count: 3);
let key-input1 = make(<LPINPUT*>,
     address: key-input.pointer-address);
let key-input2 = make(<LPINPUT*>,
     address: \%+(key-input1.pointer-address, size-of(<INPUT>)));
let key-input3 = make(<LPINPUT*>,
     address: \%+(key-input2.pointer-address, size-of(<INPUT>)));

I'm sure the method using key-input[0], etc is supposed to work
according to the documentation though.

Chris.
--
http://www.double.co.nz/dylan



Fri, 28 Mar 2003 03:00:00 GMT  
 c-ffi question involving c-struct
Hi Chris,

Quote:
> I want to call an API function that takes a pointer to some structures
> and a count of the number of structures. So I use make(...) to create
> a number of my structures:

> define C-struct <INPUT>
>   slot type-value :: <DWORD>;
>   pointer-type-name: <LPINPUT>;
> end C-struct <INPUT>;

> let key-input = make(<LPINPUT*>, element-count: 3);
> key-input[0].type-value := 0; // error here

> I get an error on the indicated line saying 'no next method'. Any
> ideas how I achieve this sort of thing?

I think you need to use pointer-value-address(). The array operations
map like this:

  (Dylan) tarray[i] == (Dylan) pointer-value(tarray, index: i) == (C) tarray[i]

Except this doesn't work in Dylan where t is a struct type. However,
there's also:

  (Dylan) pointer-value-address(tarray, index: i) == (C) &(tarray[i])

which allows you to do what you want:

  let key-input = make(<LPINPUT*>, element-count: 3);
  pointer-value-address(key-input, index: 0).type-value := 0;

The restriction on pointer-value() and structs is there because it was
thought that some people would expect it to copy the embedded struct
while some would expect to get a pointer to the existing struct. To be
on the safe side it was simply disabled, leaving clients have to make
that choice explicitly.

-- Keith



Fri, 28 Mar 2003 03:00:00 GMT  
 c-ffi question involving c-struct

Quote:

> Except this doesn't work in Dylan where t is a struct type. However,
> there's also:

>   (Dylan) pointer-value-address(tarray, index: i) == (C) &(tarray[i])

> which allows you to do what you want:

>   let key-input = make(<LPINPUT*>, element-count: 3);
>   pointer-value-address(key-input, index: 0).type-value := 0;

Yep, that did the trick. Thanks Keith.

Chris.
--
http://www.double.co.nz/dylan



Sat, 29 Mar 2003 04:07:13 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Using C structs with FD 2.0 FFI

2. FFI c-struct convenience classes? OO, not with-

3. Syntax Question involving parameters, function, ranges...

4. Two questions (Sorry, Goobers no cash involved :-)

5. Fortran syntax question (involving **)

6. Conformance question involving equivalence

7. Intersesting Questions involving extending and embedding

8. Nested structs and struct.pack

9. struct module + array module = ? ( reading C structs )

10. Fun-O Win32 FFI Question

11. C FFI question

12. FFI question #2: Getting the address of a C function

 

 
Powered by phpBB® Forum Software