的P / Invoke传递一个StringBuilder一个功能 [英] P/Invoke a Function Passed a StringBuilder

查看:120
本文介绍了的P / Invoke传递一个StringBuilder一个功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中的文件,我有一个

in a C# file i have a

class Archiver {
    [DllImport("Archiver.dll")]
    public static extern void archive(string data, StringBuilder response);
}

的字符串数据是输入,和StringBuilder响应是其中函数写入东西

string data is an input, and StringBuilder response is where the function writes something

(用C语言编写)归档函数原型是这样的:

the archive function prototype (written in C) looks like this:

void archive(char * dataChr, char * outChr);

和它接收dataChr一个字符串,然后做了

and it receives a string in dataChr, and then does a

strcpy(outChr,"some big text");

从C#我把它称为是这样的:

from C# i call it something like this:

string message = "some text here";
StringBuilder response = new StringBuilder(10000);
Archiver.archive(message,response);

这工作,但这个问题,因为你可能会看到是我给的值到StringBuilder大小,但存档功能可能会给回(的方式),比我给我的StringBuilder规模较大的文本。什么办法来解决这个问题?

this works, but the problem, as you might see is that i give a value to the StringBuilder size, but the archive function might give back a (way) larger text than the size i've given to my StringBuilder. any way to fix this?

推荐答案

您需要写,告诉你有多大的的StringBuilder 必须是一个函数,然后调用函数来初始化的StringBuilder

You need to write a function that tells you how big the StringBuilder needs to be, then call that function to initialize the StringBuilder.

这篇关于的P / Invoke传递一个StringBuilder一个功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