Re: mono_method_desc_search_in_image problem, and some question...

pierre <[email protected]> Wed, 21 Feb 2018 11:20:03 +0100
Newsgroups gmane.comp.gnome.mono.devel
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--===============0203739022465275300==
Content-Type: multipart/alternative;
 boundary="------------AC64A92F9E69F75AF5DF9B30"
Content-Language: en-US

This is a multi-part message in MIME format.
--------------AC64A92F9E69F75AF5DF9B30
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit

Hi,

Anyone about this??
if I run "mono_domain_unload(domain);"
and after do
assembly = mono_assembly_load_from_full(image, referenceName, &status, 
FALSE);
it is not working properly if referenceName is kept from one launch to 
the next...
I think the code bellow is showing it... or am I doing something wrong??
may be it is linked to the "[DllImport ("__Internal"..." which blocking 
the unloading of the assembly?

And a question: Is there a way to debug a running assembly (embeded)?
On python and lua, you can register a function that is called on every 
line executed... I totally understand it is different for mono, but what 
are my options? where can I find documentations about debugging from and 
embedding application?

Thanks,
Pierre

The source to test is (mono_test.c):
/******************************************************************************************************************/
#include <mono/jit/jit.h>

#include <mono/metadata/mono-config.h>
#include <mono/metadata/threads.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/mono-gc.h>
#include <mono/metadata/environment.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/mono-debug.h>
#include <mono/utils/mono-logger.h>



/**********************************************************************************/
/* */
/**********************************************************************************/
#define TRUE  1
#define FALSE 0
#define BYTE unsigned char
#define gMalloc malloc
#define gFree free

BYTE *readFile(FILE *f, size_t *size)
{
   BYTE *buffer;
   size_t i;

   fseek(f, 0, SEEK_END);
   *size = ftell(f);
   fseek(f, 0, SEEK_SET);

   buffer = (BYTE *) gMalloc(*size);
   if(buffer == NULL) return NULL;

   i = fread(buffer, 1, *size, f);
   if(i != *size)
   {
     gFree(buffer);
     return NULL;
   }
   return buffer;
}


/**********************************************************************************/
/* */
/**********************************************************************************/
static MonoDomain *rootDomain;
static MonoDomain *domain;
static MonoImage *image;
static MonoAssembly *assembly;




/**********************************************************************************/
/* */
/**********************************************************************************/
__declspec(dllexport) void mono_report(char* str)
{
   printf("%s\n", str);
}


/**********************************************************************************/
/* */
/**********************************************************************************/
static int monoScriptCompile(char *fileName, char *referenceName)
{

   int res;
   char *ret, *strErr, *str;
   char buffer[256];
   FILE *f;
   BYTE *data;
   size_t dataLen;
   MonoImageOpenStatus status;
   MonoAssemblyName *aName;


   if(domain != NULL)
   {
     res = mono_domain_set(rootDomain, FALSE);
     if(!res)
     {
       printf("Failed to set root domain\n");
       return FALSE;
     }

     mono_domain_unload(domain);
     mono_gc_collect(mono_gc_max_generation());
   }
   domain = NULL;
   image = NULL;
   assembly = NULL;


   domain = mono_domain_create_appdomain("ScriptSubDomain", NULL);
   res = mono_domain_set(domain, FALSE);
   if(!res)
   {
     printf("Failed to set domain\n");
     return FALSE;
   }

   sprintf(buffer, "%s.dll", fileName);
   f = fopen(buffer, "rb");
   if(f == NULL)
   {
     printf("Cannot open file: %s\n", buffer);
     return FALSE;
   }
   data = readFile(f, &dataLen);
   fclose(f);




     // open the assembly from the data we read, so we never lock files
   image = mono_image_open_from_data_with_name(data, dataLen,
                                               TRUE /* copy data */,
&status,
                                               FALSE /* ref only */,
referenceName);
   gFree(data);
   if (status != MONO_IMAGE_OK || image == NULL)
   {
     printf("Cannot create image from data\n");
     return FALSE;
   }

     // load the assembly
   assembly = mono_assembly_load_from_full(image, referenceName, 
&status, FALSE);
   if (status != MONO_IMAGE_OK || assembly == NULL)
   {
     printf("failed to load assembly\n");
     mono_image_close(image);
     return FALSE;
   }

   aName = mono_assembly_get_name(assembly);
   printf("Assembly Name: %s\n", mono_stringify_assembly_name(aName));

   res = mono_domain_set(rootDomain, FALSE);

   return TRUE;
}



/**********************************************************************************/
/* */
/**********************************************************************************/
static int monoScriptRun(void)
{
   int res, base;
   char *ret, *strErr;
   MonoMethodDesc* monoMethodDesc;
   MonoMethod* method;
   MonoObject *exception;


   if(domain == NULL || assembly == NULL)
   {
    printf("Run Failed: Not compiled\n");
     return FALSE;
   }

   if (!mono_domain_set(domain, FALSE))
   {
     printf("Run Failed: set domain failed\n");
     return FALSE;
   }

   monoMethodDesc = mono_method_desc_new("Script:Main", 0);
   if(monoMethodDesc == NULL)
   {
     printf("mono_method_desc_new failed\n" );
     return FALSE;
   }

   //Search the method in the image
   method = mono_method_desc_search_in_image(monoMethodDesc, image);
   mono_free(monoMethodDesc);
   if(method == NULL)
   {
     printf("mono_method_desc_search_in_image failed\n");
     return FALSE;
   }

   //run the method
   exception = NULL;
   mono_runtime_invoke(method, NULL, NULL, &exception);
   if(exception != NULL)
   {
     printf("An exception was thrown when calling Script:Main!\n");
     return FALSE;
   }

   res = mono_domain_set(rootDomain, FALSE);

   return TRUE;
}


/**********************************************************************************/
/* */
/**********************************************************************************/
static void monoInit(void)
{
   if(rootDomain == NULL)
   {
     mono_set_dirs("C:\\Program Files\\Mono\\lib",
                   "C:\\Program Files\\Mono\\etc");
     rootDomain = mono_jit_init("ScriptEngine");

     mono_thread_set_main(mono_thread_current());
   }
}



/**********************************************************************************/
/* */
/**********************************************************************************/
int main(int argc, char **argv)
{
   mono_report("Starting");
   monoInit();
   monoScriptCompile("code1", "code.dll");
   monoScriptRun();
   monoScriptCompile("code2", "code.dll");
   monoScriptRun();
   monoScriptCompile("code2", "code2.dll");
   monoScriptRun();
}
/******************************************************************************************************************/



The cs source (code1.cs) for the first ddl (compiled with mcs code1.cs 
-target:library):
/******************************************************************************************************************/
using System;
using System.Runtime.InteropServices;

public class ScriptEngine
{
[DllImport ("__Internal", EntryPoint="mono_report")]
static extern public void report (String str);
}

public class Script
{
   static void Main ()
   {
     ScriptEngine.report("Code 1");
   }
}
/******************************************************************************************************************/


The cs source(code2.cs) for the second ddl (compiled with mcs code2.cs 
-target:library):
/******************************************************************************************************************/
using System;
using System.Runtime.InteropServices;

public class ScriptEngine
{
[DllImport ("__Internal", EntryPoint="mono_report")]
static extern public void report (String str);
}



public class Script
{
   static void Main ()
   {
     ScriptEngine.report("Code 2");
   }
}
/******************************************************************************************************************/



