© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
49 replies
BattleFrogue

✅ Debugging exceptions not caught by try-catch

Hi, I have a small piece of code I am having trouble debugging. I have this class:
public static partial class Library
{   
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    private delegate void PfnVkCreateInstance(IntPtr pCreateInfo, IntPtr pAllocator, out IntPtr pInstance);
    private static PfnVkCreateInstance CreateInstanceInternal;
    
    public static void LoadLibrary()
    {
        IntPtr createInstanceFp = GetInstanceProcAddr(IntPtr.Zero, "vkCreateInstance");

        try
        {
            CreateInstanceInternal = Marshal.GetDelegateForFunctionPointer<PfnVkCreateInstance>(createInstanceFp);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

    [LibraryImport("vulkan-1.dll", EntryPoint = "vkGetInstanceProcAddr", StringMarshalling = StringMarshalling.Utf8)]
    private static partial int GetInstanceProcAddr(IntPtr module, string name);
}   
public static partial class Library
{   
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    private delegate void PfnVkCreateInstance(IntPtr pCreateInfo, IntPtr pAllocator, out IntPtr pInstance);
    private static PfnVkCreateInstance CreateInstanceInternal;
    
    public static void LoadLibrary()
    {
        IntPtr createInstanceFp = GetInstanceProcAddr(IntPtr.Zero, "vkCreateInstance");

        try
        {
            CreateInstanceInternal = Marshal.GetDelegateForFunctionPointer<PfnVkCreateInstance>(createInstanceFp);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

    [LibraryImport("vulkan-1.dll", EntryPoint = "vkGetInstanceProcAddr", StringMarshalling = StringMarshalling.Utf8)]
    private static partial int GetInstanceProcAddr(IntPtr module, string name);
}   

And the line I am getting the exception on is
CreateInstanceInternal = Marshal.GetDelegateForFunctionPointer<PfnVkCreateInstance>(createInstanceFp);
CreateInstanceInternal = Marshal.GetDelegateForFunctionPointer<PfnVkCreateInstance>(createInstanceFp);
. The problem I am having is that the exception that is thrown is both not caught by the catch block and doesn't provide any information as to why it throws the exception. I've even stepped through the IL generated for
Marshal.GetDelegateForFunctionPointer<PfnVkCreateInstance>(createInstanceFp);
Marshal.GetDelegateForFunctionPointer<PfnVkCreateInstance>(createInstanceFp);
and it passes through all the exception checks that it has. So I would have thought that the function would have succeeded. I'm at a loss as to what to try next. Any ideas?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ C# Exceptions Not Caught?
C#CC# / help
3y ago
✅ try catch
C#CC# / help
3y ago
✅ Try catch exception.
C#CC# / help
13mo ago