List Info

Thread: "ocaml_beginners"::[] non-intrusive integration between dll and ocaml




"ocaml_beginners"::[] non-intrusive integration between dll and ocaml
country flaguser name
United States
2007-03-28 14:04:16

Hi everyone!

I'm planning to use ocaml as a "script" language to fast prototyping a
graphics engine and gpgpu framework.
My main problem is to find a elegant and non-intrusive way to integrate DLLs
with ocaml.
I did read the ocaml reference manual and find out a way to do this by
dinamically linking libraries to ocaml code, which preprocess the library,
extract the symbols and made them accessible (external) within the ocaml
environment. I did run some very basic tests by creating a simple DLL and
used the ocamlmktop to generate a custom loop-environemnt. I was able to
properly send integer and strings (char*) parameters from ocaml to c. I was
also able to send strings (char*) but I was not able to return strings from
c to ocaml. For the float parameter, I was not able even to send it as a
parameter (I tried to change from float to double, but without success).

Just to clarify a little bit, here is the functions that I exported from the
DLL:
int testint(int i)
{
int t = i + 3;
return(t);
}

float testfloat(float f)
{
int t = f + 3.0f;
return(f+3.0f);
}

char* teststring(char* s)
{
printf("%sn";, s);
char* s2 = &s[2];
return(s2);
}

I called the ocamlmktop this way:
ocamlmktop -o test.exe -dllib "mydll.dll"

By executing the generated test.exe , I imported the functions as follow:
# external testint : int -> int = "testint";;
external testint : int -> int = "testint"
# external testfloat : float -> float = "testfloat";;
external testfloat : float -> float = "testfloat"
# external teststring : string -> string = "teststring";;;
external teststring : string -> string = "teststring";

And then simply called:
testint(10);;
- : 13 = int

testfloat(10.0);;
*** PROGRAM CRASHES ***

teststring("hello world";);;
hello world
*** PROGRAM CRASHES ***

I know that this can be very dangerous!
But my question here is: how can I solve such problems being the more
non-intrusive as possible? (I don't want to add code within the DLL or
develop any external library just to act as a stub)

Thank you,
Kind regards,
Marcos Slomp

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] non-intrusive integration between dll and ocaml
country flaguser name
United Kingdom
2007-03-31 03:58:03

On Wed, Mar 28, 2007 at 04:04:16PM -0300, Marcos Slomp wrote:
> Just to clarify a little bit, here is the functions that I exported from the
> DLL:
>; int testint(int i)
> {
> int t = i + 3;
> return(t);
> }
[...]
> By executing the generated test.exe , I imported the functions as follow:
> # external testint : int -> int = "testint";;
> external testint : int -> int = "testint"

This isn't really going to work. The fact that it happens to work for
testint is just lucky.

I'd definitely suggest you read chapter 18 of the manual closely, and
then download some existing OCaml bindings to see how they work.

http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html
http://caml.inria.fr/cgi-bin/hump.en.cgi?sort=3&browse=65

Rich.

--
Richard Jones
Red Hat

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )