1+ using System ;
12using System . Collections . Generic ;
3+ using System . Runtime . InteropServices ;
24using Silk . NET . Core . Native ;
35using Xunit ;
46
@@ -14,6 +16,32 @@ public class TestSilkMarshal
1416 NativeStringEncoding . LPUTF8Str ,
1517 NativeStringEncoding . LPWStr ,
1618 } ;
19+
20+ [ Fact ]
21+ public unsafe void TestEncodingLPWStr ( )
22+ {
23+ var input = "Hello world" ;
24+
25+ // LPWStr is 2 bytes on Windows, 4 bytes elsewhere (usually)
26+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
27+ {
28+ var pointer = SilkMarshal . StringToPtr ( input , NativeStringEncoding . LPWStr ) ;
29+
30+ Assert . Equal ( input . Length , ( int ) SilkMarshal . StringLength ( pointer , NativeStringEncoding . LPWStr ) ) ;
31+
32+ // Use short for comparison
33+ Assert . Equal ( new short [ ] { 0x48 , 0x65 , 0x6c , 0x6c , 0x6f , 0x20 , 0x77 , 0x6f , 0x72 , 0x6c , 0x64 } , new Span < short > ( ( void * ) pointer , input . Length ) ) ;
34+ }
35+ else
36+ {
37+ var pointer = SilkMarshal . StringToPtr ( input , NativeStringEncoding . LPWStr ) ;
38+
39+ Assert . Equal ( input . Length , ( int ) SilkMarshal . StringLength ( pointer , NativeStringEncoding . LPWStr ) ) ;
40+
41+ // Use int for comparison
42+ Assert . Equal ( new int [ ] { 0x48 , 0x65 , 0x6c , 0x6c , 0x6f , 0x20 , 0x77 , 0x6f , 0x72 , 0x6c , 0x64 } , new Span < int > ( ( void * ) pointer , input . Length ) ) ;
43+ }
44+ }
1745
1846 [ Fact ]
1947 public void TestEncodingString ( )
0 commit comments