Re: [程式] Unity scripting (native plugin)

楼主: riveranb (River)   2017-12-26 09:37:38
※ 引述《riveranb (River)》之铭言:
: 标题: [程式] Unity scripting (native plugin)
:
: 以下是我的 sample codes
:
: ==== C++ native plugin部分 ====
: extern "c" declspec(dllexport) void cppfunc( char * tostring, int maxlen)
: {
: std::string source = .... // get texts from opened file
: if(source.length() < maxlen)
: {
: strcpy(tostring, source.c_str());
: }
: }
:
: ==== Unity C# script部分 ====
: [DllImport ("CppPlugin")]
: static extern void cppfunc(StringBuilder tostring, int maxlen);
:
: ......
:
:
: {
: {
: StringBuilder thestring = new StringBuilder(_maxlen); // maxlen = 64
: StringBuilder thestring = new StringBuilder(_maxlen); // maxlen = 64
: while( /** if more in file **/ )
: while( /** if more in file **/ )
: {
: #if METHOD1
: thestring = new StringBuilder(_maxlen); // method 1, always correct
: #else if METHOD2
: thestring.Length = 0; // method 2, get wrong strings after several calls
: #endif
: CppInterop.cppfunc(thestring, _maxlen);
: }
: }
:
:
作者: cjcat2266 (CJ Cat)   2017-12-26 12:46:00
嗯,是我的话也会用byte[],找不太到StringBuilder传到之间的转换,让我感觉毛毛的,到底是怎么把内部buffer传过去不是很明白,如果有其他人找到资料拜托分享一下另外,需要同时设定Length和Capacity这样好像没有治本底层应该还是会重新配置新的buffer内存还有,不更动StringBuilder会维持初次结果也很神祕可做个实验,在C++把char*位址、初始值和结果值印出来
作者: wulouise (在线上!=在电脑前)   2017-12-30 10:32:00
找到的资料只有default-marshaling-for-stringsStrinbuilder本身就是一个buffer 所以给buffer之前必须设定好她的capacityhttps://goo.gl/wxHzs2 根据这边说法 SB只有在放不下新资料的时候才会reallocate memory XDDD过unmanaged code的话她一定不知道建议你用新的sb 或是参考 https://goo.gl/7Tp3Qm

Links booklink

Contact Us: admin [ a t ] ucptt.com