I'm using the P/Invoke in C# to call native function from C++ DLL as below:
C++ DLL:
extern "C" { // Function: Create Wmv video from sequences image. Codec: WMV3 (VC-1) __declspec(dllexport) bool __stdcall CreateWMV(...) { ... } }
C# wrapper class. I create the C# wrapper class function to map with native C++ code:
[DllImport("AVIEncoder.dll", EntryPoint = "CreateWMV", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool createWmv(...);
I'm sure that the parameters were marshaling correctly in C#, because it would run successfully when I called directly in client C# code. The issue only occurred when I put the function in background thread.
private void Test()
{
....
createWmv(...); // This call was processed without issue
Thread backgroundThread = new Thread(
new ThreadStart(()=>
{
createWmv(...); // This call causes AccessViolationException
}
}
The function createWmv() uses the Media Foundation Interface to generate Wmv video. I tried to debug and I found out that when I commented out the function IMFSinkWriter::WriteSample() in native code, the program run without causing the exception.
Thus, I wonder whether Microsoft has something weird in SinkWriter implementation. Does anyone have the same issue using Media Foundation this way?