为什么在使用debug调用Ada精化程序时C#会退出?

时间:2014-05-27 23:26:21

标签: c# dll ada

我在Ada中使用GPS创建了一个DLL。我动态加载它并从Ada和C ++成功调用它。

但是当我尝试从C#调用它时,程序会在调用Elaboration init时退出。我错过了什么?完全相同的DLL非常高兴从C ++和Ada调用。

编辑:如果我在没有调试的情况下启动程序,它也适用于C#。但是,如果我使用调试器运行它,那么它会在调用ElaborationInit时退出。任何Windows事件日志都没有任何迹象。

如果Ada DLL是纯的,并且我跳过了精化初始化调用,则会正确调用实际的函数DLL,因此它与精化有关。

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

namespace CallingDLLfromCS
{
   class Program
   {
      [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
      public static extern IntPtr LoadLibrary(string dllToLoad);

      [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
      public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

      [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
      public static extern bool FreeLibrary(IntPtr hModule);

      [UnmanagedFunctionPointer(CallingConvention.StdCall)]
      delegate int AdaCallable2_dlgt(int val);
      static AdaCallable2_dlgt fnAdaCallable2 = null;

      [UnmanagedFunctionPointer(CallingConvention.StdCall)]
      delegate void ElaborationInit_dlgt();
      static ElaborationInit_dlgt ElaborationInit = null;

      [UnmanagedFunctionPointer(CallingConvention.StdCall)]
      delegate void AdaFinal_dlgt();
      static AdaFinal_dlgt AdaFinal = null;

      static void Main(string[] args)
      {
         int result;
         bool fail = false;   // assume the best

         IntPtr pDll2 = LoadLibrary("libDllBuiltFromAda.dll");
         if (pDll2 != IntPtr.Zero)
         {
            // Note the @4 is because 4 bytes are passed.  This can be further reduced by the use of a DEF file in the DLL generation.
            IntPtr pAddressOfFunctionToCall = GetProcAddress(pDll2, "AdaCallable@4");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
               fnAdaCallable2 = (AdaCallable2_dlgt)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(AdaCallable2_dlgt));
            }
            else
               fail = true;

            pAddressOfFunctionToCall = GetProcAddress(pDll2, "DllBuiltFromAdainit");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
               ElaborationInit = (ElaborationInit_dlgt)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(ElaborationInit_dlgt));
            }
            else
               fail = true;

            pAddressOfFunctionToCall = GetProcAddress(pDll2, "DllBuiltFromAdafinal");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
               AdaFinal = (AdaFinal_dlgt)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(AdaFinal_dlgt));
            else
               fail = true;

            if (!fail)
            {
               ElaborationInit.Invoke();
//             ^^^^^^^^^^^^^^^^^^^^^^^^^    FAILS HERE
               result = fnAdaCallable2(50);
               Console.WriteLine("Return value is " + result.ToString());
               AdaFinal();
            }
            FreeLibrary(pDll2);
         }
      }
   }
}

这是在调试器中运行时的输出窗口:

...
'CallingDLLfromCS.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CallingDLLfromCS.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread 'vshost.NotifyLoad' (0x1390) has exited with code 0 (0x0).
The thread '<No Name>' (0x1384) has exited with code 0 (0x0).
The thread 'vshost.LoadReference' (0x1388) has exited with code 0 (0x0).
'CallingDLLfromCS.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Documents and Settings\...CallingDLLfromCS\bin\Debug\CallingDLLfromCS.exe', Symbols loaded.
********** ElaborationInit.Invoke() called here *********
The program '[2160] CallingDLLfromCS.vshost.exe: Managed (v4.0.30319)' has exited with code 1 (0x1).

0 个答案:

没有答案