开发者

Calling unmanaged C++ library (dll) from C# creates an access violation error (0xc0000005)

开发者 https://www.devze.com 2023-03-15 09:27 出处:网络
Sorry for the long question. I just wanted to include everything that I know about the problem at the moment.

Sorry for the long question. I just wanted to include everything that I know about the problem at the moment.

I'm using Visual Studio 2008 to create a Windows Form program in C# that calls a library written in C++.

The C++ DLL analyzes measurement data, which consists of several samples. The samples are passed to the library by a function (PDSetWaveSample) which takes a struct as a parameter.

After all samples are set, the measurement data is processed with another function (PDPreProcess) which also takes a struct as a parameter.

The C++ DLL is holding all the measurement data in its internal data structures and does all the processing to obtain results.

Everything seems to function correctly until the program starts processing the measurement data. At that point the program crashes and exits with this error:

The program 'XXX.vshost.exe: Managed' has exited with code -1073741819 (0xc0000005).

I’ll add some more debugging information and the C# source code later in this question.

Do you have any hints how to go forward or what probably could cause this?

I am running Windows XP Professional, Version 2002 SP 3.

When the program crashes, it first gives this error message

An unhandled exception of type 'System.ExecutionEngineException' occurred in Unknown Module.

and the possibility to Break or Continue. I choose to Break.

After that I look at the assembly:

No symbols are loaded for any call stack frame. The source code cannot be displayed.

According to the debugger, the assembly at the point where the error happens looks like this:

7C90E4FA  call        7C90E528 
7C90E4FF  mov         eax,dword ptr [esp] 
7C90E502  mov         esp,ebp 
7C90E504  pop         ebp  
7C90E505  ret              
7C90E506  lea         esp,[esp] 
7C90E50D  lea         ecx,[ecx] 
7C90E510  mov         edx,esp 
7C90E512  sysenter         
==>7C90E514  ret              
7C90E515  lea         esp,[esp] 
7C90E51C  lea         esp,[esp] 
7C90E520  lea         edx,[esp+8] 
7C90E524  int         2Eh  
7C90E526  ret              
7C90E527  nop              
7C90E528  push        ebp  
7C90E529  mov         ebp,esp 
7C90E52B  pushfd           

The execution stops at 7C90E514 ret (which I marked with ==>)

The output window shows the following messages:

