Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source generation custom marshalling example with a ref parameter #7004

Closed
1 of 3 tasks
alexmalcoci opened this issue Dec 16, 2024 · 1 comment
Closed
1 of 3 tasks

Comments

@alexmalcoci
Copy link

Issue description

It would be great if we could get an example of using native marshalling with a ref parameter. The current examples only handle in and return examples.

I was playing around with the new feature and decided to use dbghelp.dll's SymGetModuleInfo64 as an example. I wrote the custom marshaller and I am able to run it (below is the generated code), but it throws an AccessViolationException when the result of ConvertToManaged is assigned back to the ref. Does the ref parameter need special handling?

    public unsafe partial class Test
    {
        [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "7.0.10.26716")]
        [System.Runtime.CompilerServices.SkipLocalsInitAttribute]
        private static partial bool SymGetModuleInfo64(nint hProcess, long ModuleBase64, ref global::Test.Test.IMAGEHELP_MODULE64 imgHelpModule)
        {
            int __lastError;
            global::Test.Test.IMAGEHELP_MODULE64_Marshaller.Unmanaged __imgHelpModule_native;
            bool __retVal;
            int __retVal_native;
            // Marshal - Convert managed data to native data.
            __imgHelpModule_native = global::Test.Test.IMAGEHELP_MODULE64_Marshaller.ConvertToUnmanaged(imgHelpModule);
            {
                System.Runtime.InteropServices.Marshal.SetLastSystemError(0);
                __retVal_native = __PInvoke(hProcess, ModuleBase64, &__imgHelpModule_native);
                __lastError = System.Runtime.InteropServices.Marshal.GetLastSystemError();
            }

            // Unmarshal - Convert native data to managed data.
            __retVal = __retVal_native != 0;
----->      imgHelpModule = global::Test.Test.IMAGEHELP_MODULE64_Marshaller.ConvertToManaged(__imgHelpModule_native);
            System.Runtime.InteropServices.Marshal.SetLastPInvokeError(__lastError);
            return __retVal;
            // Local P/Invoke
            [System.Runtime.InteropServices.DllImportAttribute("dbghelp.dll", EntryPoint = "SymGetModuleInfo64", ExactSpelling = true)]
            static extern unsafe int __PInvoke(nint hProcess, long ModuleBase64, global::Test.Test.IMAGEHELP_MODULE64_Marshaller.Unmanaged* imgHelpModule);
        }
    }

Target framework

Check the .NET target framework(s) being used, and include the version number(s).

  • .NET Core
  • .NET Framework 9.0.100-preview.7.24407.12
  • .NET Standard
About VS info
Microsoft Visual Studio Community 2022
Version 17.10.5
VisualStudio.17.Release/17.10.5+35122.118
Microsoft .NET Framework
Version 4.8.09037

Installed Version: Community

Visual C++ 2022   00482-90000-00000-AA361
Microsoft Visual C++ 2022

Azure App Service Tools v3.0.0   17.10.344.38934
Azure App Service Tools v3.0.0

C# Tools   4.10.0-3.24324.8+04fb39164c99c519832109f21b22630bc9246fce
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

NuGet Package Manager   6.10.2
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Visual Basic Tools   4.10.0-3.24324.8+04fb39164c99c519832109f21b22630bc9246fce
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools   17.10.0-beta.24228.1+dd749058c91585e9b5dae62b0f8df892429ee28f
Microsoft Visual F# Tools
@alexmalcoci
Copy link
Author

The issue was my Unmanaged struct was not fully fleshed out so was too small, which caused it to mess up the stack on the way out. Refs work just fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant