-3

i want to use a c libary for my program in C#.
This libary contains a headerfile with functions in c, to control a camera.

Some of these functions in c, use a pointer called *fg which points at a typedef called Fg_Struct and finally this typedef is declared as Typedef struct Fg_Struct_s Fg_Struct

So now, i want to use these functions from the header file in my c# program. I created a new class and imported these functions from the dll-libary over the pinvoker-addon.
The pinvoker-addon created a new declaration of these functions (from unmanaged to managed).
Most of these new declared functions has a variable called Fg_Struct [] Fg

Visual Studio shows me an error for "Fg_Struct is undefined" in C# programm.


For example, i can use the Fg_getLastErrorDiscripstion function to call the Error discription

in C:

fgrab_struct.h

typedef struct Fg_Struct_s Fg_Struct;

fgrab-prototyp.h

include "struct.h"
const char *const Fg_getLastErrorDescription(Fg_Struct *Fg);

main.cpp

Fg_Struct *fg = NULL;
fg = Fg_Init(DLLNAME, boardNr)
fprintf(stderr, "error in Fg_Init: %s\n", Fg_getLastErrorDescription(NULL));

now i want to use it in c#:

CAM_SISO.cs

[DllImport("fglib5.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr Fg_getLastErrorDescription(Fg_Struct [] Fg);`

I'm really new in C# programming and researched in every topics with pinvoker but I couldn't find a right answer for me, how to declare or define Fg_Struct [] Fg to use the Fg_getLastErrorDiscription function in C#.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • His DLL exports plain functions and standard layout types; this is not at all a duplicate of asking how to access a `declspec(dllexport)` class. – Ben Voigt Nov 02 '16 at 20:01
  • I think his question is specific to define the structure " Fg_Struct [] Fg", so may not be duplicate – Pavan Chandaka Nov 02 '16 at 20:01

1 Answers1

0

If your code doesn't ever actually look inside a Fg_Struct, only pass it to other functions in the DLL, then you can use IntPtr wherever the C header file has Fg_Struct*.

In addition to that, since C source code uses a pointer for any of: array, pass-by-reference parameter, output parameter, the automated p/invoke generator has to guess and will often produce wrong results, such as arrays where they don't belong.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720