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

char in C# is not the same thing as char in C++ #24

Open
dmedine opened this issue Jul 20, 2022 · 0 comments
Open

char in C# is not the same thing as char in C++ #24

dmedine opened this issue Jul 20, 2022 · 0 comments

Comments

@dmedine
Copy link
Contributor

dmedine commented Jul 20, 2022

This is just a heads up as I ran into this problem in a different project.

Without testing it, I can say for sure that this:

public double pull_sample(char[] sample, double timeout = FOREVER) { int ec = 0; double res = dll.lsl_pull_sample_c(obj, sample, sample.Length, timeout, ref ec); check_error(ec); return res; }
is not going to work. I don't think anyone is using this overload much, which is why it hasn't come up, but the code as is will definitely not populate the C# array with the contents of the C++ array representing the sample. No exception or compiler warning will be issued, but the char[] sample variable on the C# side will always contain 0s. The reason is that in C# a char is not a primitive type, but rather an object that can be either 1 or 2 bytes depending on how it is encoded. Thus, this pointer won't marshal properly from managed to unmanaged code and the contents will simply retain their default initialization values.

The correct way to do this is to use byte[] on the C# side and then convert it to a char array like so:
char[] sample = System.Text.Encoding.ASCII.GetString(byte_sample).ToCharrArray()'

The same issue will occur with pull_chunk(char[,]...)

I don't know if this really matters. I can't see why anyone would need this overload in the C# wrapper. The overload with string works 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