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

Constructor taking LPCSTR (i.e const char*) no imported correctly #1256

Closed
IIFE opened this issue Oct 14, 2019 · 1 comment
Closed

Constructor taking LPCSTR (i.e const char*) no imported correctly #1256

IIFE opened this issue Oct 14, 2019 · 1 comment
Labels

Comments

@IIFE
Copy link
Contributor

IIFE commented Oct 14, 2019

Brief Description

Please refer to this repo for a reproducible example. If you build and run the app you'll get the issue faced.

I have a simple class with one constructor that takes a LPCSTR (typedef for const char*) param, and has a member of the same type.

The member generation is fine, however the constructor generation is incorrect. The constructor is imported as:
internal static extern global::System.IntPtr ctor(global::System.IntPtr __instance, global::System.IntPtr lpsz);

With the generated C# equivalent looking like:

        public Consumer(string lpsz)
        {
            __Instance = Marshal.AllocHGlobal(sizeof(global::CppSharpTestNative.dll.Consumer.__Internal));
            __ownsNativeInstance = true;
            NativeToManagedMap[__Instance] = this;
            __Internal.ctor((__Instance + __PointerAdjustment), lpsz);
        }

The last line is failing because the imported signature is expecting an IntPtr, but the generated class is using a System.String.

Is there a way to generate the import signature with the correct type, or modify the generated code to correctly convert to and from LPCSTR.

The LPCSTR member is generated fine with the correct marshaling:

        public string MLpsz
        {
            get
            {
                return Marshal.PtrToStringAnsi(((global::CppSharpTestNative.dll.Consumer.__Internal*) __Instance)->m_lpsz);
            }

            set
            {
                ((global::CppSharpTestNative.dll.Consumer.__Internal*)__Instance)->m_lpsz = (global::System.IntPtr) Marshal.StringToHGlobalAnsi(value);
            }
        }

The constructor however is not.

Thanks.

OS: Windows

Target: MSVC

@tritao
Copy link
Collaborator

tritao commented Oct 25, 2019

Thanks for the sample, it needs to be fixed.

@tritao tritao added the bug label Oct 25, 2019
@ddobrev ddobrev closed this as completed Apr 15, 2020
@ddobrev ddobrev reopened this Apr 15, 2020
ddobrev added a commit that referenced this issue Apr 15, 2020
ddobrev added a commit that referenced this issue Apr 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants