[Mono-dev] Mono embedded

Stefan Fink sf at granit.de
Wed Mar 26 11:14:43 EDT 2008


hallo,
i have make a small embedded example but i have a few problems. i want 
to call a embedded c-function like ...list_box(...)
(show below cad.h cad.c). when i compile my test.cs the ListBox function 
is unknown. who can i to make known about this function???
And ist the rest of my code right or totally wrong. i have attached the 
hole code as tgz.

Thanks for any answer !!!!

Stefan




main.c
-----------------------------------------------------------------------------------
#include <stdio.h>
#include <mono/jit/jit.h>
//#include <mono/metadata/object.h>
#include <mono/metadata/loader.h>
#include <mono/metadata/class.h>
//#include <mono/metadata/class-internals.h>
#include <mono/io-layer/io-layer.h>
#include <stdlib.h>

#include "makro_cad.h"

static void main_function (MonoDomain *domain, const char *file, int 
argc, char** argv)
{
MonoAssembly *assembly;

  assembly = mono_domain_assembly_open (domain, file);

  if (!assembly) {
    printf("No assembly\n");
    exit (2);
  }

  mono_jit_exec (domain, assembly, argc, argv);

}


int main(int argc, char* argv[]) {

MonoDomain *domain;
const char *file;
int retval;

  argv[1] = "test.exe";
  argc = 2;

  if (argc < 2){
    fprintf (stderr, "Please provide an assembly to load\n");
    return 1;
  }

  file = argv[1];

  mono_config_parse (NULL);

  domain = mono_jit_init (file);

  mono_add_internal_call ("MonoEmbed::ListBox", ListBox);

  main_function (domain, file, argc - 1, argv + 1);

  retval = mono_environment_exitcode_get ();

  mono_jit_cleanup (domain);

  return retval;

}


mono_cad.h
----------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mono/jit/jit.h>


MonoString* ListBox(MonoString* str, MonoArray* arr);




mono_cad.c
-------------------------------------------------------------------------------------

#include "mono_cad.h"
#include "cad.h"

MonoString* ListBox(MonoString* str, MonoArray* arr)
{
char* char_str;
char** char_lst;
int anz,ii;
char* ret;
MonoString* ret_str;
MonoString* lst_str;

   char_str = strcpy(char_str,mono_string_to_utf8(str));

   anz = arr->max_length;

   char_lst = malloc(sizeof(char*)*anz);
   for(ii=0;ii<anz;ii++) {
     lst_str = mono_array_get(arr, MonoString*, ii);
     char_lst[ii] = malloc(anz+1);
     char_lst[ii] = strcpy(char_lst[ii],mono_string_to_utf8(str));
   }

   ret = list_box(char_str,anz,char_lst);

   ret_str = mono_string_new_wrapper(ret);

   free(char_str);
   for(ii=0;ii<anz;ii++) {
     free(char_lst[ii]);
   }

   return(ret_str);

}



cad.h
-------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mono/jit/jit.h>


char* list_box(char* str, int anz, char** lst);



cad.c
---------------------------------------------------------------------------------------

#include "cad.h"

char* list_box(char* str, int anz, char** lst)
{
int ii;
  for(ii=0;ii<anz;ii++) {
    printf("Element %d : %s\n",ii,lst[ii]);
  }
  return(lst[1]);
}


Makefile
------------------------------------------------------------------------------------------
CFLAGS=`pkg-config --cflags mono`
LDFLAGS=`pkg-config --libs mono`

main: main.o mono_cad.o cad.o
        gcc -g -o main main.o mono_cad.o cad.o $(LDFLAGS)

main.o: main.c
        gcc -g $(CFLAGS) -c main.c

mono_cad.o: mono_cad.c
        gcc -g $(CFLAGS) -c mono_cad.c

cad.o: cad.c
        gcc -g $(CFLAGS) -c cad.c

test.exe: test.cs
        gmcs test.cs

clean:
        rm -f *.o main




test.cs
---------------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;

class MonoEmbed {

  static void Main() {
     string[] arr = {"1","2","3"};
     string ret = ListBox("Hallo",arr);
  }
}


compile test.cs
---------------------------------------------------------------------------------
 >gmcs test.cs
test.cs(8,19): error CS0103: The name `ListBox' does not exist in the 
current context
Compilation failed: 1 error(s), 0 warnings












-------------- next part --------------
A non-text attachment was scrubbed...
Name: embedded2.tgz
Type: application/x-compressed-tar
Size: 1340 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20080326/bd09bc95/attachment.bin 


More information about the Mono-devel-list mailing list