locked
No return from unmanaged code, methods to catch exception? RRS feed

  • Question

  • I am developing a c# application that needs to calls functions from a c header file. I use

    [System.Runtime.InteropServices.DllImport("xxx.dll", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true,SetLastError=true)]
            public static extern Byte xxxx(IntPtr hResourceContext, ushort xxxx, ref structa aaa,ref Result_struct Result, Byte b);

    to import the dll method. When I call this method in C#, the application simply exits without throwing an exception or error. I've used similar approach for other methods from the same dll which involves passing nested structs as parameter and they work fine. I would like to know how I can debug the code/ catch errors/exceptions from the unmanaged code.

    I tried to include the SetLastError=true during DllImport but it does not help.

    Thanks!

    Friday, September 24, 2010 4:28 PM

Answers

  • Hi,

    Thanks for your post. For debuging the unmanaged code as in your scenario, we need either the source code of that native dll or its debug symbol file. If we have source code, we could include it into the same same solution as the c# application. Then check the "enable unmanaged code debugging" in c# app's Project Properties page -> Debug tab. If we have debug symbol files for the native dll, we could load the symbol file in visual studio and then debug it.

     


    Please mark the right answer at the right time.
    Thanks,
    Sam
    • Marked as answer by SamAgain Tuesday, October 5, 2010 6:51 AM
    Monday, September 27, 2010 3:14 AM
  • Hi,

    Sorry for the late reply. If we don't have either source code or debug symbol file. Please take a look at the eventlog, is anything useful there? Or please place the p/invoke code inside a try/catch block to see if there's anything catched. Or we can get a memory dump according to the folloing this KB article.

    How to use ADPlus to troubleshoot "hangs" and "crashes"


    Please mark the right answer at the right time.
    Thanks,
    Sam
    • Marked as answer by SamAgain Tuesday, October 5, 2010 6:51 AM
    Tuesday, October 5, 2010 6:50 AM

