[PHP] How to get a string from C library into PHP via SWIG?
I have a need to call a C language function from a PHP script. The function, which I wrote, looks like this: /* * foo(): Takes a string in sIn and writes another string into sOut according to what is passed in, and the * value at *pcch. Will not write more than *pcch bytes to output, including null terminator. * Upon return, *pcch contains number of chars written to sOut. */ void foo(const char * sIn, char * sOut, size_t * pcch); So basically I need to pass down a string, and get back a string. I have been using swig but clearly I ain't getting it (I am good in C but new to PHP). Lots of warnings, crashes, and so on. No happiness. The swig docs show how to pass strings between python, java, etc., and C, but nothing on C <---> PHP. Can anyone help me with a fragment of sample code, or a link? Thanks Eric -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to get a string from C library into PHP via SWIG?
Hm, that could work, but it does produce overhead. Thank you. Eric On Tue, Jan 5, 2010 at 1:29 PM, Ashley Sheridan wrote: > > On Tue, 2010-01-05 at 13:29 -0800, Eric Fowler wrote: > > I have a need to call a C language function from a PHP script. > > The function, which I wrote, looks like this: > > /* > * foo(): Takes a string in sIn and writes another string into sOut > according to what is passed in, and the > * value at *pcch. Will not write more than *pcch bytes to output, > including null terminator. > * Upon return, *pcch contains number of chars written to sOut. > */ > void foo(const char * sIn, char * sOut, size_t * pcch); > > So basically I need to pass down a string, and get back a string. > > I have been using swig but clearly I ain't getting it (I am good in C > but new to PHP). Lots of warnings, crashes, and so on. No happiness. > > The swig docs show how to pass strings between python, java, etc., and > C, but nothing on C <---> PHP. > > Can anyone help me with a fragment of sample code, or a link? > > Thanks > > Eric > > > Can't you just compile the C into an executable and call it from a shell? > There are several ways to retrieve input from shell programs. > > Thanks, > Ash > http://www.ashleysheridan.co.uk > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to get a string from C library into PHP via SWIG?
This is going to the list per Nathan's suggestion. I confess I am doing this the hard way because I want to get a handle on how to pass across that boundary. My current angle of attack is to use the malloc.i functions in swig to handle those strings. I will keep you all posted. I know you care. :-) Eric I have a software library that parses strings into a C language structure. It is a utility for C programmers. It contains a utility function that takes the output structure and formats it as a string for display. The goal is to demonstrate the functionality by permitting users to enter strings into a text box and press a button. The string will be passed into my code, parsed, formatted as a different kind of string, and returned, where it will be displayed on the web site. The code is being shipped as a Windows DLL but for the web site I am working with Linux dynamic libraries (*.so). I am able to call functions within the binary from PHP now, but pointers within PHP are not working far less crossing the PHP/C Line Of Death without dying. please keep the responses on list for the benefit of others. well, if you have an .so, honestly i might spend the effort to wrap it up in a simple extension. but if you want to roll something out quickly, i would probly just invoke it over the shell via shell_exec() or similar. i cant imagine what sort of noticeable impact you would see in performance going over the shell in this case. honestly, i think the question is how much work do you want to do. also, do you intend to share this functionality w/ other php programers, that might be an argument for an extension. do you already have a C based program that loads up the library from the cli and uses STDIN / STDOUT, if so i think going over the shell is a no-brainer.. On Tue, Jan 5, 2010 at 2:56 PM, Nathan Nobbe wrote: > On Tue, Jan 5, 2010 at 3:42 PM, Eric Fowler wrote: >> >> Hm, that could work, but it does produce overhead. > > you should consider your overall communication paradigm. im very loosely > familiar w/ swig, basically ive heard of it ... > anyways, you have a few options for communication, > . shell (call php from C or the other way around) > . php extension, this may not make sense depending on what your C is doing > and is by far the most complex > . network protocol, socket, http or other.. > can you tell us a little bit more about what youre trying to accomplish and > specifically how C and php are communicating in general in your application? > -nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to get a string from C library into PHP via SWIG?
A little more information: my crashes all relate to handling the char datatype. Floats and ints are happy. I suspect that a char type in PHP is not the same as a char type in C. But I am not sure at all. More recently I have used cmalloc.i to allocate arrays of chars and floats. The floats are happy, the chars crash upon allocation. Weird. Eric On Tue, Jan 5, 2010 at 2:56 PM, Nathan Nobbe wrote: > On Tue, Jan 5, 2010 at 3:42 PM, Eric Fowler wrote: >> >> Hm, that could work, but it does produce overhead. > > you should consider your overall communication paradigm. im very loosely > familiar w/ swig, basically ive heard of it ... > anyways, you have a few options for communication, > . shell (call php from C or the other way around) > . php extension, this may not make sense depending on what your C is doing > and is by far the most complex > . network protocol, socket, http or other.. > can you tell us a little bit more about what youre trying to accomplish and > specifically how C and php are communicating in general in your application? > -nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to get a string from C library into PHP via SWIG?
I will expose only one function, prototyped like the foo() example I described. cstring.i is not implemented for a PHP target (wah). I think I will go the shell approach. I have enough time in this already. Eric On Tue, Jan 5, 2010 at 6:26 PM, Nathan Nobbe wrote: > On Tue, Jan 5, 2010 at 4:38 PM, Eric Fowler wrote: >> >> A little more information: my crashes all relate to handling the char >> datatype. Floats and ints are happy. >> >> I suspect that a char type in PHP is not the same as a char type in C. >> But I am not sure at all. >> >> More recently I have used cmalloc.i to allocate arrays of chars and >> floats. The floats are happy, the chars crash upon allocation. > > neat, swig generates php extensions. i wonder Eric, how much functionality > you intend to expose to php, a series of functions, classes? from the > sounds of your earlier description i would imagine maybe one class or a > single set of functions to operate on a single string variable, with > additional parameters for metadata. > im curious, if youre sold on the generator, or if youre interested to just > try your hand writing an extension. if youre strong w/ C you could probly > crank it out quickly. another option would be to version the generated > (swig) C, and repair it by hand, putting those changes in a branch. during > subsequent generations, you could merge the work from said branch and > iterate on that. > of course the other option is to figure out swig, lol. anyways, is this C > of yours for a private project or is it something i could take a peak at; im > halfway curious what youre working w/. > o, and guess what, reading through the swig php page, i discovered you can > write php extensions 'in c++', by creating a thin C wrapper - wow, i had > never thought of that, lol. > -nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php