11using System ;
22using System . Runtime . InteropServices ;
33using System . Text ;
4+ using IoTSharp . Data . Taos ;
45
56namespace TDengineDriver
67{
@@ -247,16 +248,14 @@ public static TAOS_BIND BindBinary(String val)
247248 {
248249
249250 TAOS_BIND bind = new TAOS_BIND ( ) ;
250- // IntPtr unmanagedBinary = Marshal.StringToHGlobalAnsi(val);
251- IntPtr c_str = Marshal . StringToCoTaskMemUTF8 ( val ) ;
251+ var strToBytes = val . ToUTF8IntPtr ( ) ;
252252
253- var strToBytes = System . Text . Encoding . UTF8 . GetBytes ( val ) ;
254- int length = strToBytes . Length ;
253+ int length = strToBytes . len ;
255254 IntPtr lenPtr = Marshal . AllocHGlobal ( sizeof ( ulong ) ) ;
256255 Marshal . WriteInt64 ( lenPtr , length ) ;
257256
258257 bind . buffer_type = ( int ) TDengineDataType . TSDB_DATA_TYPE_BINARY ;
259- bind . buffer = c_str ;
258+ bind . buffer = strToBytes . ptr ;
260259 bind . buffer_length = length ;
261260 bind . length = lenPtr ;
262261 bind . is_null = IntPtr . Zero ;
@@ -266,19 +265,16 @@ public static TAOS_BIND BindBinary(String val)
266265 public static TAOS_BIND BindNchar ( String val )
267266 {
268267 TAOS_BIND bind = new TAOS_BIND ( ) ;
269- var strToBytes = System . Text . Encoding . UTF8 . GetBytes ( val ) ;
270- // IntPtr unmanagedNchar = (IntPtr)Marshal.StringToHGlobalAnsi(val);
271- IntPtr c_str = ( IntPtr ) Marshal . StringToCoTaskMemUTF8 ( val ) ;
272-
273268
274- int length = strToBytes . Length ;
275- IntPtr lenPtr = Marshal . AllocHGlobal ( sizeof ( ulong ) ) ;
276- Marshal . WriteInt64 ( lenPtr , length ) ;
269+ // IntPtr unmanagedNchar = (IntPtr)Marshal.StringToHGlobalAnsi(val);
270+ var strToBytes = val . ToUTF8IntPtr ( ) ;
271+ long length = strToBytes . len ;
272+
277273
278274 bind . buffer_type = ( int ) TDengineDataType . TSDB_DATA_TYPE_NCHAR ;
279- bind . buffer = c_str ;
280- bind . buffer_length = length ;
281- bind . length = lenPtr ;
275+ bind . buffer = strToBytes . ptr ;
276+ bind . buffer_length = ( int ) length ;
277+ bind . length = length . ToIntPtr ( ) ;
282278 bind . is_null = IntPtr . Zero ;
283279
284280 return bind ;
@@ -322,10 +318,15 @@ public static void FreeTaosBind(TAOS_BIND[] binds)
322318 {
323319 foreach ( TAOS_BIND bind in binds )
324320 {
321+ #if NET5_0_OR_GREATER
325322 if ( bind . buffer_type == ( int ) TDengineDataType . TSDB_DATA_TYPE_NCHAR || bind . buffer_type == ( int ) TDengineDataType . TSDB_DATA_TYPE_BINARY )
326323 Marshal . FreeCoTaskMem ( bind . buffer ) ;
327324 else
328- Marshal . FreeHGlobal ( bind . buffer ) ;
325+ Marshal . FreeHGlobal ( bind . buffer ) ;
326+ #else
327+ Marshal . FreeHGlobal ( bind . buffer ) ;
328+ #endif
329+
329330 Marshal . FreeHGlobal ( bind . length ) ;
330331 if ( bind . is_null != IntPtr . Zero )
331332 {
0 commit comments