'CalculationForm.exe': Loaded 'C:\CalculationForm\CalculationForm\bin\Debug\CalculationForm.exe', No native symbols in symbol file.  
'CalculationForm.exe': Loaded 'C:\WINNT\system32\ntdll.dll'  
'CalculationForm.exe': Loaded 'C:\WINNT\system32\kernel32.dll'  
'CalculationForm.exe': Loaded 'C:\WINNT\system32\sysfer.dll'  
'CalculationForm.exe': Loaded 'C:\WINNT\system32\mscoree.dll'  
'CalculationForm.exe': Loaded 'C:\WINNT\system32\advapi32.dll'  
'CalculationForm.exe': Loaded 'C:\WINNT\system32\rpcrt4.dll'  
'CalculationForm.exe': Loaded 'C:\WINNT\system32\secur32.dll'  
'CalculationForm.exe': Loaded 'C:\WINNT\system32\shlwapi.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\gdi32.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\user32.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\msvcrt.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\imm32.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.4053_x-ww_e6967989\msvcr80.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\shell32.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.6028_x-ww_61e65202\comctl32.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\comctl32.dll'
'CalculationForm.exe': Loaded 'C:\WINNT\Microsoft.NET\Framework\v2.0.50727\Culture.dll' 
'CalculationForm.exe': Unloaded 'C:\WINNT\Microsoft.NET\Framework\v2.0.50727\Culture.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\assembly\NativeImages_v2.0.50727_32\mscorlib\17179b71d7680399c00ce88ddc310209\mscorlib.ni.dll' 
'CalculationForm.exe' (Managed): Loaded 'C:\WINNT\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\ole32.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\MSCTF.dll' 
'CalculationForm.exe' (Managed): Loaded 'C:\Tyohakemisto\WaveRoller\CalculationForm\CalculationForm\bin\Debug\CalculationForm.exe', Symbols loaded. 
'CalculationForm.exe': Loaded 'C:\WINNT\Microsoft.NET\Framework\v2.0.50727\mscorjit.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\assembly\NativeImages_v2.0.50727_32\System\2f867d97fb1a34e4d6985780631574bb\System.ni.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\assembly\NativeImages_v2.0.50727_32\System.Drawing\f78f03ba3b1b21e3b26369402c117d33\System.Drawing.ni.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\assembly\NativeImages_v2.0.50727_32\System.Windows.Forms\06b728c1e4ea291d4febee5ca33db4b4\System.Windows.Forms.ni.dll' 
'CalculationForm.exe' (Managed): Loaded 'C:\WINNT\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CalculationForm.exe' (Managed): Loaded 'C:\WINNT\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CalculationForm.exe' (Managed): Loaded 'C:\WINNT\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CalculationForm.exe': Loaded 'C:\WINNT\assembly\NativeImages_v2.0.50727_32\System.Configuration\b2f3e4c45a980474c2dd08166038d645\System.Configuration.ni.dll' 
'CalculationForm.exe' (Managed): Loaded 'C:\WINNT\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CalculationForm.exe': Loaded 'C:\WINNT\assembly\NativeImages_v2.0.50727_32\System.Xml\8ee82dfeff03ca87492149cdcbfc3f21\System.Xml.ni.dll' 
'CalculationForm.exe' (Managed): Loaded 'C:\WINNT\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CalculationForm.exe': Loaded 'ImageAtBase0x10000000'
'CalculationForm.exe': Unloaded 'ImageAtBase0x10000000' 
'CalculationForm.exe': Loaded 'C:\WINNT\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll' 
'CalculationForm.exe' (Managed): Loaded 'C:\WINNT\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll' 
'CalculationForm.exe': Loaded 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\Packages\Debugger\x86\Microsoft.VisualStudio.Debugger.Runtime.Impl.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.4148_x-ww_d495ac4e\msvcr90.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\uxtheme.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\xpsp2res.dll', Binary was not built with debug information. 
'CalculationForm.exe' (Managed): Loaded 'C:\CalculationForm\CalculationForm\bin\Debug\AxInterop.PDCOMMXLib.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\WinSxS\x86_Microsoft.Windows.GdiPlus_6595b64144ccf1df_1.0.6002.22509_x-ww_c7dad023\GdiPlus.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\version.dll' 
'CalculationForm.exe': Unloaded 'C:\WINNT\system32\version.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\MSCTFIME.IME' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\oleaut32.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\Microsoft.NET\Framework\v2.0.50727\diasymreader.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\clbcatq.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\comres.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\version.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\msi.dll' 
'CalculationForm.exe': Loaded 'C:\Nortek\PdCommX\PdCommX.dll', Binary was not built with debug information. 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\mfc100.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\msvcr100.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\msimg32.dll'
'CalculationForm.exe': Loaded 'C:\WINNT\system32\msvcp100.dll' 
'CalculationForm.exe': Loaded 'C:\WINNT\system32\sxs.dll' 
'CalculationForm.exe' (Managed): Loaded 'C:\ CalculationForm\CalculationForm\bin\Debug\Interop.PDCOMMXLib.dll'
'CalculationForm.exe': Loaded 'C:\WINNT\assembly\NativeImages_v2.0.50727_32\Accessibility\775f9f0da40c277eb7d460084858a2ac\Accessibility.ni.dll'
'CalculationForm.exe' (Managed): Loaded 'C:\WINNT\assembly\GAC_MSIL\Accessibility\2.0.0.0__b03f5f7f11d50a3a\Accessibility.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread 'Win32 Thread' (0x1f08) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x1cc4) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x1598) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0xaa8) has exited with code 0 (0x0). 
The thread 'Win32 Thread' (0x1db8) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x1e74) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x1958) has exited with code 0 (0x0).
The program '[4656] CalculationForm.exe: Managed' has exited with code 0 (0x0).
The program '[4656] CalculationForm.exe: Native' has exited with code 0 (0x0).

