caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Q] Callbacking a ocaml function from VC++
@ 2000-12-06  0:56 Hyun-Goo Kang
  2000-12-11  9:47 ` Xavier Leroy
  0 siblings, 1 reply; 2+ messages in thread
From: Hyun-Goo Kang @ 2000-12-06  0:56 UTC (permalink / raw)
  To: caml-list

---------------------------------------------------------------------------
[Q] Callbacking a ocaml function which uses Unix socket lib from VC++
function"
---------------------------------------------------------------------------


Hello.
I'm writing a network program written in ocaml3.0 for win32(for networking)
and VC++6.0(for user interface).

My problem is using a ocaml function which uses ocaml unix library for
socket programming
in a VC++ program.

First i defined a function which use "Unix.connect" like following...

  let connect_server () =
    let ip = xxx.xxx.xxx.xxx in
    let port = 6765 in
    let sockfd = socket PF_INET SOCK_STREAM 0 in
    let target_addr = ADDR_INET(inet_addr_of_string ip,port) in
    let  _ = connect sockfd target_addr  (* try to make connection *) in
    (* sockfd *) 1;;

 (* this function was already tested in ocaml and operated properly *)


and then to use callback mechanism, i registered that function like
following...

  let _ = Callback.register "connect" connect_server

and then i compiled it like following...

  c:\project> ocamlc -output-obj -o connect.obj unix.cma threads.cma
connect.cmo

and then i appended the object(connect.obj) in VC project,
and libcamlrun.lib libunix.lib
and wsock32.lib(if i don't append this, there are many link error),  --->
(*)

and then i included whole header files needed for callback like following

  //stdafx.h
  extern "C"
  {
  #include "./ocaml_lib/mlvalues.h"
  #include "./ocaml_lib/callback.h"
  }

and then i called ocaml connect function in VC++ program like following...

int connect_it()
{
 static value *connect = NULL;
 if (connect == NULL)
  connect = caml_named_value("connect");
 return Int_val(callback(*connect,Val_unit));
}

void CDlg2Dlg::OnConnect()
{
  int v;
  char** argv = (char**)malloc(sizeof(char*));;
 char buff[100];

 *argv = NULL;  //"dummy";

  caml_startup(argv);

 sprintf(buff,"result = %d",connect_it());
 AfxMessageBox(TEXT(buff));
}



But i couldn't get proper result. (VC++ program termination in callback
point)
What is the problem?
I think it's likely that the problem is in (*) point.

Thanks in advance...


------------
Note: I succeeded in other easy example using same style,
      if i don't use Unix or Unix socket library.







^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Q] Callbacking a ocaml function from VC++
  2000-12-06  0:56 [Q] Callbacking a ocaml function from VC++ Hyun-Goo Kang
@ 2000-12-11  9:47 ` Xavier Leroy
  0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 2000-12-11  9:47 UTC (permalink / raw)
  To: Hyun-Goo Kang; +Cc: caml-list

> [Q] Callbacking a ocaml function which uses Unix socket lib from VC++
> function"

I had a look at your example.  The problem is that the Caml code
raises an exception, and the callback() function tries to propagate
the exception upwards to Caml code that is calling the current C code,
and since there is none it stops the program.  Better use
callback_exn() and check for exceptions yourself:

int connect_it()
{
  static value *connect = NULL;
  value res;
  if (connect == NULL)
   connect = caml_named_value("connect");
  res = callback(*connect,Val_unit);
  if (Is_exception_result(res)) {
    // do something to report an error
  } else {
    return Int_val(res);
  }
}

Now, why is it that the Caml code raises an exception?  In my test
run, it's Unix.socket that raises an exception, because apparently the
wsock32 subsystem hasn't been initialized.  Adding the following lines
in the main function cured the problem:

  WSADATA wsaData;
  (void) WSAStartup(MAKEWORD(2, 0), &wsaData);

Still, this is surprising because normally the initialization of
wsock32 is performed by the Caml Unix library when it starts up.  I
haven't been able yet to track the problem down.

- Xavier Leroy



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2000-12-11 17:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-12-06  0:56 [Q] Callbacking a ocaml function from VC++ Hyun-Goo Kang
2000-12-11  9:47 ` Xavier Leroy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).