Questions tagged [ref-struct]
15 questions
16
votes
1 answer
Why ref structs cannot be used as type arguments?
C# 7.2 introduced ref structs. However, given a ref struct like this:
public ref struct Foo {
public int Bar;
}
I cannot use it as a type argument:
int i = 0;
var x = Unsafe.As(ref i); // <- Error CS0306 The type 'Foo' may not be used…

Fit Dev
- 3,413
- 3
- 30
- 54
11
votes
2 answers
How to test whether a ref struct method is throwing an exception using xUnit?
I'm new to xUnit, but, as far as I know, the standard way of checking whether something throws an exception is to use Assert.Throws or Assert.ThrowsAny methods.
But these methods expect an Action as parameter; and ref structs can't be…

Trauer
- 1,981
- 2
- 18
- 40
5
votes
1 answer
Why a `stackalloc` expression cannot be assigned to a `Span` parameter?
Consider the following methods (fiddle):
void test(Span param)
{
//Fail, the stackalloc'ed buffer could be exposed.
param = stackalloc int[10];
}
void test2(Span param)
{
//OK
Span local = stackalloc int[10];
}
I…

Margaret Bloom
- 41,768
- 5
- 78
- 124
4
votes
1 answer
Corrupt contents in NodeJS ref-struct upon garbage collection
Nesting a ref-struct instance within another, one of the properties of the nested object is corrupted upon manual garbage collection.
See this minimal code reproduction: https://github.com/hunterlester/minimum-ref-struct-corruption
Notice on the 3rd…

Hunter Lester
- 2,972
- 3
- 15
- 19
3
votes
1 answer
Why can I declare a ref struct as a member of a class in c#7.3?
According the docs:
You can't declare a ref struct as a member of a class or a normal struct.
But I managed to compile and run this:
public ref struct RefStruct
{
public int value;
}
public class MyClass
{
public RefStruct Item =>…

joe
- 1,078
- 2
- 11
- 30
3
votes
0 answers
node-ffi OUT parameter with type of Struct Pointer cannot receive back values from DLL call
C Data Structure:
typedef struct dpfpdd_dev_info {
unsigned int size;
char name[MAX_DEVICE_NAME_LENGTH];
DPFPDD_HW_DESCR descr;
DPFPDD_HW_ID id;
DPFPDD_HW_VERSION ver;
…

SunTossed
- 31
- 3
3
votes
3 answers
How to define a ref struct in F# in .NET Standard 2.0?
When F# 4.5 was announced, it was stated that:
The F# feature set is comprised of
[...]
The ability to produce IsByRefLike structs (examples of such structs: Span<'T>and ReadOnlySpan<'T>).
How to "produce" these types? I tried the…

Theodore Tsirpanis
- 743
- 8
- 21
2
votes
0 answers
node-ffi-napi not receiving any response from dll on method call, possible struct issue?
So C++ isn't my strong suit so it's very likely I've made a mistake here in the creation of my struct types, I don't quite understand how to create the equivalent return type the C++ code expects on the js side.
Here's a working example from the…

Paul Ryan
- 461
- 2
- 6
2
votes
1 answer
Unable to install ref-struct module on nodejs 12.14.0
i am trying to install ref-struct module using following command :
npm install ref-struct
Environment specification :
nodejs : 12.14.0
npm : 6.13.4
Error i am getting error :
Output as text in above screenshots.…

Shivam Kumar
- 123
- 3
- 10
2
votes
0 answers
Array of structure as OUT parameter from C to JS using node-ffi, ref-array and ref-struct
I am using node-ffi for integration of JS with C library. I figured out ways to pass in complex structures as IN params and get single structure as OUT param. But, I could not successfully get an array of structures from C and iterate over them in…

rakeshgn
- 41
- 8
2
votes
1 answer
How to read data from a nodejs Buffer created by node-ffi with length 0
I'm trying to wrap an existing C library using node-ffi but I can't seem to read the returned result. The situation is a little awkward because the function returns the C struct
typedef struct {
int size;
void *ptr;
} datum;
and the length…

Charlie Burns
- 6,994
- 20
- 29
1
vote
1 answer
System.Reflection.Emit: field or property of ref struct
For the following interface and struct:
internal interface IRecord where T : struct
{
ref T Values { get; }
}
public struct Entity
{
public int Field1;
...
}
I would like get the following lambda expression via…

Weifen Luo
- 603
- 5
- 13
1
vote
0 answers
How do I use complex data structures with `ffi`, `ref-struct`, and `ref-array`?
I am working on a project that uses ffi to interact with a compiled C library. I am trying to get some complex data structures to work and am having problems figuring out the proper way to get values inside these data structures. I have two…

Zachary Abresch
- 756
- 3
- 8
- 20
0
votes
1 answer
How To Read 16 bits integer with ffi and ref-struct
I am able to load a certain DLL and call a certain function in that DLL using some basic Win32 C/C++ code.
The function I call wants the address of a buffer allocated by the caller, which will be filled by the call, at a given offset, with a 16 bits…

manuell
- 7,528
- 5
- 31
- 58
0
votes
1 answer
What is GRefString implementation in GLib?
I wonder where is the reference count stored? As the type is defined as:
typedef char GRefString;
And all the g_ref_string*…() functions are returning simply gchar * instead of a structure, which could hold the reference count. Is it the trick of…

psprint
- 349
- 1
- 10