When program ends, the call stack is:

ntdll.dll!7c90e514()    
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll] 
ntdll.dll!7c90df5a()    
kernel32.dll!7c8025db()     
mscorwks.dll!79e8c639()     
mscorwks.dll!79e8c56f()     
mscorwks.dll!79f7466d()     
mscorwks.dll!7a105cf1()     
mscorwks.dll!7a105e37()     
mscorwks.dll!7a1061db()     
mscorwks.dll!7a09c314()     
mscorwks.dll!79f504da()     
mscorwks.dll!79ef4755()     
ntdll.dll!7c9032a8()    
ntdll.dll!7c90327a()    
ntdll.dll!7c92a8c3()    
ntdll.dll!7c90e48a()    
ntdll.dll!7c94b721()    
ntdll.dll!7c919f68()    
ntdll.dll!7c919f68()    
ntdll.dll!7c919f68()    
ntdll.dll!7c919f68()    
ntdll.dll!7c910385()    
ntdll.dll!7c917c51()    
ntdll.dll!7c917e8f()    
mscorwks.dll!79e71b4c()     
mscorwks.dll!79e821f9()     
mscorwks.dll!79e96571()     
mscorwks.dll!79e965a4()     
mscorwks.dll!79e965c2()     
mscorwks.dll!79ecca87()     
mscorwks.dll!79ef531b()     
mscorwks.dll!79ef6dd8()     
mscorwks.dll!79ef6e76()     
mscorwks.dll!79ef4755()     
ntdll.dll!7c9032a8()    
ntdll.dll!7c90327a()    
ntdll.dll!7c92a8c3()    
ntdll.dll!7c90e48a()    
ntdll.dll!7c917ca7()    
ntdll.dll!7c917e8f()    
mscorwks.dll!7a0ecb29()     
mscorwks.dll!79f6879a()     
mscorwks.dll!79f68780()     
mscorwks.dll!79f73f3d()     
mscorwks.dll!79ecd659()     
mscorwks.dll!79e71b4c()     
mscorwks.dll!79e821f9()     
mscorwks.dll!79e96571()     
mscorwks.dll!79e965a4()     
mscorwks.dll!79e965c2()     
mscorwks.dll!79f87ad3()     
mscorwks.dll!79f87c32()     
mscorlib.ni.dll!792d5428开发者_C百科()  
mscorlib.ni.dll!792d51d6()  
mscorlib.ni.dll!792d50be()  
mscorwks.dll!79e71b4c()     
mscorwks.dll!79e821f9()     
mscorwks.dll!79e96571()     
mscorwks.dll!79e965a4()     
mscorwks.dll!79f29e09()     
mscorwks.dll!79f2a1e0()     
mscorwks.dll!79f2a130()     
mscorwks.dll!79f29837()     
mscorwks.dll!79f298bc()     
mscorwks.dll!79f29967()     
System.ni.dll!7a574b73()    
System.Windows.Forms.ni.dll!7b1c87be()  
mscorwks.dll!79e71b4c()     
mscorwks.dll!79e821f9()     
System.Windows.Forms.ni.dll!7b1c86a0()  
System.Windows.Forms.ni.dll!7b1c8621()  
System.Windows.Forms.ni.dll!7b6fa167()  
mscorwks.dll!79e71b4c()     
mscorwks.dll!79e821f9()     
mscorwks.dll!79fc1fe2()     
mscorwks.dll!79fc219a()     
mscorwks.dll!79fc22be()     
mscorwks.dll!79f077ad()     
mscorwks.dll!79e8c4ec()     
mscorwks.dll!79e8840b()     
mscorwks.dll!79f13cb5()     
mscorwks.dll!79f1129c()     
mscorwks.dll!79f07e17()     
mscoree.dll!7900b77b()  
mscoree.dll!7900b73d()  
mscoree.dll!79004de3()  
kernel32.dll!7c817077()     

