WIN32API: callback functions?

Hi all,

I wonder how can I use a win dll function which accepts a function
pointer parameter. For example, in the WPdpack lib here is a
function:

typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *,
const u_char *);

int pcap_loop(pcap_t *, int, pcap_handler, u_char *);

And I would like to use like this:

p=Proc {|v1,v2,v3| fun(v1,v2,v3)}
pcap_loop(var1,var2,p,var4)

I have the following idea: In C, I create a ruby class, with which I
can store a Proc object, and this ruby class can give back a C
function’s address as a 4 byte-length string.

This C function of pcap_handler type can read out the stored Proc
object, and calls it with it parameters (after converting to ruby
variables).

And then use like this:

obj=PcapHandler.new {{|v1,v2,v3| fun(v1,v2,v3)}
pcap_loop(var1,var2,obj.funAddress,var4)

Is there a simpler method? Is this solution work? How can make a more
generic solution (with variable number of params, etc.)

Thanks:
Circum

p=Proc {|v1,v2,v3| fun(v1,v2,v3)}
pcap_loop(var1,var2,p,var4)

Well, you have an example in ruby-pcap. In the case of this library the
choice was made to write it like this

  Pcap::Capture.open_live(...).each do | ... |
     # ...
  end

#each, which seems to be like your pcap_loop, just call #yield

Look at the source of ruby-pcap this will give you some ideas.

Guy Decoux

Gee, I didn’t noticed that there is a ruby-pcap module. That’s great!
What a neat-o-featureful language this ruby is! :-))

···

On Wed, 5 Feb 2003, ts wrote:

p=Proc {|v1,v2,v3| fun(v1,v2,v3)}
pcap_loop(var1,var2,p,var4)

Well, you have an example in ruby-pcap. In the case of this library the
choice was made to write it like this

Pcap::Capture.open_live(…).each do | … |
# …
end

#each, which seems to be like your pcap_loop, just call #yield

Look at the source of ruby-pcap this will give you some ideas.