33using System . IO ;
44using System . Reflection ;
55using System . Runtime . InteropServices ;
6+ using System . Text ;
67using NXPorts . Attributes ;
78
89namespace ClrLoader
@@ -12,6 +13,21 @@ public static class ClrLoader
1213 static bool _initialized = false ;
1314 static List < DomainData > _domains = new List < DomainData > ( ) ;
1415
16+ private static string PtrToStringUtf8 ( IntPtr ptr )
17+ {
18+ if ( ptr == IntPtr . Zero )
19+ return null ;
20+
21+ int len = 0 ;
22+ while ( Marshal . ReadByte ( ptr , len ) != 0 )
23+ len ++ ;
24+
25+ byte [ ] bytes = new byte [ len ] ;
26+ Marshal . Copy ( ptr , bytes , 0 , len ) ;
27+
28+ return Encoding . UTF8 . GetString ( bytes ) ;
29+ }
30+
1531 [ DllExport ( "pyclr_initialize" , CallingConvention . Cdecl ) ]
1632 public static void Initialize ( )
1733 {
@@ -36,11 +52,10 @@ private static string AssemblyDirectory
3652 }
3753
3854 [ DllExport ( "pyclr_create_appdomain" , CallingConvention . Cdecl ) ]
39- public static IntPtr CreateAppDomain (
40- [ MarshalAs ( UnmanagedType . LPUTF8Str ) ] string name ,
41- [ MarshalAs ( UnmanagedType . LPUTF8Str ) ] string configFile
42- )
55+ public static IntPtr CreateAppDomain ( IntPtr namePtr , IntPtr configFilePtr )
4356 {
57+ string name = PtrToStringUtf8 ( namePtr ) ;
58+ string configFile = PtrToStringUtf8 ( configFilePtr ) ;
4459 Print ( $ "Creating AppDomain { name } with { configFile } ") ;
4560
4661 var clrLoaderDir = AssemblyDirectory ;
@@ -68,13 +83,16 @@ public static IntPtr CreateAppDomain(
6883 [ DllExport ( "pyclr_get_function" , CallingConvention . Cdecl ) ]
6984 public static IntPtr GetFunction (
7085 IntPtr domain ,
71- [ MarshalAs ( UnmanagedType . LPUTF8Str ) ] string assemblyPath ,
72- [ MarshalAs ( UnmanagedType . LPUTF8Str ) ] string typeName ,
73- [ MarshalAs ( UnmanagedType . LPUTF8Str ) ] string function
86+ IntPtr assemblyPathPtr ,
87+ IntPtr typeNamePtr ,
88+ IntPtr functionPtr
7489 )
7590 {
7691 try
7792 {
93+ string assemblyPath = PtrToStringUtf8 ( assemblyPathPtr ) ;
94+ string typeName = PtrToStringUtf8 ( typeNamePtr ) ;
95+ string function = PtrToStringUtf8 ( functionPtr ) ;
7896 var domainData = _domains [ ( int ) domain ] ;
7997 Print ( $ "Getting functor for function { function } of type { typeName } in assembly { assemblyPath } ") ;
8098 return domainData . GetFunctor ( assemblyPath , typeName , function ) ;
0 commit comments