Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] CamlDL/Abstract pointers problem
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: ronniec95@l...
Subject: [Caml-list] CamlDL/Abstract pointers problem
Hi list,

I'm trying to create an FFI to a messaging interface and keep getting problems
with passing around opaque pointers from C back to Ocaml (crashes). This
is on WIN32 btw. Here's the simples example I could thing of that reproduces
the problem. Any help is appreciated!

--- test.idl ---
typedef [abstract,ptr] void* mywindow;

quote(C,"__declspec(dllexport)")
mywindow init_window();

--- end test.idl ---

--- testimpl.c --
typedef struct _Foo
{
	int x;
	char* y;
} Foo;

mywindow init_window(void)
{
	/* Initialise a structure */
	Foo* f = (Foo*) malloc(sizeof(Foo));
	f->x = 10;
	f->y = (char*)malloc(sizeof(char) * 256);
	strcpy(f->y,"hello world");
	printf("%d %s\n",f->x,f->y);
	fflush(stdout);
	return (mywindow)f; // Crashes after leaving this block
}

--- end testimpl.c ---


--- main.ml ---
let win () = Test.init_window ()
--- end main.ml ---

I'm compiling (test_stub.c (generated) and testimpl.c) with MSVC 6. with
flags : /MD /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "CAML_DLL"
/D "_USRDLL"

and building a cmxa (ocamlopt) file that I link with
ocamlopt -verbose -o go -I . -cclib "/link -LIBPATH:C:/Progra~1/Micros~2/vc98/lib"
test.cmxa main.cmx

which displays:
+ ml /nologo /coff /Cp /c /Fo"c:\TEMP\camlstartupca0b4c.obj" "c:\TEMP\camlstartupd503a3.asm">NUL
+ cl /nologo /MT /Fe"go" -I"e:\\Local\\ObjectiveCaml\\lib" "c:\TEMP\camlstartupca0b4c.obj"
"e:\\Local\\ObjectiveCaml\\lib\std_exit.obj" "main.obj" "test.lib" "e
:\\Local\\ObjectiveCaml\\lib\stdlib.lib" "libtest.lib" "/link" "-LIBPATH:C:/Progra~1/Micros~2/vc98/lib"
"e:\\Local\\ObjectiveCaml\\lib\libasmrun.lib" advapi32.lib


I'm thinking that either I've forgotten to initialise something or linked
with wrong options but I can't figure out what that would be. Any sample
program/makefile that works under win32 or hints would be appreciated. BTW.
Generating my own handcoded stubs appears to  work fine (though is extremely
dull).

All I want to do is just give Ocaml a pointer to something I've created
in C and pass it back to other C functions later; don't want it to do anything
with it (including moving it around the ocaml heap).\

Thanks for any help.

Ronnie

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners