本文介绍了DLLImport c ++函数具有char *输入和输出参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于使用DllImport的某些特定问题的文章很多.las,我经常看到对同一问题的不同答复.例如,有人说如果c ++函数返回aa char *和int * strLen,那么有人说我应该在我的dllImport语句中使用StringBuilder,而其他人则说return byte [],有些人在dllImport中有一个marshall语句,有些人则不这样做.没错由于旧的C#/.net版本,似乎需要一些答案.

There are a lot of articles about some specific problem using DllImport.Alas quite often I see different replies to the same question.For instance some say if a c++ function returns a a char* and an int* strLen, then some people say I should use a StringBuilder in my dllImport statement and others say return byte[], some have a marshall statement in the dllImport, some don't. Some answers seem needed because of old C# / .net versions.

所以问题是:如果从c ++进行的dll调用相当简单,没有奇怪的调用约定或其他奇怪的项目,那么如果您具有输出char *和size或输入char *和size,那么对应的DllImport函数应该是什么?

So the question is: If the dll call from c++ is fairly straightforward, without strange calling conventions, or other strange items, what should the corresponding DllImport functions be if you have output char* and size or input char * and size?

c++ .h
bool SendString(const char* pSendStr, long strSize);
bool ReadString(char* pReadStr, long& readStrSize);

对应的DllImports是什么?用字符串替换instr和outstr?字符串生成器?字符[]?字节[]?是否需要任何元帅声明?

What are the corresponding DllImports? replace the instr and outstr with string? stringbuilder? char[]? byte[]? Is any marshal statement needed?

推荐答案

bool SendString(const char* pSendStr, long strSize);

此功能很简单.文本从呼叫者发送到被呼叫者. p/invoke的声明如下:

This function is the easy one. The text is sent from the caller to the callee. The p/invoke is declared like this:

[DllImport(dllname, CallingConvention = CallingConvention.Cdecl)]
static extern bool SendString(string SendStr, int Len);

请注意,我假设使用cdecl调用约定,因为这是C ++代码的默认设置.还要注意Windows上C ++中的long为32位宽.因此它与C#中的int匹配.

Note that I'm assuming the cdecl calling convention since that is the default for C++ code. And also do note that long in C++ on Windows is 32 bits wide. So it matches int in C#.

调用函数时,您需要传递字符串及其长度.但是,通常的约定是使用以null终止的字符串,因此不需要length参数.我会这样声明非托管函数:

When you call the function you need to pass the string and its length. However, the normal convention is for null-terminated strings to be used so the length parameter is not needed. I'd declare the unmanaged function like this:

bool SendString(const char* pSendStr);

p/这样调用:

[DllImport(dllname, CallingConvention = CallingConvention.Cdecl)]
static extern bool SendString(string SendStr);

另一个功能稍微复杂一点.您已经这样声明了:

The other function is a little more complex. You've declared it like this:

bool ReadString(char* pReadStr, long& readStrSize);

在此,调用方分配由被调用方填充的缓冲区.您可以使用StringBuilder作为文本,并让封送处理程序为您完成工作. p/调用为:

Here the caller allocates the buffer which is populated by the callee. You can use StringBuilder for the text and let the marshaler do the work for you. The p/invoke is:

[DllImport(dllname, CallingConvention = CallingConvention.Cdecl)]
static extern bool ReadString(StringBuilder ReadStr, ref int Len);

约定是您提供所提供缓冲区的长度.反过来,该函数将让您知道它写入了多少个字符.您将这样调用该函数:

The convention is that you supply the length of the provided buffer. In turn the function will let you know how many characters it wrote. You'd call the function like this:

int len = 256;
StringBuilder ReadStr = new StringBuilder(len);
bool succeeded = ReadString(ReadStr, ref len);
if (succeeded)
{
    string str = ReadStr.ToString();
}

这篇关于DLLImport c ++函数具有char *输入和输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 06:14