I have created a wrapper class for the C++ DLL. The wrapper class is based on the header file for the library:

#ifndef _PDWAVEAPI_H__  
#define _PDWAVEAPI_H__

#ifdef PDWAVE_EXPORTS
#define PDWAVE_API __declspec(dllexport)
#else
#define PDWAVE_API __declspec(dllimport)
#endif

typedef struct {
   bool  bValid;
   float fPressure;
   float fDistance;
   float fVel[4];
   unsigned short nAmp[4];
 } PDWaveSample;

typedef struct {
    float fST[4096];
    float fWinFloor;
    float fWinCeil;
    bool bUseWindow;
    bool bSTOk;
    bool bGetRawAST;
    bool bValidBurst;
 } PDWaveBurst;


PDWAVE_API int __stdcall PDSetWaveSample(PDWaveSample *pWaveSample);
PDWAVE_API int __stdcall PDPreProcess(int nSample, PDWaveBurst *pWaveBurst);

and the C# wrapper class is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices; // DllImport

namespace Cpp_LibraryApi
{
    [StructLayout(LayoutKind.Sequential)]
    public struct PDWaveSample
    {
        public bool bValid;                             
        public float fPressure;                         
        public float fDistance;                         
        public float[] fVel;
        public ushort[] nAmp

        // Constructor to initialize tables

        public static PDWaveSample Create()
        {
            PDWaveSample DataStruct = new PDWaveSample();
            DataStruct.fVel = new float[4];
            DataStruct.nAmp = new ushort[4];
            return DataStruct;
        }
    }
    [StructLayout(LayoutKind.Sequential)]
    public  struct PDWaveBurst {
        public float[] fST;
        public float fWinFloor;
        public float fWinCeil;
        public bool bUseWindow;
        public bool bSTOk;
        public bool bGetRawAST;
        public bool bValidBurst;

        public static PDWaveBurst Create()
        {
            PDWaveBurst DataStruct = new PDWaveBurst();
            DataStruct.fST = new float[Constants.PD_MAX_WAVEMEAS_AST];
            return DataStruct;
        }
    }

    public class Cpp_LibraryWrapper
    {
        [DllImport("cpp_library.dll")]
        public static extern int PDSetWaveSample(ref PDWaveSample pWaveSample);

        [DllImport("cpp_library.dll")]
        public static extern int PDPreProcess(int nSample, ref PDWaveBurst pWaveBurst);

    }
}

The Windows Forms application is using these functions in the following way:

I have simplified the function a little bit. Actually the measurements are read from a serial line using a third party ActiveX component.

using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Timers;
using System.Diagnostics;

namespace CalculationForm
{
    public partial class frmCalculationForm : Form
    {
        int nBurstSamples = 0;

        public frmCalculationForm()
        {
            InitializeComponent();
        }

        private void OnNewData_Event(object sender, Events_OnNewDataEvent e)
        {
            try
            {
                Cpp_LibraryApi.PDWaveSample WaveSampleData = Cpp_LibraryApi.PDWaveSample.Create();

                ReadWaveSample(ref WaveSampleData);
                SetWaveSample(WaveSampleData);

                if (++nBurstSamples > 512)
                {
                    // Process
                    ProcessBurstData(nBurstSamples);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message.ToString());
            }
        }

        public void ReadWaveSample(ref Cpp_LibraryApi.PDWaveSample WaveSampleData)
        {
            float[] dVel = new float[4] { (float)-3.26, (float)-2.808, (float)-3.651, (float)-3.43 };
            ushort[] usAmp = new ushort[4] { 41, 40, 0, 0 };
            WaveSampleData.bValid = true;
            WaveSampleData.fPressure = 12.432;
            WaveSampleData.fDistance = 2.64;
            WaveSampleData.fVel = dVel;
            WaveSampleData.nAmp = usAmp;
        }

