Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Q] Callbacking a ocaml function from VC++
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: [Q] Callbacking a ocaml function from VC++
> [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