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

Mono(Unity) PInvoke struct with wchar_t[] #21732

Closed
kombain32 opened this issue Dec 13, 2023 · 0 comments
Closed

Mono(Unity) PInvoke struct with wchar_t[] #21732

kombain32 opened this issue Dec 13, 2023 · 0 comments

Comments

@kombain32
Copy link

kombain32 commented Dec 13, 2023

I'm trying to PInvoke (on Windows) a method which has a struct parameter with wchar_t[].

The C++ (Qt) declaration looks this:
`
#define MAX_FIELD_LEN (30)

typedef void* Context;
typedef void* Handle;

struct Info
{
wchar_t id[MAX_FIELD_LEN];
wchar_t version[MAX_FIELD_LEN];
wchar_t date[MAX_FIELD_LEN];
uint8_t channels;
wchar_t serialNumber[MAX_FIELD_LEN];
uint32_t speed;
};

#define LIB_EXPORT __stdcall

LIB_EXPORT
Status Open(Context ctx
, Type type
, qsizetype idx
, Info& Info
, Handle &handle
);

enum Status
{
/Statuses/
}

enum Type
{
/Types/
};

`

The C# declaration looks this:

`
[StructLayout(LayoutKind.Sequential)]
public struct Info
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
public char[] id;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
public char[] version;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
public char[] date;
public byte channels;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
public char[] serialNumber;
public uint speed;
}

[DllImport("lib", CallingConvention = CallingConvention.StdCall)]
public static extern Status Open(IntPtr context, Type type, int index, ref Info data, ref IntPtr handle);

`
This works in WinForms, but in Mono (Unity), the struct is not filled with values. I suspect that it's about different string marshalling in Mono and MS, but I can't figure out how to fix it.

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