从包含“\ 0”字符的非托管C ++ DLL返回LPTSTR(returning a LPTSTR from an unmanaged C++ DLL that contains '\0

编程入门 行业动态 更新时间:2024-10-27 05:34:08
从包含“\ 0”字符的非托管C ++ DLL返回LPTSTR(returning a LPTSTR from an unmanaged C++ DLL that contains '\0' characters)

我有一个C#gui调用一个非托管的C ++ DLL。 回调用于dll - > gui messaging。

在dll中创建一个可能包含'\ 0'字符的LPTSTR。 该字符串需要通过回调参数传递回gui,完全通过回调。

不幸的是,我只能将字符串传递给gui,直到null字符。 似乎编组切断了弦乐。

// C# callback declarations public delegate bool callbackDelegate(int iEvent, [MarshalAs(UnmanagedType.LPWStr)] string SomeString); private callbackDelegate callbackDelegateInstance; // instantiating and calling the callback in C# callbackDelegateInstance = new callbackDelegate(CallbackHandler); DLLCallbackFunction(callbackDelegateInstance); // C# callback handler private bool CallbackHandler(int iEvent, [MarshalAs(UnmanagedType.LPWStr)] string SomeString) { // SomeString only contains characters up until the null char }

有没有办法返回整个字符串,包括来自dll的空字符?

我有权访问dll和gui代码。

I have a C# gui calling an unmanaged C++ dll. callbacks are used for dll -> gui messaging.

in the dll a LPTSTR is created that may contain '\0' characters. that string needs to be passed back via a callback parameter to the gui, in full, via the callback.

unfortunately i can only get the string passed to the gui up until the null character. seems the marshaling cuts the string.

// C# callback declarations public delegate bool callbackDelegate(int iEvent, [MarshalAs(UnmanagedType.LPWStr)] string SomeString); private callbackDelegate callbackDelegateInstance; // instantiating and calling the callback in C# callbackDelegateInstance = new callbackDelegate(CallbackHandler); DLLCallbackFunction(callbackDelegateInstance); // C# callback handler private bool CallbackHandler(int iEvent, [MarshalAs(UnmanagedType.LPWStr)] string SomeString) { // SomeString only contains characters up until the null char }

Is there a way to return the entire string, including null chars from the dll?

I do have access to dll and gui code.

最满意答案

您可以将其编组为原始字节数组而不是LPTSTR。 如果您的数据不是常量,则必须添加额外的长度参数。

委托将被声明如下:

public delegate bool callbackDelegate(int iEvent, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] byte[] data, int dataCount);

在非管理方面:

typedef void (__stdcall *callbackDelegate)(int iEvent, const char* data, int size);

You could marshal it as a raw byte array instead of an LPTSTR. If your data is not a constant size, you will have to add an additional length parameter.

The delegate would be declared something like this:

public delegate bool callbackDelegate(int iEvent, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] byte[] data, int dataCount);

And on the unmanaged side:

typedef void (__stdcall *callbackDelegate)(int iEvent, const char* data, int size);

更多推荐

本文发布于:2023-07-15 18:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117482.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   DLL   LPTSTR   characters   unmanaged

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!