DEV Community

Jens Eckervogt
Jens Eckervogt

Posted on

Is it correct or wrongly? struct type* to struct type[] If I create multiple arcs or rectangles?

Hello everyone,

Can I have to give question about drawing multiple rectangles or arcs whatever do you want...

public unsafe static void draw_arcs(..., Arc[] arcs, int num_arcs)
{
...
fixed (Arc* _arcs = arcs)
{
// m_draw_arcs is an internal method from my dll/so
m_draw_arcs(display, window, context, _arcs, num_arcs);
}
}

Do you think I am wrong for C#?

Thanks!

Top comments (5)

Collapse
 
katnel20 profile image
Katie Nelson

What is?

fixed(Arc* _arcs = arcs)

That’s the part that looks non-C#

Collapse
 
jenseckervogt profile image
Jens Eckervogt • Edited

That is C#. Check out Microsoft Development Network Server

C# unsafe and fixed

Does it work like an example "const char* name" from C/C++ to byte* name but I copy internal method FreeImageNet's PtrToStr() to my example program with string if Mono or Net Framework crashes because it works with conversion to string like this:

Result for example with FreeImageSharp ( It is fixed by me ) Because they can't do with FreeImage_GetVersion() into string
example like this:

That is solution with byte* to string

But I have problem with struct type like example Rectangle

public struct Rectangle
{
public int x;
public int y;
public uint width;
public uint height;
...
}

And I use in C/C++ like this

void m_draw_rects(Display* display, Window window, Context context, Rectangle* recs, int* num_rects)
{
...
]

But I have problem with call from DLLImport with unsafe variable

I would like to know understand how do I use with C/C++ to C# like DLLImport if DLLImport knows sometimes unsafe variable like "Rectangle* recs".

Do you think that I should use "IntPtr* recs" or just "Rectangle* recs"?

I can't find google search and google doesn't like my solution....

It means I want draw multiple rectangles.

Thanks I hope who knows C# structure with unsafe variable.

But for default types like byte, char, string and other simple types = Ok
But with structure or class? Is it possible working with unsafe variable?

Collapse
 
katnel20 profile image
Katie Nelson

Can you show us the DLLImport statement for the C++ function. It sounds like a PInvoke error in marshaling the data.

Thread Thread
 
jenseckervogt profile image
Jens Eckervogt

I know that. But It looks simple like that
Simple sample

That is why - I really know like this FreeImage for example:

///
/// Returns a string containing the current version of the library.
///
/// The current version of the library.
public static unsafe string GetVersion() { return PtrToStr(GetVersion_()); }
[DllImport(FreeImageLibrary, CharSet = CharSet.Ansi, EntryPoint = "FreeImage_GetVersion")]
private static unsafe extern byte* GetVersion_();

From FreeImage's Documentation / API References: Click here!

I hope you understand.

But I have problem with Rectangle* rectangles why does C/C++ have only "*" not "[]".

I hope you understand me. Thanks

Thread Thread
 
katnel20 profile image
Katie Nelson

Here the PtrToStr method does the conversion from the static pointer to a csharp string type. You need to convert that pointer to Rectangle structs to an array in csharp.