1. one more C question ( mixedlib.e )
i think this may be the last C question i have before i will release my socket
wrappers for linux ( soon! almost done ). hopefully this is the case.
Ok bernie ( or anyone else for that matter ), lets say i have this pointer to a
struct created by mixedlib.e:
<Eu code>
pointer my_struct
my_struct = struc ( "yada yada specs here in correct form", HIGH )
-- pretending i put some vals in my_struct
<end Eu code>
If i wanted to pass a C struct to a function, would i pass the *pointer* to
this struct created by mixedlib.e even though the function wants the "value" not
the "reference"? like this:
<C code>
void happy_func( struct defined_struct some_struct );
<end C code>
would i just
<Eu code> # with earlier declarations #
c_proc( happy_func_, { my_struct } ) -- happy_func_ is a "define_c_proc"
-- assumed to have been declared earlier
<end Eu code>
There must be something im missing here. Perhaps its my knowledge of C that
lacks or my half-assed knowledge of mixedlib.e?
Thanks to anyone who can help me out here.
--
cense
a member of the
ak-software development team
http://ak-software.virtualave.net/
contract work for
Web Velocity IT inc.
http://www.webvelocity.ca/
2. Re: one more C question ( mixedlib.e )
- Posted by Bernie <xotron at PCOM.NET>
Aug 31, 2000
-
Last edited Sep 01, 2000
On Thu, 31 Aug 2000 19:55:54 -0600, cense <cense at MAIL.RU> wrote:
>i think this may be the last C question i have before i will release my
socket
>wrappers for linux ( soon! almost done ). hopefully this is the case.
>
>Ok bernie ( or anyone else for that matter ), lets say i have this pointer
to a
>struct created by mixedlib.e:
>
cense:
I need to see the actual "C" function you are using to really understand
what you are doing so I can help you.
I think you probably will be passing a POINTER to a structure
because there is no way to pass the value of a structure.
You could only pass the value of a single entry that is contained
in a structure.
Normally you will pass a POINTER to a "C" function in a predefined
structure format that the "C" function is expecting. The "C"
function code then USES that POINTER to move data in and out of
the predefined structure and returns a flag to tell you if exceeded.
Hope this helps.
Bernie
3. Re: one more C question ( mixedlib.e )
- Posted by Robert Craig <rds at ATTCANADA.NET>
Aug 31, 2000
-
Last edited Sep 01, 2000
cense writes:
> If i wanted to pass a C struct to a function, would
> i pass the *pointer* to this struct created by mixedlib.e
> even though the function wants the "value" not
> the "reference"? like this:
Euphoria supports passing *pointers* to structures,
not structures by value.
If you can create an intermediate C routine that
expects a pointer to a structure and then
passes the structure by value, that would work.
Alternatively, you can go way out on the bleeding edge,
and try passing the members of the structure as
individual arguments. i.e. tell Euphoria that the
C routine takes (say) 4 integer arguments
instead of one structure, and then pass
the structure members as 4 arguments: (a,b,c,d).
They will be pushed onto the call stack, hopefully in
the same way that C expects a structure to be pushed
when passed by value. Maybe you'll need the opposite order:
(d,c,b,a). You'd have to look at the assembly code produced
by a C compiler to learn if this is the right way to pass
a structure. I might look into this eventually, but I'm pretty busy
right now. Good Luck!
Regards,
Rob Craig
Rapid Deployment Software
http://www.RapidEuphoria.com
4. Re: one more C question ( mixedlib.e )
- Posted by cense <cense at mail.ru>
Aug 31, 2000
-
Last edited Sep 01, 2000
On Thu, 31 Aug 2000, Bernie wrote:
> cense:
> I need to see the actual "C" function you are using to really understand
> what you are doing so I can help you.
> I think you probably will be passing a POINTER to a structure
> because there is no way to pass the value of a structure.
> You could only pass the value of a single entry that is contained
> in a structure.
> Normally you will pass a POINTER to a "C" function in a predefined
> structure format that the "C" function is expecting. The "C"
> function code then USES that POINTER to move data in and out of
> the predefined structure and returns a flag to tell you if exceeded.
>
> Hope this helps.
>
> Bernie
Ok bernie, here is the C function prototype and the struct that this function
wants:
< fun C code >
/* this is the function ( you probaly know of it ) */
char *inet_ntoa( struct in_addr inaddr ); // does this really want a pointer?
/* here is the struct ( ya probably know about this one too ) */
struct in_addr {
u_long s_addr; // 32-bit netid/hostid in network byte order
};
<end the fun of it all >
so does char *inet_ntoa( struct in_addr inaddr ); really want a pointer to a
in_addr struct or the "value"? im so confused when it comes to memory related
things and pointers in C.
--
cense
a member of the
ak-software development team
http://ak-software.virtualave.net/
contract work for
Web Velocity IT inc.
http://www.webvelocity.ca/
5. Re: one more C question ( mixedlib.e )
- Posted by cense <cense at MAIL.RU>
Aug 31, 2000
-
Last edited Sep 01, 2000
On Thu, 31 Aug 2000, Robert Craig wrote:
> cense writes:
> > If i wanted to pass a C struct to a function, would
> > i pass the *pointer* to this struct created by mixedlib.e
> > even though the function wants the "value" not
> > the "reference"? like this:
>
> Euphoria supports passing *pointers* to structures,
> not structures by value.
>
> If you can create an intermediate C routine that
> expects a pointer to a structure and then
> passes the structure by value, that would work.
>
> Alternatively, you can go way out on the bleeding edge,
> and try passing the members of the structure as
> individual arguments. i.e. tell Euphoria that the
> C routine takes (say) 4 integer arguments
> instead of one structure, and then pass
> the structure members as 4 arguments: (a,b,c,d).
> They will be pushed onto the call stack, hopefully in
> the same way that C expects a structure to be pushed
> when passed by value. Maybe you'll need the opposite order:
> (d,c,b,a). You'd have to look at the assembly code produced
> by a C compiler to learn if this is the right way to pass
> a structure. I might look into this eventually, but I'm pretty busy
> right now. Good Luck!
>
> Regards,
> Rob Craig
> Rapid Deployment Software
> http://www.RapidEuphoria.com
sounds like alot of work! i dont do alot of memory/pointer work and thus im
confused on the subject every once in a while. I will see what alternatives are
possible before i takle such a seemingly long task. ( i have school to attend
now #one more year# too so that dont help me out time wise )
--
cense
a member of the
ak-software development team
http://ak-software.virtualave.net/
contract work for
Web Velocity IT inc.
http://www.webvelocity.ca/
6. Re: one more C question ( mixedlib.e )
>> Ok bernie, here is the C function prototype and the struct that this
function
>> wants:
>> < fun C code >
>> /* this is the function ( you probaly know of it ) */
>> char *inet_ntoa( struct in_addr inaddr ); // does this really want a
pointer?
>> /* here is the struct ( ya probably know about this one too ) */
>> struct in_addr {
>> u_long s_addr; // 32-bit netid/hostid in network byte order
>> };
>> <end the fun of it all >
>> so does char *inet_ntoa( struct in_addr inaddr ); really want a pointer
to a
>> in_addr struct or the "value"? im so confused when it comes to memory
related
>> things and pointers in C.
cense:
-- I have declared the variables just before they are used to make it
-- easier to understand the example you may need to move them.
--
------ USING MIXEDLIB.E -------
-- NOTE ALWAYS allocate in high memory when using LINUX ( no
allocate_low )
-- this defines structure for in_address
--
constant in_addr = struc(" s_addr : ulong : 1 ", HIGH)
-- char *inet_ntoa( struct in_addr inaddr );
-- This function converts the Internet host address addr NUMBER
-- to a STRING in the standard numbers-and-dots notation.
-- The RETURN VALUE is a POINTER to a buffer in memory that was created
-- by the "C" code.
-- The parameter to the function is structure of the type in_addr
-- called inaddr.
--
-- define the function like this with what ever name used for the dll.
--
pointer inet_ntoa
inet_ntoa = define_c_func( DLLNAME , "inet_ntoa", {C_POINTER}, C_POINTER)
-- declare a structure called inaddr of the type in_addr
-- struct in_addr inaddr;
--
pointer inaddr
inaddr = dups(in_addr, HIGH)
-- place your addres unsigned long integer the structure
--
Sets(inaddr, "s_addr", 12345678) -- some address
-- To call the function
--
pointer a_char_ptr
a_char_ptr = c_func(inet_ntoa, {inaddr})
-- The "C" function will return a POINTER to a character string
-- in MEMORY in the standard numbers-and-dots notation.
-- You will have to copy this string to another place to save it
-- because the next time this function is called it "C" will
-- over write it.
-- you can use -- strdup function to make a copy
-- or -- str2seq function to convert to sequence
Bernie
7. Re: one more C question ( mixedlib.e )
cense wrote:
> < fun C code >
>
> /* this is the function ( you probaly know of it ) */
> char *inet_ntoa( struct in_addr inaddr ); // does this really
> want a pointer?
>
> /* here is the struct ( ya probably know about this one too ) */
> struct in_addr {
> u_long s_addr; // 32-bit netid/hostid in network byte order
> };
>
I've worked with similar things in win32lib, like color values. Windows is
actually looking for a 32-bit color structure, where the first 3 low order
bytes are the RGB values. Technically, this is a structure, I suppose so
that it's easy for Windows to grab the components of the color.
Internet addresses are really just 32-bit values, but each octet is used for
subnetting, etc. When you've got a 32-bit structure, you should be able to
simply pass the 32-bit value. It's only when you've got larger structures
(in my experience) that you need to pass pointers.
Matt
8. Re: one more C question ( mixedlib.e )
On Fri, 01 Sep 2000, Matthew Lewis wrote:
> cense wrote:
>
> > < fun C code >
> >
> > /* this is the function ( you probaly know of it ) */
> > char *inet_ntoa( struct in_addr inaddr ); // does this really
> > want a pointer?
> >
> > /* here is the struct ( ya probably know about this one too ) */
> > struct in_addr {
> > u_long s_addr; // 32-bit netid/hostid in network byte order
> > };
> >
>
> I've worked with similar things in win32lib, like color values. Windows is
> actually looking for a 32-bit color structure, where the first 3 low order
> bytes are the RGB values. Technically, this is a structure, I suppose so
> that it's easy for Windows to grab the components of the color.
>
> Internet addresses are really just 32-bit values, but each octet is used for
> subnetting, etc. When you've got a 32-bit structure, you should be able to
> simply pass the 32-bit value. It's only when you've got larger structures
> (in my experience) that you need to pass pointers.
>
> Matt
ill try your suggestion before i go ahead with bernie's for the simple fact
that your solution seems to be alot less work and it achives the same thing
with less code. Thank Matt.
--
cense
a member of the
ak-software development team
http://ak-software.virtualave.net/
contract work for
Web Velocity IT inc.
http://www.webvelocity.ca/
9. Re: one more C question ( mixedlib.e )
On Fri, 01 Sep 2000, Bernie wrote:
> >> Ok bernie, here is the C function prototype and the struct that this
> function
> >> wants:
> >> < fun C code >
> >> /* this is the function ( you probaly know of it ) */
> >> char *inet_ntoa( struct in_addr inaddr ); // does this really want a
> pointer?
> >> /* here is the struct ( ya probably know about this one too ) */
> >> struct in_addr {
> >> u_long s_addr; // 32-bit netid/hostid in network byte order
> >> };
> >> <end the fun of it all >
> >> so does char *inet_ntoa( struct in_addr inaddr ); really want a pointer
> to a
> >> in_addr struct or the "value"? im so confused when it comes to memory
> related
> >> things and pointers in C.
>
>
>
> cense:
>
> -- I have declared the variables just before they are used to make it
> -- easier to understand the example you may need to move them.
> --
>
> ------ USING MIXEDLIB.E -------
>
> -- NOTE ALWAYS allocate in high memory when using LINUX ( no
> allocate_low )
>
> -- this defines structure for in_address
> --
> constant in_addr = struc(" s_addr : ulong : 1 ", HIGH)
>
> -- char *inet_ntoa( struct in_addr inaddr );
> -- This function converts the Internet host address addr NUMBER
> -- to a STRING in the standard numbers-and-dots notation.
> -- The RETURN VALUE is a POINTER to a buffer in memory that was created
> -- by the "C" code.
> -- The parameter to the function is structure of the type in_addr
> -- called inaddr.
> --
> -- define the function like this with what ever name used for the dll.
> --
> pointer inet_ntoa
> inet_ntoa = define_c_func( DLLNAME , "inet_ntoa", {C_POINTER}, C_POINTER)
>
> -- declare a structure called inaddr of the type in_addr
> -- struct in_addr inaddr;
> --
> pointer inaddr
> inaddr = dups(in_addr, HIGH)
>
> -- place your addres unsigned long integer the structure
> --
> Sets(inaddr, "s_addr", 12345678) -- some address
>
> -- To call the function
> --
> pointer a_char_ptr
> a_char_ptr = c_func(inet_ntoa, {inaddr})
>
> -- The "C" function will return a POINTER to a character string
> -- in MEMORY in the standard numbers-and-dots notation.
> -- You will have to copy this string to another place to save it
> -- because the next time this function is called it "C" will
> -- over write it.
> -- you can use -- strdup function to make a copy
> -- or -- str2seq function to convert to sequence
>
> Bernie
Thanks for all the help Bernie. Your solution will be my final attempt if Matts
idea does not work. ( who knows? until i try )
Thanks for all the previous help too. without mixedlib.e and your help i dont
think i could have ever gotten this far with my wrapper. Your name will
definately appear in the credits.
--
cense
a member of the
ak-software development team
http://ak-software.virtualave.net/
contract work for
Web Velocity IT inc.
http://www.webvelocity.ca/
10. Re: one more C question ( mixedlib.e )
On Fri, 1 Sep 2000 16:29:32 -0600, cense <cense at MAIL.RU> wrote:
>Thanks for all the help Bernie. Your solution will be my final attempt if
Matts
>idea does not work. ( who knows? until i try )
>
I could have given you the same easy way that Matt gave you
but you will not learn from using short-cuts when you have
to setup a complicated structures and functions. You also will
not gain the experience of using the mixed library as you
requested.
My example is to show you how to write your code, not how
to use the shortest, easiest way to get finished.
Remember everything you learn by programming is an investment
in your future so be sure to learn as much as you can before
you graduate from school.
Bernie
11. Re: one more C question ( mixedlib.e )
On Fri, 01 Sep 2000, Bernie wrote:
> On Fri, 1 Sep 2000 16:29:32 -0600, cense <cense at MAIL.RU> wrote:
>
> >Thanks for all the help Bernie. Your solution will be my final attempt if
> Matts
> >idea does not work. ( who knows? until i try )
> >
>
> I could have given you the same easy way that Matt gave you
> but you will not learn from using short-cuts when you have
> to setup a complicated structures and functions. You also will
> not gain the experience of using the mixed library as you
> requested.
> My example is to show you how to write your code, not how
> to use the shortest, easiest way to get finished.
> Remember everything you learn by programming is an investment
> in your future so be sure to learn as much as you can before
> you graduate from school.
> Bernie
Thanks for explaining and reminding me that there is more to programming
than just meeting dealines and being a fast developer. I needed that to *wake
me up* from the stupid pressure i put myself under to complete this software.
Now that i think about it, your solution is probably alot more safe than just
flinging a 32-bit value into a function. But then again i could be wrong. Im
not the king of memory management as you obviously can tell.
I appreciate what contributions you've given and all the help that you offered.
I think it will, in the end, allow me to produce more quality software and have
fun doin it!
--
cense
a member of the
ak-software development team
http://ak-software.virtualave.net/
contract work for
Web Velocity IT inc.
http://www.webvelocity.ca/