我在我的一个项目中使用 P/Invoke 来调用 avifil32.dll 方法,到目前为止一切正常。今天,几个月后回到那个项目并同时切换到 Windows 8,我发现没有任何工作了。我对 avifil32 方法的大部分调用都会导致 AccessViolationException(“尝试读取或写入 protected 内存...”)。
造成这种情况的一个函数是 AVIFileOpen,之前我以这种方式调用它(在 pinvoke.net here 上也有说明):
[DllImport("avifil32.dll", PreserveSig=true)]
static extern int AVIFileOpen(out IntPtr ppfile, string szFile, uint mode, int pclsidHandler);
在网上搜索后我找到了this有用的帖子,因此我将调用更改为(注意 AVIFileOpenW):
[DllImport("avifil32.dll", PreserveSig=true, CharSet = CharSet.Auto)]
public static extern int AVIFileOpenW(ref int ppfile, [MarshalAs(UnmanagedType.LPWStr)] String szFile, int uMode, int pclsidHandler);
现在可以了。但是我还有其他问题,例如我在调用 AVIFileCreateStream
那我做错了什么?更有趣的是,这可能是导致现在不再工作的原因?我承认我不知道在此期间会发生什么。巨大的变化已经从 Windows 7 传递到 Windows 8,但这可以解释问题吗?
编辑
根据 Hans Passant 的建议,我已经更正了 P/Invoke 调用,现在我不再有 AccessViolationexception。然而,在我工作之前,这对我来说仍然是一个谜。事实上,现在(Win8)和之前(Win7)我都有 64 位操作系统。 然而,项目设置中还有另一个细节可能有助于解释。这是一个从我的主项目中调用的库项目。我的主要项目以 x64 为目标(不能选择以 x86 为目标),而库以 AnyCPU 为目标。我用这些设置做了一次又一次的试验,我的旧版本 AVIFileOpen 调用程序在 Win7 x64 机器下工作,但在 Win8 x64 机器下不工作。这是否与 JIT 编译器对 AnyCPU 的不同“管理”有关,或者一定是我遗漏了一些其他设置?
现在该程序可以在 x86 和 x64 中运行。下面是我正在使用的代码的摘录:
AVIFileInit();
IntPtr aviFile = IntPtr.Zero;
IntPtr aviStream = IntPtr.Zero;
string tmp = Path.GetTempFileName();
if (AVIFileOpen(out aviFile, tmp.Substring(0, tmp.Length - 4) + ".avi", OF_WRITE | OF_CREATE, IntPtr.Zero) == 0)
{
AVISTREAMINFO strhdr = new AVISTREAMINFO();
strhdr.fccType = mmioStringToFOURCC("vids", 0);
strhdr.fccHandler = mmioStringToFOURCC("CVID", 0);
strhdr.dwScale = 1;
strhdr.dwRate = 25;
strhdr.dwSuggestedBufferSize = 102400;
strhdr.dwQuality = -1;
strhdr.rcFrame.bottom = 320;
strhdr.rcFrame.right = 320;
strhdr.szName = "";
if (AVIFileCreateStream(aviFile, out aviStream, ref strhdr) == 0)
{
AVICOMPRESSOPTIONS_CLASS options = new AVICOMPRESSOPTIONS_CLASS();
options.fccType = (uint)streamtypeVIDEO;
options.lpParms = IntPtr.Zero;
options.lpFormat = IntPtr.Zero;
bool ok = AVISaveOptions(this.Handle, ICMF_CHOOSE_KEYFRAME | ICMF_CHOOSE_DATARATE, 1, ref aviStream, ref options);
}
}
public static readonly int streamtypeVIDEO = mmioFOURCC('v', 'i', 'd', 's');
public const UInt32 ICMF_CHOOSE_KEYFRAME = 0x0001;
public const UInt32 ICMF_CHOOSE_DATARATE = 0x0002;
public const UInt32 ICMF_CHOOSE_PREVIEW = 0x0004;
public const int OF_WRITE = 1;
public const int OF_READWRITE = 2;
public const int OF_CREATE = 4096;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public UInt32 left;
public UInt32 top;
public UInt32 right;
public UInt32 bottom;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct AVISTREAMINFO
{
public Int32 fccType;
public Int32 fccHandler;
public Int32 dwFlags;
public Int32 dwCaps;
public Int16 wPriority;
public Int16 wLanguage;
public Int32 dwScale;
public Int32 dwRate;
public Int32 dwStart;
public Int32 dwLength;
public Int32 dwInitialFrames;
public Int32 dwSuggestedBufferSize;
public Int32 dwQuality;
public Int32 dwSampleSize;
public RECT rcFrame;
public Int32 dwEditCount;
public Int32 dwFormatChangeCount;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)] public String szName;
}
[StructLayout(LayoutKind.Sequential)]
public class AVICOMPRESSOPTIONS_CLASS
{
public UInt32 fccType;
public UInt32 fccHandler;
public UInt32 dwKeyFrameEvery;
public UInt32 dwQuality;
public UInt32 dwBytesPerSecond;
public UInt32 dwFlags;
public IntPtr lpFormat;
public UInt32 cbFormat;
public IntPtr lpParms;
public UInt32 cbParms;
public UInt32 dwInterleaveEvery;
}
[DllImport("avifil32.dll")]
public static extern void AVIFileInit();
[DllImport("winmm.dll", CharSet = CharSet.Auto)]
public static extern int mmioStringToFOURCC([MarshalAs(UnmanagedType.LPWStr)] String sz, int uFlags);
[DllImport("avifil32.dll", PreserveSig = true, CharSet = CharSet.Auto)]
public static extern int AVIFileOpen(out IntPtr ppfile, String szFile, int uMode, IntPtr pclsidHandler);
[DllImport("avifil32.dll", CharSet = CharSet.Auto)]
public static extern int AVIFileCreateStream(IntPtr pfile, out IntPtr ppavi, ref AVISTREAMINFO ptr_streaminfo);
[DllImport("avifil32.dll")]
public static extern bool AVISaveOptions(IntPtr hwnd, UInt32 uiFlags, Int32 nStreams, ref IntPtr ppavi, ref AVICOMPRESSOPTIONS_CLASS plpOptions);
请您参考如下方法:
The big change has been passing from Windows 7 to Windows 8
这可以解释问题,您可能使用的是 64 位版本的 Windows 8。您使用的两个声明都是错误的,并且当您在 64 位模式下运行程序时,将以非常难以诊断的方式出现故障。相关的项目设置是Project + Properties,Build tab,Platform target setting。最好在此处使用“x86”(在 VS2012 及更高版本中勾选首选 32 位),这将减少您因这些错误声明而遭受的痛苦。
第一个参数是 PAVIFILE,“指向接收接口(interface)指针的缓冲区的指针”。最后一个参数是 CLSID,“指向类标识符的指针”。指针在 32 位模式下为 4 个字节,在 64 位模式下为 8 个字节。相应的 C# 声明必须使用 ref 或 out 关键字或使用 IntPtr 才能兼容。
所以第一个参数是“指向指针的指针”,使用 out IntPtr 是正确的。就像你一样。
最后一个参数几乎从未使用过,您传递了一个空指针。在这种情况下,您希望声明它 IntPtr 并在调用时传递 IntPtr.Zero。您最初传递了 0、4 个字节,而函数预期将 8 个字节设置为 0。足以生成错误的指针值并获取 AccessViolationException。
您使用的替换声明特别令人讨厌,您只传递了足够的空间来存储一个 int,4 个字节,但该函数在 64 位模式下写入了 8 个字节。这会破坏堆栈或 GC 堆,非常讨厌。后续调用(如 AviFileCreateStream)确实很可能会失败,因为接口(interface)指针值不太可能原封不动地保留下来。
我修复了 pinvoke.net 声明。
