From 6f47d892a3b4fbb299b916e0f0506b2780e7cc70 Mon Sep 17 00:00:00 2001 From: Jetha Chan Date: Fri, 20 May 2016 19:09:06 +0900 Subject: [PATCH] autogenerate different getter code for strings and wide strings --- CodeGenDom/NativeCodeGen.cs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/CodeGenDom/NativeCodeGen.cs b/CodeGenDom/NativeCodeGen.cs index 94becec..9e09cf5 100644 --- a/CodeGenDom/NativeCodeGen.cs +++ b/CodeGenDom/NativeCodeGen.cs @@ -186,8 +186,26 @@ private static void GeneratePropertyDefinition(StringBuilder sb, NativeClassInfo WriteLine(sb, " {0}* instance = reinterpret_cast<{0}*>(instanceId);", classInfo.NativeName); WriteLine(sb, " static {0} localData;", info.NativeType); WriteLine(sb, " localData = instance->Get{0}();", info.NativeName); - WriteLine(sb, " *data = (void*)&localData;"); - WriteLine(sb, " *size = sizeof(localData);"); + + // need special handling for strings and wide strings here, + // or you'll end up trying to marshal the memory address + // instead of the contents into a managed string downstream + switch(info.NativeType) + { + case "wchar_t*": + WriteLine(sb, " *data = (void*)localData;"); + WriteLine(sb, " *size = wcslen(localData);"); + break; + case "char*": + WriteLine(sb, " *data = (void*)localData;"); + WriteLine(sb, " *size = strlen(localData);"); + break; + + default: + WriteLine(sb, " *data = (void*)&localData;"); + WriteLine(sb, " *size = sizeof(localData);"); + break; + } WriteLine(sb, "}}"); } }