Thanks!
Strange that I am able to get error message before just using char *,
thought
it meant passing string by pointer already.
I change the argument to char ** errMsg, but in Ruby, when I tried to
convert the pointer to string, I got error message:
Encoding::UndefinedConversionError in SamplesizeController#compute "\xC9"
from ASCII-8BIT to UTF-8
Extracted source (around line *#34*):
323334353637
if options.is_a?(::JSON::State)
# Called from JSON.{generate,dump}, forward it to JSON gem's to_json
self.to_json_without_active_support_encoder(options)
else
# to_json is being invoked directly, use ActiveSupport's encoder
ActiveSupport::JSON.encode(self, options)
Do I need to change anything in ruby file when use a pointer?
Thanks!
Liz
On Friday, November 20, 2015 at 2:57:33 PM UTC-5, Frederick Cheung wrote:
>
> On Friday, November 20, 2015 at 5:34:33 PM UTC, Liz Huang wrote:
> > Hi,
> >
> > I am trying to use Fiddle to call C function in dynamic library, I used
> to be able to
> > pass a return long variable and an error message, but now only return
> long variable
> > is returned, can't get the error message, I create a very simple example
> to test,
> > this is my add.c
> >
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <string.h>
> >
> > long add(long maxn, double delta, double conf, char *errMsg)
> > {
> > long answer;
> >
> > errMsg = (char *) malloc(6*sizeof(char));
> > answer = (long)(maxn + delta + conf);
> > errMsg = "Hello!";
>
>
> Arguments are passed by value in C, so if you assign to a variable in a
> function it does not change its value in the calling function. You need to
> change your C function so that either:
>
> - the argument is a pointer to some memory allocated by the caller and you
> copy into it by strncpy or similar
> - the argument is a pointer to a pointer size block of memory. Your
> function would then allocate a buffer, write the error message to that
> buffer and then write the value of the pointer to the argument, ie the last
> argument to the function is now char **message and your code does *message
> = malloc(...)
>
> Fred
>
>
> >
> > return answer;
> > }
> >
> > and add.h
> >
> > #ifndef add_h
> > #define add_h
> >
> > long add(long maxn, double delta, double conf, char *errMsg);
> >
> > #endif
> >
> > I created lib_add.so and part of my .rb file calling fiddle:
> >
> > class SamplesizeController < ApplicationController
> > require 'fiddle'
> >
> > .....
> > def compute
> >
> > str = "there!"
> > libm = Fiddle.dlopen('/var/www/myapp/smart/lib/lib_add.so')
> > add = Fiddle::Function.new(libm['add'],[Fiddle::TYPE_LONG,
> > Fiddle::TYPE_DOUBLE,
> > Fiddle::TYPE_DOUBLE,
> > Fiddle::TYPE_VOIDP],
> > Fiddle::TYPE_LONG)
> >
> > add.call(session[:nmax], session[:delta], session[:conf], str)
> > session[:errmsg] = str.to_s
> > redirect_to :action => "results"
> > end
> >
> > ....
> >
> > end
> >
> > session[:errmsg] can be modified? only return variable can be changed
> or something I did
> > was wrong? Any advice will be greatly appreciated.
> >
> > Thanks!
> > Liz
>
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/31752ab2-0bcd-4f3b-b72a-18d2a9022053%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.