我有一个包含两个项目的解决方案:一个C#控制台应用程序和一个C库。C库具有一个返回HRESULT的函数。我需要以某种方式更改此函数以使其返回字符串到我的C#代码。它应该是这样的:
C#:
[DllImport("MyLib.dll", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern long MyFunction(bunch of params, [MarshalAs(UnmanagedType.BStr)] out string text);
Run Code Online (Sandbox Code Playgroud)
C:
extern "C" HRESULT __declspec(dllexport) MyFunction(bunch of params, BSTR* text)
{
PWSTR finalResult;
//lots of code
(*text) = SysAllocString(finalResult);
//cleanup code
}
Run Code Online (Sandbox Code Playgroud)
我可以更改两个项目。但是,无法知道字符串的大小。因此,我尝试在C库中分配字符串,但这会导致访问冲突异常和各种问题。解决这个问题的最佳方法是什么?
归档时间: |
|
查看次数: |
782 次 |
最近记录: |