@@ -167,24 +167,31 @@ public override Type CSharpSignatureType(TypePrinterContext ctx)
167167
168168 public override void CSharpMarshalToNative ( CSharpMarshalContext ctx )
169169 {
170+ string param = ctx . Parameter . Name ;
170171 if ( ctx . Parameter . Usage == ParameterUsage . Unknown &&
171172 ! ctx . Parameter . Type . IsReference ( ) &&
173+ ! ( ctx . Parameter . Type is TemplateParameterSubstitutionType ) &&
172174 ctx . MarshalKind != MarshalKind . NativeField &&
173175 ctx . MarshalKind != MarshalKind . VTableReturnValue &&
174176 ctx . MarshalKind != MarshalKind . Variable )
175177 {
176- ctx . Return . Write ( ctx . Parameter . Name ) ;
178+ ctx . Return . Write ( param ) ;
177179 return ;
178180 }
181+
182+ var substitution = Type as TemplateParameterSubstitutionType ;
183+ if ( substitution != null )
184+ param = $ "({ substitution . Replacement } ) (object) { param } ";
185+
179186 if ( Equals ( Context . Options . Encoding , Encoding . ASCII ) )
180187 {
181- ctx . Return . Write ( $ "Marshal.StringToHGlobalAnsi({ ctx . Parameter . Name } )") ;
188+ ctx . Return . Write ( $ "Marshal.StringToHGlobalAnsi({ param } )") ;
182189 return ;
183190 }
184191 if ( Equals ( Context . Options . Encoding , Encoding . Unicode ) ||
185192 Equals ( Context . Options . Encoding , Encoding . BigEndianUnicode ) )
186193 {
187- ctx . Return . Write ( $ "Marshal.StringToHGlobalUni({ ctx . Parameter . Name } )") ;
194+ ctx . Return . Write ( $ "Marshal.StringToHGlobalUni({ param } )") ;
188195 return ;
189196 }
190197 throw new System . NotSupportedException (
@@ -200,7 +207,7 @@ public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
200207 return ;
201208 }
202209
203- Type type = ctx . ReturnType . Type . Desugar ( ) ;
210+ Type type = Type . Desugar ( ) ;
204211 Type pointee = type . GetPointee ( ) . Desugar ( ) ;
205212 var isChar = type . IsPointerToPrimitiveType ( PrimitiveType . Char ) ||
206213 ( pointee . IsPointerToPrimitiveType ( PrimitiveType . Char ) &&
@@ -211,27 +218,31 @@ public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
211218 if ( Equals ( encoding , Encoding . ASCII ) )
212219 encoding = Context . Options . Encoding ;
213220
221+ string returnVarName = ctx . Function != null &&
222+ ctx . Function . ReturnType . Type . Desugar ( ) . IsAddress ( ) ?
223+ $ "(global::System.IntPtr) { ctx . ReturnVarName } " : ctx . ReturnVarName ;
224+
214225 if ( Equals ( encoding , Encoding . ASCII ) )
215226 {
216- ctx . Return . Write ( $ "Marshal.PtrToStringAnsi({ ctx . ReturnVarName } )") ;
227+ ctx . Return . Write ( $ "Marshal.PtrToStringAnsi({ returnVarName } )") ;
217228 return ;
218229 }
219230 if ( Equals ( encoding , Encoding . UTF8 ) )
220231 {
221- ctx . Return . Write ( $ "Marshal.PtrToStringUTF8({ ctx . ReturnVarName } )") ;
232+ ctx . Return . Write ( $ "Marshal.PtrToStringUTF8({ returnVarName } )") ;
222233 return ;
223234 }
224235
225236 // If we reach this, we know the string is Unicode.
226237 if ( isChar || ctx . Context . TargetInfo . WCharWidth == 16 )
227238 {
228- ctx . Return . Write ( $ "Marshal.PtrToStringUni({ ctx . ReturnVarName } )") ;
239+ ctx . Return . Write ( $ "Marshal.PtrToStringUni({ returnVarName } )") ;
229240 return ;
230241 }
231242 // If we reach this, we should have an UTF-32 wide string.
232243 const string encodingName = "System.Text.Encoding.UTF32" ;
233244 ctx . Return . Write ( $@ "CppSharp.Runtime.Helpers.MarshalEncodedString({
234- ctx . ReturnVarName } , { encodingName } )" ) ;
245+ returnVarName } , { encodingName } )" ) ;
235246 }
236247 }
237248
0 commit comments