All replies

  • Hi,

    Thanks for your post. For debuging the unmanaged code as in your scenario, we need either the source code of that native dll or its debug symbol file. If we have source code, we could include it into the same same solution as the c# application. Then check the "enable unmanaged code debugging" in c# app's Project Properties page -> Debug tab. If we have debug symbol files for the native dll, we could load the symbol file in visual studio and then debug it.

     


    Please mark the right answer at the right time.
    Thanks,
    Sam
    • Marked as answer by SamAgain Tuesday, October 5, 2010 6:51 AM
    Monday, September 27, 2010 3:14 AM
  • Sam,

     

    Thanks for the reply. If I do not have both the source code and debug symbol file, is there no way to debug it?

    Thanks!

    Monday, September 27, 2010 10:00 PM
  • Hi,

    Sorry for the late reply. If we don't have either source code or debug symbol file. Please take a look at the eventlog, is anything useful there? Or please place the p/invoke code inside a try/catch block to see if there's anything catched. Or we can get a memory dump according to the folloing this KB article.

    How to use ADPlus to troubleshoot "hangs" and "crashes"


    Please mark the right answer at the right time.
    Thanks,
    Sam
    • Marked as answer by SamAgain Tuesday, October 5, 2010 6:51 AM
    Tuesday, October 5, 2010 6:50 AM
  • Sam,

    Thanks again for the reply. Eventlog does not contain anything useful. I placed the pinvoke code inside the try block and still the exception was not caught in the catch block. I installed the ADplus tool and obtained the log. But I'm unsure to find the details of the exception. This is the log I got -

     

    FirstChance_ld_DLL_Load
    Unload module <not found> at 03be0000
    FirstChance_ud_DLL_UnLoad
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    (1ac0.8e0): C++ EH exception - code e06d7363 (first chance)
    FirstChance_eh_CPlusPlusEH

    Current time:
    Debug session time: Mon Oct 11 13:12:27.703 2010 (UTC - 7:00)
    System Uptime: 3 days 16:02:11.808
    Process Uptime: 0 days 0:00:40.703
      Kernel time: 0 days 0:00:00.250
      User time: 0 days 0:00:00.390


    Call stack below ---
    *** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\WINDOWS\system32\KERNEL32.dll -
    *** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\Program Files\Common Files\Agilent\SimStack\AgtRMSimulate.dll -
     # ChildEBP RetAddr  Args to Child             
    WARNING: Stack unwind information not available. Following frames may be wrong.
    00 0012e4ec 03ca57b3 e06d7363 00000001 00000003 KERNEL32!RaiseException+0x52
    01 0012e524 03c95bc5 0012e58c 03cd93f4 96eaa929 AgtRMSimulate!DllUnregisterServer+0x20325
    02 0012e620 7c910222 00000007 001507d8 00150000 AgtRMSimulate!DllUnregisterServer+0x10737
    03 0012e64c 7c910222 7c91019b 7c9101db 03d02a70 ntdll!RtlAllocateHeap+0x15e
    04 0012e650 7c91019b 7c9101db 03d02a70 00000030 ntdll!RtlAllocateHeap+0x15e
    05 0012e654 7c9101db 03d02a70 00000030 00000020 ntdll!RtlAllocateHeap+0xd7
    06 0012e66c 7c910222 7c91019b 7c9101db 7c9100c4 ntdll!RtlAllocateHeap+0x117
    07 0012e670 7c91019b 7c9101db 7c9100c4 0000000c ntdll!RtlAllocateHeap+0x15e
    08 0012e674 7c9101db 7c9100c4 0000000c 00000000 ntdll!RtlAllocateHeap+0xd7
    09 0012e6ac 7c91005d 78583c1b 044c0000 00000000 ntdll!RtlAllocateHeap+0x117
    *** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.4974_x-ww_d889290f\MSVCR90.dll -
    0a 0012e6b0 78583c1b 044c0000 00000000 78583c3a ntdll!RtlFreeHeap+0x130
    0b 0012e6bc 78583c3a 00000002 03d02a70 03d02ba8 MSVCR90!free+0xcd
    0c 00000000 00000000 00000000 00000000 00000000 MSVCR90!free+0xec

    (1ac0.8e0): C++ EH exception - code e06d7363 (first chance)
    FirstChance_eh_CPlusPlusEH

    Current time:
    Debug session time: Mon Oct 11 13:12:27.734 2010 (UTC - 7:00)
    System Uptime: 3 days 16:02:11.830
    Process Uptime: 0 days 0:00:40.735
      Kernel time: 0 days 0:00:00.250
      User time: 0 days 0:00:00.390


    Call stack below ---
     # ChildEBP RetAddr  Args to Child             
    WARNING: Stack unwind information not available. Following frames may be wrong.
    00 0012decc 03ca57b3 e06d7363 00000001 00000003 KERNEL32!RaiseException+0x52
    01 0012df04 03c95bc5 00000000 00000000 03cad487 AgtRMSimulate!DllUnregisterServer+0x20325
    02 0012e620 7c910222 00000007 001507d8 00150000 AgtRMSimulate!DllUnregisterServer+0x10737
    03 0012e64c 7c910222 7c91019b 7c9101db 03d02a70 ntdll!RtlAllocateHeap+0x15e
    04 0012e650 7c91019b 7c9101db 03d02a70 00000030 ntdll!RtlAllocateHeap+0x15e
    05 0012e654 7c9101db 03d02a70 00000030 00000020 ntdll!RtlAllocateHeap+0xd7
    06 0012e66c 7c910222 7c91019b 7c9101db 7c9100c4 ntdll!RtlAllocateHeap+0x117
    07 0012e670 7c91019b 7c9101db 7c9100c4 0000000c ntdll!RtlAllocateHeap+0x15e
    08 0012e674 7c9101db 7c9100c4 0000000c 00000000 ntdll!RtlAllocateHeap+0xd7
    09 0012e6ac 7c91005d 78583c1b 044c0000 00000000 ntdll!RtlAllocateHeap+0x117
    0a 0012e6b0 78583c1b 044c0000 00000000 78583c3a ntdll!RtlFreeHeap+0x130
    0b 0012e6bc 78583c3a 00000002 03d02a70 03d02ba8 MSVCR90!free+0xcd
    0c 00000000 00000000 00000000 00000000 00000000 MSVCR90!free+0xec

    (1ac0.8e0): C++ EH exception - code e06d7363 (first chance)
    FirstChance_eh_CPlusPlusEH

    Current time:
    Debug session time: Mon Oct 11 13:12:27.734 2010 (UTC - 7:00)
    System Uptime: 3 days 16:02:11.838
    Process Uptime: 0 days 0:00:40.735
      Kernel time: 0 days 0:00:00.250
      User time: 0 days 0:00:00.390


    Call stack below ---
     # ChildEBP RetAddr  Args to Child             
    WARNING: Stack unwind information not available. Following frames may be wrong.
    00 0012e8dc 03ca57b3 e06d7363 00000001 00000003 KERNEL32!RaiseException+0x52
    01 0012e914 03c88b7f 0012ea2c 03cd7808 96eaa611 AgtRMSimulate!DllUnregisterServer+0x20325
    02 0012eacc 03c89f5c 0012e90c 0012ed04 0012eca0 AgtRMSimulate!DllUnregisterServer+0x36f1
    *** WARNING: Unable to verify checksum for C:\Program Files\IVI Foundation\VISA\VisaCom\GlobMgr.dll
    *** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\Program Files\IVI Foundation\VISA\VisaCom\GlobMgr.dll -
    03 0012ec1c 635021d8 03d02a78 0012ed04 0012eca0 AgtRMSimulate!DllUnregisterServer+0x4ace
    *** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\clr.dll -
    04 0012ec40 79161f8e 00000000 0012ed2c 001b6fb0 GlobMgr+0x21d8
    05 0012ec44 00000000 0012ed2c 001b6fb0 00163998 clr!CoUninitializeEE+0x3dc6

    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_ld_DLL_Load
    FirstChance_epr_Process_Shut_Down

    Current time:
    Debug session time: Mon Oct 11 13:12:35.078 2010 (UTC - 7:00)
    System Uptime: 3 days 16:02:19.174
    Process Uptime: 0 days 0:00:48.079
      Kernel time: 0 days 0:00:00.296
      User time: 0 days 0:00:00.421


    Call stacks below ---

    .  0  Id: 1ac0.8e0 Suspend: 0 Teb: 7ffdf000 Unfrozen
    *** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\Tools\RFCalibTool\RFCalibTool\bin\Debug\QMSL_MSVC9R.dll -
     # ChildEBP RetAddr  Args to Child             
    WARNING: Stack unwind information not available. Following frames may be wrong.
    00 001264e4 04da354c ffffffff c0000417 00126910 ntdll!KiFastSystemCallRet
    01 00126820 04da358b 00000000 00000000 00000000 QMSL_MSVC9R!QLIB_GetLastEfsErrorInfo+0x6d0ec
    02 00127366 00000000 00000000 00000000 00000000 QMSL_MSVC9R!QLIB_GetLastEfsErrorInfo+0x6d12b

    Creating c:\\20101011_131220_Crash_Mode\FULLDUMP_FirstChance_epr_Process_Shut_Down_RFCalibTool.exe__1360_2010-10-11_13-12-35-109_1ac0.dmp - mini user dump
    Dump successfully written

    Thread Usage Information:
     User Mode Time
      Thread       Time
       0:8e0       0 days 0:00:00.390

     

     

    Thanks!

    Monday, October 11, 2010 8:24 PM