0

I have an API written in C++ and am attempting to use this with interop in C# Windows forms. I am having difficulty defining the equivalent C# struct. Here is the C++ struct:

typedef struct
{
    char SerNo[LENGTH];
    HANDLE dev;
} myDevice;

Edit:

Thanks to the comments the first item is solved. Here is the current C# status:

    public unsafe struct sdrplay_api_DeviceT
    {
        [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 64)]
        string SerNo;

        public IntPtr dev;
    }

However nothing I try get's the HANDLE. So I guess at this time, I will expand on the code further:

In the original C++ code, there is a function as follows:

GetDevices(myDevice *devs);

and in the C# code I have done this:

 public unsafe static extern GetDevices([In, Out] myDevice[] devs);

Now the code I use is as follows:

myDevice[] devs = new myDevice[6];

var error = GetDevices(devs);

If I look at devs in Visual Studio I see the SerNo (devs[0].SerNo) correctly but the "handle is always 0;

I tried IntPtr, byte, but I am at a loss. Thanks, Tom

yacc
  • 2,915
  • 4
  • 19
  • 33
Tom
  • 527
  • 1
  • 8
  • 28
  • 1
    This is C code not C++ code. – n314159 Jan 16 '20 at 18:00
  • What's different about those dozens that you could do them all, but not this one? – GSerg Jan 16 '20 at 18:01
  • The difficulty is how do you define these two items in C#? All the others were either unsigned char (becomes byte) or double etc. These two I have not found the C# equivalent – Tom Jan 16 '20 at 18:05
  • 1
    For marshalling the `char SerNo[LENGTH]` array see [Marshal C++ struct array into C#](https://stackoverflow.com/q/188299/3744182). You may also need [Marshaling c structures in c#](https://stackoverflow.com/a/9188760/3744182). – dbc Jan 16 '20 at 18:05
  • `HANDLE` could be anything. Look up the `typedef` or `#define`. – J. Doe Jan 16 '20 at 18:08
  • 1
    `[MarshalAs(UnmanagedType.ByValTStr, SizeConst=LENGTH)] string SerNo` and `IntPtr dev`? – GSerg Jan 16 '20 at 18:08
  • @Tom the `HANDLE` is probably another `struct` or a Macro, without knowing its definition, we cannot say anything regarding that. – n314159 Jan 16 '20 at 18:08
  • @n314159 Why would this not be valid C++ code? – GSerg Jan 16 '20 at 18:10
  • @GSerg It is valid C++ code, but the usage of the `typedef` before the `struct` suggests that is really C code (that is just in that subset of C code that is supported by C++) since for C the `typedef` is needed to not always have to write `struct myDevice` when using it while in C++ this also given for `struct myDevice {...};` as definition. – n314159 Jan 16 '20 at 18:13
  • `HANDLE` is presumably `typedef PVOID HANDLE;` as defined in `WinNT.h`, see https://learn.microsoft.com/en-us/windows/win32/winprog/windows-data-types – dbc Jan 16 '20 at 18:13
  • @GSerg If that code is standard conforming C++ depends on whether `LENGTH` is a compile-time constant expression or not. – J. Doe Jan 16 '20 at 18:13
  • 1
    Could be char[], byte[] or string, decorated with [MarshalAs] to make it a ByValArray or ByValTStr. Which one you need depends on the C code, whether it stores a 0-terminated C string in the field and what kind of encoding it assumes. Start with byte[] if you have no idea, the debugger can give you some insight. – Hans Passant Jan 16 '20 at 18:25
  • 1
    @dbc The link you sent worked for the first entry! This in particular: [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 64)] public string SerNo; – Tom Jan 16 '20 at 19:29

0 Answers0