2013-06-24 120 views
0

我在Visual Studio 2012中使用C#调用包含在我的项目需要的一组外部库中的函数。该函数需要传入一个双指针,但我不确定确切的语法。单指针对我很好。我正在使用不安全的关键字。如何使用指针指向c#中的指针?

AVFormatContext _file = new AVFormatContext(); 

fixed (AVFormatContext* p_file = &_file) 
{ 
    avformat_alloc_output_context2(&p_file,null,null,filename); 
} 

VS抱怨与错误的“& p_file”语法“不能拿一个只读的局部变量的地址”。

任何帮助将不胜感激!

+0

尝试使用'ref'参数声明'avformat_alloc_output_context2'而不是使用'&'。 –

+0

这也行不通。错误状态:无法传递'p_file'作为ref或out参数,因为它是'固定变量' – zjacobs

+0

我认为您没有发布足够的代码。 – Brian

回答

6

由于p_file在固定块内是只读的,所以不能取地址p_file。如果你可以采取其地址那么这将是可能的:

fixed (AVFormatContext* p_file = &_file) 
{ 
    AVFormatContext** ppf = &p_file; 
    *ppf = null; // Just changed the contents of a read-only variable! 

因此你必须采取的东西你的地址可以变化:

fixed (AVFormatContext* p_file = &_file) 
{ 
    AVFormatContext* pf = p_file; 
    AVFormatContext** ppf = &pf; 

而现在我们都好;更改*ppf不会更改p_file

+0

这有效......谢谢! – zjacobs

+0

@ user91986:不客气! –