我有一个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);更多推荐
发布评论