On 13/02/2018 18:22, pierre wrote:
> Here a sample in one (dirty) c file...
> the file can be compiled easily, just adding the mono library and the 
> mono include path.
>
> the core is doing:
>
>       monoScriptCompile("code1", "code.dll");
>       monoScriptRun();
>       monoScriptCompile("code2", "code.dll");
>       monoScriptRun();
>       monoScriptCompile("code2", "code2.dll");
>       monoScriptRun();
>
> And the result on my system is:
>
>     Starting
>     Assembly Name: code1, Version=0.0.0.0, Culture=neutral,
>     PublicKeyToken=null
>     Code 1
>     Assembly Name: code1, Version=0.0.0.0, Culture=neutral,
>     PublicKeyToken=null
>     *Code 1*
>     Assembly Name: code2, Version=0.0.0.0, Culture=neutral,
>     PublicKeyToken=null
>     Code 2
>
> and as you can see, the code is not reloaded properly when the 
> reference is kept, but reloaded in the second case!!!
>
>
> On 13/02/2018 14:15, R Zaghi wrote:
>> Just for the record, I mean write it in a C# class and load that one 
>> class so you can handle everything in C# itself - much more robust.
>>
>>
>> On Tue, 13 Feb 2018 at 19:04, R Zaghi <[email protected] 
>> <mailto:[email protected]>> wrote:
>>
>>     If this is actually a problem with a library like mono then it
>>     sounds like a caching problem. If you build mono from source then
>>     it's easier to look into this...
>>
>>     Compare your code with this example. In the example, the .dll
>>     assembly is re-loaded in a loop. You can see the clean up portion
>>     and the shutdown or initialisation portions too:
>>
>>     https://github.com/ramin-zaghi/mono-embedding
>>
>>     Regarding how to compile code at runtime without a system() call,
>>     you can use CodeDom (look it up) to compile from files, or in a
>>     mono-specific way use Mono Compiler Service (e.g the Evaluator
>>     and CompilerContext classes - google them) which allow you to
>>     evaluate partial expressions/statements/etc.
>>
>>     I use both depending on situation and they work pretty well.
>>     Roslyn is apparently another option but let's not go there :)
>>
>>     R.
>>
>>
>>
>>
>>
>>     On Tue, 13 Feb 2018 at 16:17, pierre
>>     <[email protected]
>>     <mailto:[email protected]>> wrote:
>>
>>         Thanks for the answer.
>>
>>
>>         > you 100% sure the old files are all overwritten?
>>         Yes, I have checked the file time... and also included a
>>
>>             remove(fileName);
>>
>>         to be sure!
>>
>>
>>         >There are a couple of different ways to get compiled binary
>>         at runtime without a system() call
>>         Which ones?
>>
>>         I have tried something:
>>         calling mono_image_open_from_data_with_name and
>>         mono_assembly_load_from_full with a different file name on
>>         every compile and it is working!!!
>>
>>         so, the following code is not reloading properly:
>>
>>             monoEngine->fileName = strdup("code.dll");
>>
>>
>>             monoEngine->image =
>>             mono_image_open_from_data_with_name(data, dataLen,
>>             TRUE /* copy data */,
>>             &status,
>>             FALSE /* ref only */,
>>             monoEngine->fileName);
>>             if (status != MONO_IMAGE_OK || monoEngine->image == NULL)
>>             {
>>             }
>>             monoEngine->assembly =
>>             mono_assembly_load_from_full(monoEngine->image,
>>             monoEngine->fileName,
>>             &status, FALSE);
>>             if (status != MONO_IMAGE_OK || monoEngine->assembly == NULL)
>>             {
>>             }
>>
>>
>>         but, the same with a different file name on every run (only
>>         the monoEngine->fileName creation differ) is working:
>>
>>             static int version = 1;
>>             ...
>>
>>             sprintf(monoEngine->fileName, "code%03d.dll", version);
>>             version ++;
>>
>>
>>             monoEngine->image =
>>             mono_image_open_from_data_with_name(data, dataLen,
>>             TRUE /* copy data */,
>>             &status,
>>             FALSE /* ref only */,
>>             monoEngine->fileName);
>>             if (status != MONO_IMAGE_OK || monoEngine->image == NULL)
>>             {
>>             }
>>             monoEngine->assembly =
>>             mono_assembly_load_from_full(monoEngine->image,
>>             monoEngine->fileName,
>>             &status, FALSE);
>>             if (status != MONO_IMAGE_OK || monoEngine->assembly == NULL)
>>             {
>>             }
>>
>>         Is there a wait to be introduced after a mono_domain_unload?
>>         It is like doing mono_domain_unload,
>>         mono_image_open_from_data_with_name and
>>         mono_assembly_load_from_full with the same file name is
>>         detected and the unload is not performed....
>>
>>
>>         > This is more likely to be a problem outside of mono.
>>         I do agree... but I am running out of idea on why!!!
>>
>>
>>         and for mono_method_desc_search_in_image? is it bug?
>>
>>
>>         Pierre
>>
>>
>>         On 13/02/2018 07:15, R Zaghi wrote:
>>>         I think we need to know a bit more about what you are doing
>>>         in the code exactly but as a quick first guess if you are
>>>         recompiling using a system() call then are you 100% sure the
>>>         old files are all overwritten? There are a couple of
>>>         different ways to get compiled binary at runtime without a
>>>         system() call which I prefer but if you are using a system()
>>>         call then have you tried two separate calls with two
>>>         parallel binaries loaded as a start to debug your code?
>>>
>>>         This is more likely to be a problem outside of mono.
>>>
>>>         Ramin
>>>
>>>
>>>
>>>
>>>         Ramin Zaghi
>>>
>>>         *Mosaic3DX™ | User Interface Technology*
>>>         St John's Innovation Centre,
>>>         Cowley Road,
>>>         Cambridge,
>>>         CB4 0WS, UK*
>>>         *
>>>         *E*:**[email protected] <mailto:[email protected]>
>>>         *T*: +44 1223 421 311 <tel:+44%201223%20421311>
>>>         http://linkedin.com/in/raminzaghi
>>>
>>>
>>>
>>>         On Tue, 13 Feb 2018 at 01:27, pierre
>>>         <[email protected]
>>>         <mailto:[email protected]>> wrote:
>>>
>>>             Hi,
>>>
>>>             I am trying to embed mono... and I ran into a problem
>>>             with the code:
>>>
>>>                 monoMethodDesc = mono_method_desc_new("Script:Main", 0);
>>>                 method =
>>>                 mono_method_desc_search_in_image(monoMethodDesc,
>>>                 monoEngine->image);
>>>
>>>
>>>             It is returning a method on the cs code:
>>>
>>>                 public class Script
>>>                 {
>>>                   static public void Main ()
>>>                   {
>>>                 ScriptEngine.report("--Main Called ");
>>>                   }
>>>                 }
>>>
>>>
>>>             but it is also returning a method on the cs code (with
>>>             the wrong class name):
>>>
>>>                 public class Script*2*
>>>                 {
>>>                   static public void Main ()
>>>                   {
>>>                 ScriptEngine.report("--Main Called ");
>>>                   }
>>>                 }
>>>
>>>             while it should only return for:
>>>
>>>                 monoMethodDesc =
>>>                 mono_method_desc_new("Script2:Main", 0);
>>>
>>>
>>>             Am i doing something wrong or is this a bug? It seem
>>>             that mono_method_desc_search_in_image is returning a
>>>             value if the actual class name is starting with the
>>>             given  class name.... so, the same method is returned if
>>>             I look for "Script:Main" but the declared class is
>>>             "Script:Main", "Script_test:Main" or "Script2:Main"...
>>>
>>>             and a question:
>>>             is there a way to know if mono_domain_unload was
>>>             successful or not?
>>>
>>>             I am creating an app domain per script so that I can
>>>             recompile and reload the script at will...
>>>             I do not detect any error, but the new script seems not
>>>             to replace the old one...
>>>             Basically, I am doing:
>>>
>>>                 res = mono_domain_set(rootDomain, FALSE);
>>>                 mono_domain_unload(monoEngine->domain);
>>>                 monoEngine->domain =
>>>                 mono_domain_create_appdomain("ScriptEngine-sub", NULL);
>>>
>>>                 data = readFile(f, &dataLen);
>>>                 fclose(f);
>>>                 monoEngine->image =
>>>                 mono_image_open_from_data_with_name(data, dataLen,
>>>                 TRUE /* copy data */,
>>>                 &status,
>>>                 FALSE /* ref only */,
>>>                 monoEngine->fileName);
>>>                   free(data);
>>>                   if (status != MONO_IMAGE_OK || monoEngine->image
>>>                 == NULL)
>>>                   {
>>>                     return FALSE;
>>>                   }
>>>
>>>                     // load the assembly
>>>                   monoEngine->assembly =
>>>                 mono_assembly_load_from_full(monoEngine->image,
>>>                 monoEngine->fileName,
>>>                 &status, FALSE);
>>>                   if (status != MONO_IMAGE_OK ||
>>>                 monoEngine->assembly == NULL)
>>>                   {
>>>                 mono_image_close(monoEngine->image);
>>>                     return FALSE;
>>>                   }
>>>
>>>             but it does not seem to work. The code that runs is
>>>             always the first loaded one!!
>>>
>>>             I also added the following code to my engine:
>>>
>>>                 mono_trace_set_log_handler(monoLogCallback, NULL);
>>>                 mono_trace_set_print_handler(monoPrintCallback);
>>>                 mono_trace_set_printerr_handler(monoPrintCallback);
>>>                 mono_trace_set_level_string ("debug");
>>>
>>>
>>>             but it is reporting debug info only on the first run...
>>>             is there a way to having it working on every run?
>>>
>>>             Lastly, Is there a way to compile cs source without
>>>             launching a
>>>
>>>                 system("msc.code.cs -target:library");
>>>
>>>             thanks in advance
>>>
>>>             Pierre
>>>
>>>
>>>
>>>             _______________________________________________
>>>             Mono-list maillist  [email protected] <mailto:[email protected]>
>>>             http://lists.dot.net/mailman/listinfo/mono-list
>>>
>>>             _______________________________________________
>>>             Mono-devel-list mailing list
>>>             [email protected]
>>>             <mailto:[email protected]>
>>>             http://lists.dot.net/mailman/listinfo/mono-devel-list
>>>
>>>         -- 
>>>
>>>
>>>
>>>         Ramin Zaghi
>>>
>>>         *Mosaic3DX™ | User Interface Technology*
>>>         St John's Innovation Centre,
>>>         Cowley Road,
>>>         Cambridge,
>>>         CB4 0WS, UK*
>>>         *
>>>         *E*:**[email protected] <mailto:[email protected]>
>>>         *T*: +44 1223 421 311
>>>         http://linkedin.com/in/raminzaghi
>>>
>>
>>     -- 
>>
>>
>>
>>     Ramin Zaghi
>>
>>     *Mosaic3DX™ | User Interface Technology*
>>     St John's Innovation Centre,
>>     Cowley Road,
>>     Cambridge,
>>     CB4 0WS, UK*
>>     *
>>     *E*:**[email protected] <mailto:[email protected]>
>>     *T*: +44 1223 421 311
>>     http://linkedin.com/in/raminzaghi
>>
>> -- 
>>
>>
>>
>> Ramin Zaghi
>>
>> *Mosaic3DX™ | User Interface Technology*
>> St John's Innovation Centre,
>> Cowley Road,
>> Cambridge,
>> CB4 0WS, UK*
>> *
>> *E*:**[email protected] <mailto:[email protected]>
>> *T*: +44 1223 421 311
>> http://linkedin.com/in/raminzaghi
>>
>


