gpt4 book ai didi

c# - 以更人性化的方式编码 char**

转载 作者:太空宇宙 更新时间:2023-11-04 11:39:30 25 4
gpt4 key购买 nike

我在 C# 中有以下接口(interface)方法声明:

[ComVisible(true), ComImport, SuppressUnmanagedCodeSecurity,
Guid("A668B8F2-BA87-4F63-9D41-768F7DE9C50E"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ILAVAudioStatus
{
[PreserveSig]
int GetOutputDetails(out IntPtr pOutputFormat, out int pnChannels, out int pSampleRate, out uint pChannelMask);
}

pOutputformat在C++中实际上是一个char**

我需要执行 Marshal.PtrToStringAnsi(pOutputformat) 来获取实际的字符串。

是否有一些编码属性可以将 pOutputformat 直接转换为输出字符串而不是使用 IntPtr?

这是 C++ 方法的作用:

HRESULT CLAVAudio::GetOutputDetails(const char **pOutputFormat, int *pnChannels, int *pSampleRate, DWORD *pChannelMask)
{
if(!m_pOutput || m_pOutput->IsConnected() == FALSE) {
return E_UNEXPECTED;
}
if (m_avBSContext) {
if (pOutputFormat) {
*pOutputFormat = get_sample_format_desc(SampleFormat_Bitstream);
}
return S_FALSE;
}
if (pOutputFormat) {
*pOutputFormat = get_sample_format_desc(m_OutputQueue.sfFormat);
}
if (pnChannels) {
*pnChannels = m_OutputQueue.wChannels;
}
if (pSampleRate) {
*pSampleRate = m_OutputQueue.dwSamplesPerSec;
}
if (pChannelMask) {
*pChannelMask = m_OutputQueue.dwChannelMask;
}
return S_OK;
}

最佳答案

Marshal.PtrToStringAnsi(pOutputformat)

是你能做的最好的。这是无法避免的。如果您尝试编码为 out string OutputFormat,则编码器将在 native 代码返回的指针上调用 CoTaskMemFree。以泪水告终。

问题仍然是,谁负责释放 pOutputformat 指向的内存?只有您自己知道答案。

有人想知道为什么这个 COM 接口(interface)的设计者选择使用 C 字符串而不是 COM BSTR

关于c# - 以更人性化的方式编码 char**,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21890022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com