        public void SetWaveSample(Cpp_LibraryApi.PDWaveSample WaveSampleData)
        {
            Cpp_LibraryWrapper.PDSetWaveSample(ref WaveSampleData);
        }

        public void ProcessBurstData(int nSamples)
        {
            try
            {
                Cpp_LibraryApi.PDWaveBurst WaveBurstData = Cpp_LibraryApi.PDWaveBurst.Create();

                WaveBurstData.fST = new float[4096];
                WaveBurstData.fWinFloor = (float)1.25;
                WaveBurstData.fWinCeil = 2;
                WaveBurstData.bUseWindow = false;
                WaveBurstData.bSTOk = false;
                WaveBurstData.bGetRawAST = false;
                WaveBurstData.bValidBurst = false;

                Cpp_LibraryWrapper.PDPreProcess(nSamples, ref WaveBurstData);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message.ToString());
            }
        }
    }
}

Everything seems to go well until the program reaches the measurement processing phase. When

Cpp_LibraryWrapper.PDPreProcess(nSamples, ref WaveBurstData);

is called, the execution stops with this error:

The program 'XXX.vshost.exe: Managed' has exited with code -1073741819 (0xc0000005).

I have set these debugging options:

  • Enable unmanaged code debugging
  • Enable the Visual Studio hosting process
  • Common Language Runtime Exceptions “Thrown”
  • Win32 Exceptions -> Access violation “Thrown”


Your structs are marshalled incorrectly because you didn't declare the arrays quite right. You need to tell the marshaller that they are fixed length arrays.

EDIT

In my original answer I missed the addition error that the bool members were not marshalled correctly. The default marshalling is for the 4 byte Windows BOOL but you need 1 byte C++ bool. The code below now handles that correctly. Sorry for the confusion.

public struct PDWaveSample
{
    [MarshalAs(UnmanagedType.I1)]
    public bool bValid;                             
    public float fPressure;                         
    public float fDistance;                         
    [MarshalAs(UnmanagedType.LPArray, SizeConst=4)]
    public float[] fVel;
    [MarshalAs(UnmanagedType.LPArray, SizeConst=4)]
    public ushort[] nAmp
}

public struct PDWaveBurst {
    [MarshalAs(UnmanagedType.LPArray, SizeConst=4096)]
    public float[] fST;
    public float fWinFloor;
    public float fWinCeil;
    [MarshalAs(UnmanagedType.I1)]
    public bool bUseWindow;
    [MarshalAs(UnmanagedType.I1)]
    public bool bSTOk;
    [MarshalAs(UnmanagedType.I1)]
    public bool bGetRawAST;
    [MarshalAs(UnmanagedType.I1)]
    public bool bValidBurst;
}


Actually, I had to marshall the arrays as ByValArray. Otherwise the running environment had something to complain about:

"A first chance exception of type 'System.TypeLoadException' occurred in CalculationForm.exe

Additional information: Cannot marshal field 'fVel' of type 'PdWaveApi.PDWaveSample': Invalid managed/unmanaged type combination (Arrays fields must be paired with ByValArray or SafeArray)."

So, I changed the struct to this:

public struct PDWaveSample
{
    [MarshalAs(UnmanagedType.I1)]
    public bool bValid;                             
    public float fPressure;                         
    public float fDistance;                         
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
    public float[] fVel;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
    public ushort[] nAmp
}

public struct PDWaveBurst {
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=4096)]
    public float[] fST;
    public float fWinFloor;
    public float fWinCeil;
    [MarshalAs(UnmanagedType.I1)]
    public bool bUseWindow;
    [MarshalAs(UnmanagedType.I1)]
    public bool bSTOk;
    [MarshalAs(UnmanagedType.I1)]
    public bool bGetRawAST;
    [MarshalAs(UnmanagedType.I1)]
    public bool bValidBurst;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消