--------------AC64A92F9E69F75AF5DF9B30
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">Hi,<br>
      <br>
      Anyone about this??<br>
      if I run "mono_domain_unload(domain);"<br>
      and after do<br>
      assembly = mono_assembly_load_from_full(image, referenceName,
      &amp;status, FALSE);<br>
      it is not working properly if referenceName is kept from one
      launch to the next...<br>
      I think the code bellow is showing it... or am I doing something
      wrong??<br>
      may be it is linked to the "<tt>[DllImport ("__Internal"..."</tt>
      which blocking the unloading of the assembly?<br>
      <br>
      And a question: Is there a way to debug a running assembly
      (embeded)? <br>
      On python and lua, you can register a function that is called on
      every line executed... I totally understand it is different for
      mono, but what are my options? where can I find documentations
      about debugging from and embedding application?<br>
      <br>
      Thanks,<br>
      Pierre<br>
      <br>
      The source to test is (mono_test.c):<br>
/******************************************************************************************************************/<br>
      <tt>#include &lt;mono/jit/jit.h&gt;</tt><tt><br>
      </tt><tt><br>
      </tt><tt>#include &lt;mono/metadata/mono-config.h&gt;</tt><tt><br>
      </tt><tt>#include &lt;mono/metadata/threads.h&gt;</tt><tt><br>
      </tt><tt>#include &lt;mono/metadata/assembly.h&gt;</tt><tt><br>
      </tt><tt>#include &lt;mono/metadata/mono-gc.h&gt;</tt><tt><br>
      </tt><tt>#include &lt;mono/metadata/environment.h&gt;</tt><tt><br>
      </tt><tt>#include &lt;mono/metadata/debug-helpers.h&gt;</tt><tt><br>
      </tt><tt>#include &lt;mono/metadata/mono-debug.h&gt;</tt><tt><br>
      </tt><tt>#include &lt;mono/utils/mono-logger.h&gt;</tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt>/**********************************************************************************/</tt><tt><br>
      </tt><tt>/*                                                                               
        */</tt><tt><br>
      </tt><tt>/**********************************************************************************/</tt><tt><br>
      </tt><tt>#define TRUE  1</tt><tt><br>
      </tt><tt>#define FALSE 0</tt><tt><br>
      </tt><tt>#define BYTE unsigned char</tt><tt><br>
      </tt><tt>#define gMalloc malloc</tt><tt><br>
      </tt><tt>#define gFree free</tt><tt><br>
      </tt><tt><br>
      </tt><tt>BYTE *readFile(FILE *f, size_t *size)</tt><tt><br>
      </tt><tt>{</tt><tt><br>
      </tt><tt>  BYTE *buffer;</tt><tt><br>
      </tt><tt>  size_t i;</tt><tt><br>
      </tt><tt><br>
      </tt><tt>  fseek(f, 0, SEEK_END);</tt><tt><br>
      </tt><tt>  *size = ftell(f);</tt><tt><br>
      </tt><tt>  fseek(f, 0, SEEK_SET);</tt><tt><br>
      </tt><tt><br>
      </tt><tt>  buffer = (BYTE *) gMalloc(*size);</tt><tt><br>
      </tt><tt>  if(buffer == NULL) return NULL;</tt><tt><br>
      </tt><tt><br>
      </tt><tt>  i = fread(buffer, 1, *size, f);</tt><tt><br>
      </tt><tt>  if(i != *size)</tt><tt><br>
      </tt><tt>  {</tt><tt><br>
      </tt><tt>    gFree(buffer);</tt><tt><br>
      </tt><tt>    return NULL;</tt><tt><br>
      </tt><tt>  }</tt><tt><br>
      </tt><tt>  return buffer;</tt><tt><br>
      </tt><tt>}</tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt>/**********************************************************************************/</tt><tt><br>
      </tt><tt>/*                                                                               
        */</tt><tt><br>
      </tt><tt>/**********************************************************************************/</tt><tt><br>
      </tt><tt>static MonoDomain *rootDomain;</tt><tt><br>
      </tt><tt>static MonoDomain *domain;</tt><tt><br>
      </tt><tt>static MonoImage *image;</tt><tt><br>
      </tt><tt>static MonoAssembly *assembly;</tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt>/**********************************************************************************/</tt><tt><br>
      </tt><tt>/*                                                                               
        */</tt><tt><br>
      </tt><tt>/**********************************************************************************/</tt><tt><br>
      </tt><tt>__declspec(dllexport) void mono_report(char* str)</tt><tt><br>
      </tt><tt>{</tt><tt><br>
      </tt><tt>  printf("%s\n", str);</tt><tt><br>
      </tt><tt>}</tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt>/**********************************************************************************/</tt><tt><br>
      </tt><tt>/*                                                                               
        */</tt><tt><br>
      </tt><tt>/**********************************************************************************/</tt><tt><br>
      </tt><tt>static int monoScriptCompile(char *fileName, char
        *referenceName)</tt><tt><br>
      </tt><tt>{</tt><tt><br>
      </tt><tt> </tt><tt><br>
      </tt><tt>  int res;</tt><tt><br>
      </tt><tt>  char *ret, *strErr, *str;</tt><tt><br>
      </tt><tt>  char buffer[256];</tt><tt><br>
      </tt><tt>  FILE *f;</tt><tt><br>
      </tt><tt>  BYTE *data;</tt><tt><br>
      </tt><tt>  size_t dataLen;</tt><tt><br>
      </tt><tt>  MonoImageOpenStatus status;</tt><tt><br>
      </tt><tt>  MonoAssemblyName *aName;</tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt>  if(domain != NULL)</tt><tt><br>
      </tt><tt>  {</tt><tt><br>
      </tt><tt>    res = mono_domain_set(rootDomain, FALSE);</tt><tt><br>
      </tt><tt>    if(!res) </tt><tt><br>
      </tt><tt>    {</tt><tt><br>
      </tt><tt>      printf("Failed to set root domain\n");</tt><tt><br>
      </tt><tt>      return FALSE;</tt><tt><br>
      </tt><tt>    }</tt><tt><br>
      </tt><tt><br>
      </tt><tt>    mono_domain_unload(domain);</tt><tt><br>
      </tt><tt>    mono_gc_collect(mono_gc_max_generation());</tt><tt><br>
      </tt><tt>  }</tt><tt><br>
      </tt><tt>  domain = NULL;</tt><tt><br>
      </tt><tt>  image = NULL;</tt><tt><br>
      </tt><tt>  assembly = NULL;</tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt>  domain =
        mono_domain_create_appdomain("ScriptSubDomain", NULL);</tt><tt><br>
      </tt><tt>  res = mono_domain_set(domain, FALSE);</tt><tt><br>
      </tt><tt>  if(!res) </tt><tt><br>
      </tt><tt>  {</tt><tt><br>
      </tt><tt>    printf("Failed to set domain\n");</tt><tt><br>
      </tt><tt>    return FALSE;</tt><tt><br>
      </tt><tt>  }</tt><tt><br>
      </tt><tt><br>
      </tt><tt>  sprintf(buffer, "%s.dll", fileName);</tt><tt><br>
      </tt><tt>  f = fopen(buffer, "rb");</tt><tt><br>
      </tt><tt>  if(f == NULL) </tt><tt><br>
      </tt><tt>  {</tt><tt><br>
      </tt><tt>    printf("Cannot open file: %s\n", buffer);</tt><tt><br>
      </tt><tt>    return FALSE;</tt><tt><br>
      </tt><tt>  }</tt><tt><br>
      </tt><tt>  data = readFile(f, &amp;dataLen);</tt><tt><br>
      </tt><tt>  fclose(f);</tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt>    // open the assembly from the data we read, so we
        never lock files</tt><tt><br>
      </tt><tt>  image = mono_image_open_from_data_with_name(data,
        dataLen, </tt><tt><br>
      </tt><tt>                                              TRUE /*
        copy data */, </tt><tt><br>
      </tt><tt>                                             
        &amp;status, </tt><tt><br>
      </tt><tt>                                              FALSE /*
        ref only */, </tt><tt><br>
      </tt><tt>                                             
        referenceName);</tt><tt><br>
      </tt><tt>  gFree(data);</tt><tt><br>
      </tt><tt>  if (status != MONO_IMAGE_OK || image == NULL)</tt><tt><br>
      </tt><tt>  {</tt><tt><br>
      </tt><tt>    printf("Cannot create image from data\n");</tt><tt><br>
      </tt><tt>    return FALSE;</tt><tt><br>
      </tt><tt>  }</tt><tt><br>
      </tt><tt><br>
      </tt><tt>    // load the assembly</tt><tt><br>
      </tt><tt>  assembly = mono_assembly_load_from_full(image,
        referenceName, &amp;status, FALSE);</tt><tt><br>
      </tt><tt>  if (status != MONO_IMAGE_OK || assembly == NULL)</tt><tt><br>
      </tt><tt>  {</tt><tt><br>
      </tt><tt>    printf("failed to load assembly\n");</tt><tt><br>
      </tt><tt>    mono_image_close(image);</tt><tt><br>
      </tt><tt>    return FALSE;</tt><tt><br>
      </tt><tt>  }</tt><tt><br>
      </tt><tt><br>
      </tt><tt>  aName = mono_assembly_get_name(assembly);</tt><tt><br>
      </tt><tt>  printf("Assembly Name: %s\n",
        mono_stringify_assembly_name(aName));</tt><tt><br>
      </tt><tt><br>
      </tt><tt>  res = mono_domain_set(rootDomain, FALSE);</tt><tt><br>
      </tt><tt><br>
      </tt><tt>  return TRUE;</tt><tt><br>
      </tt><tt>}</tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt>/**********************************************************************************/</tt><tt><br>
      </tt><tt>/*                                                                               
        */</tt><tt><br>
      </tt><tt>/**********************************************************************************/</tt><tt><br>
      </tt><tt>static int monoScriptRun(void)</tt><tt><br>
      </tt><tt>{</tt><tt><br>
      </tt><tt>  int res, base;</tt><tt><br>
      </tt><tt>  char *ret, *strErr;</tt><tt><br>
      </tt><tt>  MonoMethodDesc* monoMethodDesc;</tt><tt><br>
      </tt><tt>  MonoMethod* method;</tt><tt><br>
      </tt><tt>  MonoObject *exception;</tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt>  if(domain == NULL || assembly == NULL)</tt><tt><br>
      </tt><tt>  {</tt><tt><br>
      </tt><tt>   printf("Run Failed: Not compiled\n");</tt><tt><br>
      </tt><tt>    return FALSE;</tt><tt><br>
      </tt><tt>  }</tt><tt><br>
      </tt><tt><br>
      </tt><tt>  if (!mono_domain_set(domain, FALSE)) </tt><tt><br>
      </tt><tt>  {</tt><tt><br>
      </tt><tt>    printf("Run Failed: set domain failed\n");</tt><tt><br>
      </tt><tt>    return FALSE;</tt><tt><br>
      </tt><tt>  }</tt><tt><br>
      </tt><tt>  </tt><tt><br>
      </tt><tt>  monoMethodDesc = mono_method_desc_new("Script:Main",
        0);</tt><tt><br>
      </tt><tt>  if(monoMethodDesc == NULL)</tt><tt><br>
      </tt><tt>  {</tt><tt><br>
      </tt><tt>    printf("mono_method_desc_new failed\n" );</tt><tt><br>
      </tt><tt>    return FALSE;</tt><tt><br>
      </tt><tt>  }</tt><tt><br>
      </tt><tt><br>
      </tt><tt>  //Search the method in the image</tt><tt><br>
      </tt><tt>  method =
        mono_method_desc_search_in_image(monoMethodDesc, image);</tt><tt><br>
      </tt><tt>  mono_free(monoMethodDesc);</tt><tt><br>
      </tt><tt>  if(method == NULL)</tt><tt><br>
      </tt><tt>  {</tt><tt><br>
      </tt><tt>    printf("mono_method_desc_search_in_image failed\n");</tt><tt><br>
      </tt><tt>    return FALSE;</tt><tt><br>
      </tt><tt>  }</tt><tt><br>
      </tt><tt><br>
      </tt><tt>  //run the method</tt><tt><br>
      </tt><tt>  exception = NULL;</tt><tt><br>
      </tt><tt>  mono_runtime_invoke(method, NULL, NULL,
        &amp;exception);</tt><tt><br>
      </tt><tt>  if(exception != NULL) </tt><tt><br>
      </tt><tt>  {</tt><tt><br>
      </tt><tt>    printf("An exception was thrown when calling
        Script:Main!\n");</tt><tt><br>
      </tt><tt>    return FALSE;</tt><tt><br>
      </tt><tt>  }</tt><tt><br>
      </tt><tt><br>
      </tt><tt>  res = mono_domain_set(rootDomain, FALSE);</tt><tt><br>
      </tt><tt><br>
      </tt><tt>  return TRUE;</tt><tt><br>
      </tt><tt>}</tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt>/**********************************************************************************/</tt><tt><br>
      </tt><tt>/*                                                                               
        */</tt><tt><br>
      </tt><tt>/**********************************************************************************/</tt><tt><br>
      </tt><tt>static void monoInit(void)</tt><tt><br>
      </tt><tt>{</tt><tt><br>
      </tt><tt>  if(rootDomain == NULL)</tt><tt><br>
      </tt><tt>  {</tt><tt><br>
      </tt><tt>    mono_set_dirs("C:\\Program Files\\Mono\\lib",</tt><tt><br>
      </tt><tt>                  "C:\\Program Files\\Mono\\etc");</tt><tt><br>
      </tt><tt>    rootDomain = mono_jit_init("ScriptEngine");</tt><tt><br>
      </tt><tt><br>
      </tt><tt>    mono_thread_set_main(mono_thread_current());</tt><tt><br>
      </tt><tt>  }</tt><tt><br>
      </tt><tt>}</tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt><br>
      </tt><tt>/**********************************************************************************/</tt><tt><br>
      </tt><tt>/*                                                                               
        */</tt><tt><br>
      </tt><tt>/**********************************************************************************/</tt><tt><br>
      </tt><tt>int main(int argc, char **argv)</tt><tt><br>
      </tt><tt>{</tt><tt><br>
      </tt><tt>  mono_report("Starting");</tt><tt><br>
      </tt><tt>  monoInit();</tt><tt><br>
      </tt><tt>  monoScriptCompile("code1", "code.dll");</tt><tt><br>
      </tt><tt>  monoScriptRun();</tt><tt><br>
      </tt><tt>  monoScriptCompile("code2", "code.dll");</tt><tt><br>
      </tt><tt>  monoScriptRun();</tt><tt><br>
      </tt><tt>  monoScriptCompile("code2", "code2.dll");</tt><tt><br>
      </tt><tt>  monoScriptRun();</tt><tt><br>
      </tt><tt>}</tt><br>
/******************************************************************************************************************/<br>
      <br>
      <br>
      <br>
      The cs source (code1.cs) for the first ddl (compiled with mcs
      code1.cs -target:library):<br>
/******************************************************************************************************************/<br>
      <tt>using System;</tt><tt><br>
      </tt><tt>using System.Runtime.InteropServices;</tt><tt><br>
      </tt><tt><br>
      </tt><tt>public class ScriptEngine</tt><tt><br>
      </tt><tt>{</tt><tt><br>
      </tt><tt>[DllImport ("__Internal", EntryPoint="mono_report")]</tt><tt><br>
      </tt><tt>static extern public void report (String str);</tt><tt><br>
      </tt><tt>}</tt><tt><br>
      </tt><tt></tt><tt><br>
      </tt><tt>public class Script</tt><tt><br>
      </tt><tt>{</tt><tt><br>
      </tt><tt>  static void Main ()</tt><tt><br>
      </tt><tt>  {</tt><tt><br>
      </tt><tt>    ScriptEngine.report("Code 1");</tt><tt><br>
      </tt><tt>  }</tt><tt><br>
      </tt><tt>}</tt><br>
/******************************************************************************************************************/<br>
      <br>
      <br>
      The cs source(code2.cs) for the second ddl (compiled with mcs
      code2.cs -target:library):<br>
/******************************************************************************************************************/<br>
      <tt>using System;</tt><tt><br>
      </tt><tt>
        using System.Runtime.InteropServices;</tt><tt><br>
      </tt><tt>
      </tt><tt><br>
      </tt><tt>
        public class ScriptEngine</tt><tt><br>
      </tt><tt>
        {</tt><tt><br>
      </tt><tt>
        [DllImport ("__Internal", EntryPoint="mono_report")]</tt><tt><br>
      </tt><tt>
        static extern public void report (String str);</tt><tt><br>
      </tt><tt>
        }</tt><tt><br>
      </tt><tt>
      </tt><tt><br>
      </tt><tt>
      </tt><tt><br>
      </tt><tt>
      </tt><tt><br>
      </tt><tt>
        public class Script</tt><tt><br>
      </tt><tt>
        {</tt><tt><br>
      </tt><tt>
          static void Main ()</tt><tt><br>
      </tt><tt>
          {</tt><tt><br>
      </tt><tt>
            ScriptEngine.report("Code 2");</tt><tt><br>
      </tt><tt>
          }</tt><tt><br>
      </tt><tt>
        }</tt><br>
/******************************************************************************************************************/<br>
      <br>
      <br>
      <br>
      On 13/02/2018 18:22, pierre wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:[email protected]">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <div class="moz-cite-prefix">Here a sample in one (dirty) c
        file...<br>
        the file can be compiled easily, just adding the mono library
        and the mono include path.<br>
        <br>
        the core is doing:<br>
        <blockquote><tt>  monoScriptCompile("code1", "code.dll");</tt><tt><br>
          </tt><tt>  monoScriptRun();</tt><tt><br>
          </tt><tt>  monoScriptCompile("code2", "code.dll");</tt><tt><br>
          </tt><tt>  monoScriptRun();</tt><tt><br>
          </tt><tt>  monoScriptCompile("code2", "code2.dll");</tt><tt><br>
          </tt><tt>  monoScriptRun();</tt><br>
        </blockquote>
        And the result on my system is:<br>
        <blockquote><tt>Starting</tt><br>
          <tt>Assembly Name: code1, Version=0.0.0.0, Culture=neutral,
            PublicKeyToken=null</tt><br>
          <tt>Code 1</tt><br>
          <tt>Assembly Name: code1, Version=0.0.0.0, Culture=neutral,
            PublicKeyToken=null</tt><br>
          <b><font size="+1" color="#ff0000"><tt>Code 1</tt></font></b><br>
          <tt>Assembly Name: code2, Version=0.0.0.0, Culture=neutral,
            PublicKeyToken=null</tt><br>
          <tt>Code 2</tt><br>
        </blockquote>
        and as you can see, the code is not reloaded properly when the
        reference is kept, but reloaded in the second case!!!<br>
        <br>
        <br>
        On 13/02/2018 14:15, R Zaghi wrote:<br>
      </div>
      <blockquote type="cite"
cite="mid:CAFLMc5sGxa1Y_hK5OT60stR_50agzXDFxVc4+DmnET=xzNyEKA@mail.gmail.com">
        <div>
          <div dir="auto">Just for the record, I mean write it in a C#
            class and load that one class so you can handle everything
            in C# itself - much more robust.</div>
          <div dir="auto"><br>
          </div>
          <br>
          <div class="gmail_quote">
            <div>On Tue, 13 Feb 2018 at 19:04, R Zaghi &lt;<a
                href="mailto:[email protected]"
                moz-do-not-send="true">[email protected]</a>&gt;
              wrote:<br>
            </div>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
              <div>
                <div dir="auto">If this is actually a problem with a
                  library like mono then it sounds like a caching
                  problem. If you build mono from source then it's
                  easier to look into this...</div>
                <div dir="auto"><br>
                </div>
                <div dir="auto">Compare your code with this example. In
                  the example, the .dll assembly is re-loaded in a loop.
                  You can see the clean up portion and the shutdown or
                  initialisation portions too:</div>
                <div dir="auto"><br>
                </div>
                <div dir="auto"><a
                    href="https://github.com/ramin-zaghi/mono-embedding"
                    target="_blank" moz-do-not-send="true">https://github.com/ramin-zaghi/mono-embedding</a><br>
                </div>
                <div dir="auto"><br>
                </div>
                <div dir="auto">Regarding how to compile code at runtime
                  without a system() call, you can use CodeDom (look it
                  up) to compile from files, or in a mono-specific way
                  use Mono Compiler Service (e.g the Evaluator and
                  CompilerContext classes - google them) which allow you
                  to evaluate partial expressions/statements/etc.</div>
                <div dir="auto"><br>
                </div>
                <div dir="auto">I use both depending on situation and
                  they work pretty well. Roslyn is apparently another
                  option but let's not go there :)</div>
              </div>
              <div>
                <div dir="auto"><br>
                </div>
                <div dir="auto">R.</div>
              </div>
              <div>
                <div dir="auto"><br>
                </div>
                <div dir="auto"><br>
                </div>
                <div dir="auto"><br>
                </div>
                <div dir="auto"><br>
                </div>
                <br>
                <div class="gmail_quote">
                  <div>On Tue, 13 Feb 2018 at 16:17, pierre &lt;<a
                      href="mailto:[email protected]"
                      target="_blank" moz-do-not-send="true">[email protected]</a>&gt;
                    wrote:<br>
                  </div>
                  <blockquote class="gmail_quote" style="margin:0 0 0
                    .8ex;border-left:1px #ccc solid;padding-left:1ex">
                    <div text="#000000" bgcolor="#FFFFFF">
                      <div
                        class="m_8137183264084839095m_-2028573316465613946moz-cite-prefix">Thanks
                        for the answer.</div>
                    </div>
                    <div text="#000000" bgcolor="#FFFFFF">
                      <div
                        class="m_8137183264084839095m_-2028573316465613946moz-cite-prefix"><br>
                        <br>
                        &gt; you 100% sure the old files are all
                        overwritten? <br>
                      </div>
                    </div>
                    <div text="#000000" bgcolor="#FFFFFF">
                      <div
                        class="m_8137183264084839095m_-2028573316465613946moz-cite-prefix">
                        Yes, I have checked the file time... and also
                        included a<br>
                        <blockquote><tt>remove(fileName);</tt><br>
                        </blockquote>
                        to be sure!</div>
                    </div>
                    <div text="#000000" bgcolor="#FFFFFF">
                      <div
                        class="m_8137183264084839095m_-2028573316465613946moz-cite-prefix"><br>
                        <br>
                        &gt;There are a couple of different ways to get
                        compiled binary at runtime without a system()
                        call <br>
                      </div>
                    </div>
                    <div text="#000000" bgcolor="#FFFFFF">
                      <div
                        class="m_8137183264084839095m_-2028573316465613946moz-cite-prefix">
                        Which ones? <br>
                        <br>
                        I have tried something:  <br>
                        calling <tt>mono_image_open_from_data_with_name</tt>
                        and <tt>mono_assembly_load_from_full </tt>with
                        a different file name on every compile and it is
                        working!!!<br>
                        <br>
                        so, the following code is not reloading
                        properly:<br>
                        <blockquote><tt>monoEngine-&gt;fileName =
                            strdup("code.dll");</tt></blockquote>
                      </div>
                    </div>
                    <div text="#000000" bgcolor="#FFFFFF">
                      <div
                        class="m_8137183264084839095m_-2028573316465613946moz-cite-prefix">
                        <blockquote><br>
                          <tt>monoEngine-&gt;image =
                            mono_image_open_from_data_with_name(data,
                            dataLen, </tt><br>
                          <tt>                                                         
                            TRUE /* copy data */, </tt><br>
                          <tt>                                                         
                            &amp;status, </tt><br>
                          <tt>                                                         
                            FALSE /* ref only */, </tt><br>
                          <tt>                                                         
                            monoEngine-&gt;fileName);</tt><br>
                          <tt>if (status != MONO_IMAGE_OK ||
                            monoEngine-&gt;image == NULL)</tt><br>
                          <tt>{</tt><br>
                          <tt>}</tt><br>
                          <tt>monoEngine-&gt;assembly =
                            mono_assembly_load_from_full(monoEngine-&gt;image,
                          </tt><br>
                          <tt>                                                     
                            monoEngine-&gt;fileName, </tt><br>
                          <tt>                                                     
                            &amp;status, FALSE);</tt><br>
                          <tt>if (status != MONO_IMAGE_OK ||
                            monoEngine-&gt;assembly == NULL)</tt><br>
                          <tt>{</tt><br>
                          <tt>}</tt><br>
                        </blockquote>
                      </div>
                    </div>
                    <div text="#000000" bgcolor="#FFFFFF">
                      <div
                        class="m_8137183264084839095m_-2028573316465613946moz-cite-prefix">
                        <br>
                        but, the same with a different file name on
                        every run (only the <tt>monoEngine-&gt;fileName</tt>
                        creation differ<tt>) is working</tt>:<br>
                        <blockquote><tt>static int version = 1;</tt><br>
                          <tt>...</tt><br>
                          <br>
                          <tt>sprintf(monoEngine-&gt;fileName, 
                            "code%03d.dll", version);</tt><br>
                          <tt>version ++;</tt></blockquote>
                      </div>
                    </div>
                    <div text="#000000" bgcolor="#FFFFFF">
                      <div
                        class="m_8137183264084839095m_-2028573316465613946moz-cite-prefix">
                        <blockquote><br>
                          <tt>monoEngine-&gt;image =
                            mono_image_open_from_data_with_name(data,
                            dataLen, </tt><br>
                          <tt>
                                                                                     
                            TRUE /* copy data */, </tt><br>
                          <tt>
                                                                                     
                            &amp;status, </tt><br>
                          <tt>
                                                                                     
                            FALSE /* ref only */, </tt><br>
                          <tt>
                                                                                     
                            monoEngine-&gt;fileName);</tt><br>
                          <tt> if (status != MONO_IMAGE_OK ||
                            monoEngine-&gt;image == NULL)</tt><br>
                          <tt> {</tt><br>
                          <tt> }</tt><br>
                          <tt> monoEngine-&gt;assembly =
                            mono_assembly_load_from_full(monoEngine-&gt;image,
                          </tt><br>
                          <tt>
                                                                                 
                            monoEngine-&gt;fileName, </tt><br>
                          <tt>
                                                                                 
                            &amp;status, FALSE);</tt><br>
                          <tt> if (status != MONO_IMAGE_OK ||
                            monoEngine-&gt;assembly == NULL)</tt><br>
                          <tt> {</tt><br>
                          <tt> }</tt><br>
                        </blockquote>
                      </div>
                    </div>
                    <div text="#000000" bgcolor="#FFFFFF">
                      <div
                        class="m_8137183264084839095m_-2028573316465613946moz-cite-prefix">
                        Is there a wait to be introduced after a
                        mono_domain_unload? It is like doing <tt>mono_domain_unload,
                          mono_image_open_from_data_with_name and </tt><tt>mono_assembly_load_from_full
                        </tt>with the same file name is detected <tt>and
                          the unload is not performed....<br>
                        </tt></div>
                    </div>
                    <div text="#000000" bgcolor="#FFFFFF">
                      <div
                        class="m_8137183264084839095m_-2028573316465613946moz-cite-prefix"><tt> </tt><br>
                        <br>
                        &gt; This is more likely to be a problem outside
                        of mono.<br>
                      </div>
                    </div>
                    <div text="#000000" bgcolor="#FFFFFF">
                      <div
                        class="m_8137183264084839095m_-2028573316465613946moz-cite-prefix">
                        I do agree... but I am running out of idea on
                        why!!!<br>
                        <br>
                        <br>
                        and for mono_method_desc_search_in_image? is it
                        bug?</div>
                    </div>
                    <div text="#000000" bgcolor="#FFFFFF">
                      <div
                        class="m_8137183264084839095m_-2028573316465613946moz-cite-prefix"><br>
                        <br>
                        Pierre</div>
                    </div>
                    <div text="#000000" bgcolor="#FFFFFF">
                      <div
                        class="m_8137183264084839095m_-2028573316465613946moz-cite-prefix"><br>
                        <br>
                        On 13/02/2018 07:15, R Zaghi wrote:<br>
                      </div>
                    </div>
                    <div text="#000000" bgcolor="#FFFFFF">
                      <blockquote type="cite">
                        <div>
                          <div>
                            <div dir="auto">I think we need to know a
                              bit more about what you are doing in the
                              code exactly but as a quick first guess if
                              you are recompiling using a system() call
                              then are you 100% sure the old files are
                              all overwritten? There are a couple of
                              different ways to get compiled binary at
                              runtime without a system() call which I
                              prefer but if you are using a system()
                              call then have you tried two separate
                              calls with two parallel binaries loaded as
                              a start to debug your code?</div>
                            <div dir="auto"><br>
                            </div>
                            <div dir="auto">This is more likely to be a
                              problem outside of mono.</div>
                            <div dir="auto"><br>
                            </div>
                            <div dir="auto">Ramin</div>
                          </div>
                        </div>
                        <div>
                          <div>
                            <div dir="auto"><br>
                            </div>
                            <div dir="auto"><br>
                            </div>
                            <div dir="auto"><br>
                            </div>
                            <div dir="auto"><br>
                            </div>
                            <div dir="auto">
                              <div
style="color:rgb(117,117,117);word-spacing:1px;font-size:1rem;line-height:normal!important">Ramin
                                Zaghi</div>
                              <div
style="color:rgb(117,117,117);word-spacing:1px;line-height:normal!important"><br
                                  style="line-height:normal!important">
                                <div
                                  style="line-height:normal!important"><img
src="https://drive.google.com/uc?id=0B3xiwa7kf1uwRlYxenBFM1o2NEk&amp;export=download"
                                    style="line-height:normal!important"
                                    moz-do-not-send="true"><b
                                    style="line-height:normal!important;font-size:1rem">Mosaic3DX™
                                    | <font
                                      style="line-height:normal!important;font-size:0.625rem">User
                                      Interface Technology</font></b></div>
                                <div
                                  style="line-height:normal!important">
                                  <div
                                    style="line-height:normal!important;font-size:0.8125rem">St
                                    John's Innovation Centre,</div>
                                  <div
                                    style="line-height:normal!important;font-size:0.8125rem">Cowley
                                    Road,</div>
                                  <div
                                    style="line-height:normal!important;font-size:0.8125rem">Cambridge,</div>
                                  <div
                                    style="line-height:normal!important;font-size:0.8125rem">CB4
                                    0WS, UK<b
                                      style="line-height:normal!important"><br
style="line-height:normal!important">
                                    </b></div>
                                  <div
                                    style="line-height:normal!important;font-size:0.8125rem"><b
style="line-height:normal!important;font-size:0.8125rem">E</b>:<b
                                      style="line-height:normal!important"> </b><a
                                      href="mailto:[email protected]"
style="font-size:0.8125rem;line-height:normal!important" target="_blank"
                                      moz-do-not-send="true">[email protected]</a></div>
                                  <div
                                    style="line-height:normal!important;font-size:0.8125rem"><b
style="line-height:normal!important;font-size:0.8125rem">T</b>: <a
                                      href="tel:+44%201223%20421311"
                                      value="+441223421311"
                                      style="font-size:0.8125rem;line-height:normal!important"
                                      target="_blank"
                                      moz-do-not-send="true">+44 1223
                                      421 311</a></div>
                                </div>
                                <div
                                  style="line-height:normal!important"><font
                                    style="line-height:normal!important"
                                    size="2"><a
                                      href="http://linkedin.com/in/raminzaghi"
style="font-size:0.8125rem;line-height:normal!important" target="_blank"
                                      moz-do-not-send="true">http://linkedin.com/in/raminzaghi</a></font></div>
                                <div
                                  style="line-height:normal!important"
                                  dir="auto"><br>
                                </div>
                              </div>
                            </div>
                            <div dir="auto"><br>
                            </div>
                            <br>
                            <div class="gmail_quote">
                              <div>On Tue, 13 Feb 2018 at 01:27, pierre
                                &lt;<a
                                  href="mailto:[email protected]"
                                  target="_blank" moz-do-not-send="true">[email protected]</a>&gt;
                                wrote:<br>
                              </div>
                              <blockquote class="gmail_quote"
                                style="margin:0 0 0 .8ex;border-left:1px
                                #ccc solid;padding-left:1ex">
                                <div text="#000000" bgcolor="#FFFFFF">
                                  <div
class="m_8137183264084839095m_-2028573316465613946m_5069017230163669461m_-3097578244239708022moz-text-html"
                                    lang="x-western"> Hi,<br>
                                    <br>
                                    I am trying to embed mono... and I
                                    ran into a problem with the code: <br>
                                    <blockquote><tt>monoMethodDesc =
                                        mono_method_desc_new("Script:Main",
                                        0);</tt><tt><br>
                                      </tt><tt>method =
                                        mono_method_desc_search_in_image(monoMethodDesc,
                                        monoEngine-&gt;image);</tt><br>
                                    </blockquote>
                                    <br>
                                    It is returning a method on the cs
                                    code:<br>
                                    <blockquote><tt>public class Script</tt><tt><br>
                                      </tt><tt>{</tt><tt><br>
                                      </tt><tt>  static public void Main
                                        ()</tt><tt><br>
                                      </tt><tt>  {</tt><tt><br>
                                      </tt><tt>   
                                        ScriptEngine.report("--Main
                                        Called ");</tt><tt><br>
                                      </tt><tt>  }</tt><tt><br>
                                      </tt><tt>}</tt><br>
                                    </blockquote>
                                    <br>
                                    but it is also returning a method on
                                    the cs code (with the wrong class
                                    name):<br>
                                    <br>
                                    <blockquote><tt>public class Script<b>2</b></tt><tt><br>
                                      </tt><tt> {</tt><tt><br>
                                      </tt><tt>   static public void
                                        Main ()</tt><tt><br>
                                      </tt><tt>   {</tt><tt><br>
                                      </tt><tt>    
                                        ScriptEngine.report("--Main
                                        Called ");</tt><tt><br>
                                      </tt><tt>   }</tt><tt><br>
                                      </tt><tt> }</tt><br>
                                    </blockquote>
                                    while it should only return for:<br>
                                    <blockquote><tt>monoMethodDesc =
                                        mono_method_desc_new("Script2:Main",
                                        0);</tt><br>
                                    </blockquote>
                                    <tt> </tt><br>
                                    Am i doing something wrong or is
                                    this a bug? It seem that <tt>mono_method_desc_search_in_image
                                    </tt>is returning a value if the
                                    actual class name is starting with
                                    the given  class name.... so, the
                                    same method is returned if I look
                                    for "Script:Main" but the declared
                                    class is "Script:Main",
                                    "Script_test:Main" or
                                    "Script2:Main"...<br>
                                    <br>
                                    and a question:<br>
                                    is there a way to know if
                                    mono_domain_unload was successful or
                                    not? <br>
                                    <br>
                                    I am creating an app domain per
                                    script so that I can recompile and
                                    reload the script at will... <br>
                                    I do not detect any error, but the
                                    new script seems not to replace the
                                    old one...<br>
                                    Basically, I am doing:<br>
                                    <blockquote><tt>res =
                                        mono_domain_set(rootDomain,
                                        FALSE);</tt><br>
                                      <tt>mono_domain_unload(monoEngine-&gt;domain);</tt><br>
                                      <tt>monoEngine-&gt;domain =
                                        mono_domain_create_appdomain("ScriptEngine-sub",
                                        NULL);</tt><br>
                                      <br>
                                      <tt>data = readFile(f,
                                        &amp;dataLen);</tt><br>
                                      <tt>fclose(f);</tt><br>
                                      <tt>monoEngine-&gt;image =
                                        mono_image_open_from_data_with_name(data,
                                        dataLen, </tt><br>
                                      <tt>                                                         
                                        TRUE /* copy data */, </tt><br>
                                      <tt>                                                         
                                        &amp;status, </tt><br>
                                      <tt>                                                         
                                        FALSE /* ref only */, </tt><br>
                                      <tt>                                                         
                                        monoEngine-&gt;fileName);</tt><br>
                                      <tt>  free(data);</tt><br>
                                      <tt>  if (status != MONO_IMAGE_OK
                                        || monoEngine-&gt;image == NULL)</tt><br>
                                      <tt>  {</tt><br>
                                      <tt>    return FALSE;</tt><br>
                                      <tt>  }</tt><br>
                                      <br>
                                      <tt>    // load the assembly</tt><br>
                                      <tt>  monoEngine-&gt;assembly =
                                        mono_assembly_load_from_full(monoEngine-&gt;image,
                                      </tt><br>
                                      <tt>                                                     
                                        monoEngine-&gt;fileName, </tt><br>
                                      <tt>                                                     
                                        &amp;status, FALSE);</tt><br>
                                      <tt>  if (status != MONO_IMAGE_OK
                                        || monoEngine-&gt;assembly ==
                                        NULL)</tt><br>
                                      <tt>  {</tt><br>
                                      <tt>   
                                        mono_image_close(monoEngine-&gt;image);</tt><br>
                                      <tt>    return FALSE;</tt><br>
                                      <tt>  }</tt><br>
                                    </blockquote>
                                    but it does not seem to work. The
                                    code that runs is always the first
                                    loaded one!! <br>
                                    <br>
                                    I also added the following code to
                                    my engine:<br>
                                    <blockquote><tt>mono_trace_set_log_handler(monoLogCallback,
                                        NULL);</tt><br>
                                      <tt>mono_trace_set_print_handler(monoPrintCallback);</tt><br>
                                      <tt>mono_trace_set_printerr_handler(monoPrintCallback);</tt><br>
                                      <tt>mono_trace_set_level_string
                                        ("debug");</tt><br>
                                    </blockquote>
                                    <br>
                                    but it is reporting debug info only
                                    on the first run... is there a way
                                    to having it working on every run?<br>
                                    <br>
                                    Lastly, Is there a way to compile cs
                                    source without launching a <br>
                                    <blockquote><tt>system("msc.code.cs
                                        -target:library");</tt><br>
                                    </blockquote>
                                    thanks in advance<br>
                                    <br>
                                    Pierre<br>
                                    <br>
                                  </div>
                                  <br>
                                  <fieldset
class="m_8137183264084839095m_-2028573316465613946m_5069017230163669461m_-3097578244239708022mimeAttachmentHeader"></fieldset>
                                  <br>
                                  <div
class="m_8137183264084839095m_-2028573316465613946m_5069017230163669461m_-3097578244239708022moz-text-plain"
style="font-family:-moz-fixed;font-size:14px" lang="x-unicode">
                                    <pre>_______________________________________________
Mono-list maillist  -  <a class="m_8137183264084839095m_-2028573316465613946m_5069017230163669461m_-3097578244239708022moz-txt-link-abbreviated" href="mailto:[email protected]" target="_blank" moz-do-not-send="true">[email protected]</a>
<a class="m_8137183264084839095m_-2028573316465613946m_5069017230163669461m_-3097578244239708022moz-txt-link-freetext" href="http://lists.dot.net/mailman/listinfo/mono-list" target="_blank" moz-do-not-send="true">http://lists.dot.net/mailman/listinfo/mono-list</a>
</pre>
                                  </div>
                                </div>
_______________________________________________<br>
                                Mono-devel-list mailing list<br>
                                <a
                                  href="mailto:[email protected]"
                                  target="_blank" moz-do-not-send="true">[email protected]</a><br>
                                <a
                                  href="http://lists.dot.net/mailman/listinfo/mono-devel-list"
                                  rel="noreferrer" target="_blank"
                                  moz-do-not-send="true">http://lists.dot.net/mailman/listinfo/mono-devel-list</a><br>
                              </blockquote>
                            </div>
                          </div>
                        </div>
                        <div>-- <br>
                        </div>
                        <div
                          class="m_8137183264084839095m_-2028573316465613946gmail_signature"
                          data-smartmail="gmail_signature">
                          <div>
                            <div>
                              <div>
                                <div>
                                  <div>
                                    <div>
                                      <div>
                                        <div>
                                          <div><br>
                                          </div>
                                          <div><br>
                                          </div>
                                          <div><br>
                                          </div>
                                          <div>Ramin Zaghi</div>
                                          <div><br>
                                            <div><img
src="https://drive.google.com/uc?id=0B3xiwa7kf1uwRlYxenBFM1o2NEk&amp;export=download"
                                                moz-do-not-send="true"><b>Mosaic3DX™
                                                | <font size="1">User
                                                  Interface Technology</font></b></div>
                                            <div>
                                              <div
                                                style="font-size:small">St
                                                John's Innovation
                                                Centre,</div>
                                              <div
                                                style="font-size:small">Cowley
                                                Road,</div>
                                              <div
                                                style="font-size:small">Cambridge,</div>
                                              <div
                                                style="font-size:small">CB4
                                                0WS, UK<b><br>
                                                </b></div>
                                              <div
                                                style="font-size:small"><b>E</b>:<b> </b><a
href="mailto:[email protected]" target="_blank"
                                                  moz-do-not-send="true">[email protected]</a></div>
                                              <div
                                                style="font-size:small"><b>T</b>: +44
                                                1223 421 311</div>
                                            </div>
                                            <div><font size="2"><a
                                                  href="http://linkedin.com/in/raminzaghi"
                                                  target="_blank"
                                                  moz-do-not-send="true">http://linkedin.com/in/raminzaghi</a></font></div>
                                            <div><font size="2"><br>
                                              </font></div>
                                          </div>
                                        </div>
                                      </div>
                                    </div>
                                  </div>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </blockquote>
                      <p><br>
                      </p>
                    </div>
                  </blockquote>
                </div>
              </div>
              <div>-- <br>
              </div>
              <div class="m_8137183264084839095gmail_signature"
                data-smartmail="gmail_signature">
                <div>
                  <div>
                    <div>
                      <div>
                        <div>
                          <div>
                            <div>
                              <div>
                                <div><br>
                                </div>
                                <div><br>
                                </div>
                                <div><br>
                                </div>
                                <div>Ramin Zaghi</div>
                                <div><br>
                                  <div><img
src="https://drive.google.com/uc?id=0B3xiwa7kf1uwRlYxenBFM1o2NEk&amp;export=download"
                                      moz-do-not-send="true"><b>Mosaic3DX™
                                      | <font size="1">User Interface
                                        Technology</font></b></div>
                                  <div>
                                    <div style="font-size:small">St
                                      John's Innovation Centre,</div>
                                    <div style="font-size:small">Cowley
                                      Road,</div>
                                    <div style="font-size:small">Cambridge,</div>
                                    <div style="font-size:small">CB4
                                      0WS, UK<b><br>
                                      </b></div>
                                    <div style="font-size:small"><b>E</b>:<b> </b><a
href="mailto:[email protected]" target="_blank"
                                        moz-do-not-send="true">[email protected]</a></div>
                                    <div style="font-size:small"><b>T</b>: +44
                                      1223 421 311</div>
                                  </div>
                                  <div><font size="2"><a
                                        href="http://linkedin.com/in/raminzaghi"
                                        target="_blank"
                                        moz-do-not-send="true">http://linkedin.com/in/raminzaghi</a></font></div>
                                  <div><font size="2"><br>
                                    </font></div>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </blockquote>
          </div>
        </div>
        <div dir="ltr">-- <br>
        </div>
        <div class="gmail_signature" data-smartmail="gmail_signature">
          <div dir="ltr">
            <div>
              <div dir="ltr">
                <div>
                  <div dir="ltr">
                    <div>
                      <div dir="ltr">
                        <div>
                          <div dir="ltr"><br>
                          </div>
                          <div dir="ltr"><br>
                          </div>
                          <div dir="ltr"><br>
                          </div>
                          <div dir="ltr">Ramin Zaghi</div>
                          <div dir="ltr"><br>
                            <div><img
src="https://drive.google.com/uc?id=0B3xiwa7kf1uwRlYxenBFM1o2NEk&amp;export=download"
                                moz-do-not-send="true"><b>Mosaic3DX™ | <font
                                  size="1">User Interface Technology</font></b></div>
                            <div>
                              <div style="font-size:small">St John's
                                Innovation Centre,</div>
                              <div style="font-size:small">Cowley Road,</div>
                              <div style="font-size:small">Cambridge,</div>
                              <div style="font-size:small">CB4 0WS, UK<b><br>
                                </b></div>
                              <div style="font-size:small"><b>E</b>:<b> </b><a
                                  href="mailto:[email protected]"
                                  target="_blank" moz-do-not-send="true">[email protected]</a></div>
                              <div style="font-size:small"><b>T</b>: +44
                                1223 421 311</div>
                            </div>
                            <div><font size="2"><a
                                  href="http://linkedin.com/in/raminzaghi"
                                  target="_blank" moz-do-not-send="true">http://linkedin.com/in/raminzaghi</a></font></div>
                            <div><font size="2"><br>
                              </font></div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </blockquote>
      <p><br>
      </p>
    </blockquote>
    <p><br>
    </p>
  </body>
</html>

--------------AC64A92F9E69F75AF5DF9B30--

--===============0203739022465275300==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: inline

X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KTW9uby1kZXZl
bC1saXN0IG1haWxpbmcgbGlzdApNb25vLWRldmVsLWxpc3RAbGlzdHMuZG90Lm5ldApodHRwOi8v
bGlzdHMuZG90Lm5ldC9tYWlsbWFuL2xpc3RpbmZvL21vbm8tZGV2ZWwtbGlzdAo=

--===============0203739022465275300==--