Calling C dll from C#, return types slightly different

Caleb Johnson

I've written a dll in C which has functions I can call when referencing the DLL in C#. If I use a basic type like an int it works fine, but I have structs which are slightly different in C# than they are in C due to language differences. Here is an example. This is the function definition in C#:

[DllImport("hello_world_cuda.dll", CharSet = CharSet.Auto)]
public static extern Batch Cut();

And here is it in C:

extern "C" Batch __declspec(dllexport) __stdcall Cut()

You can see the return type Batch is the same, but here is its definition in C#

class Envelope
{
    public byte[] Payload;
    public byte[] Signature;
}

class Batch
{
    public Envelope[] Messages;
    public int MsgCount;
}

And here is the definition in C

struct Envelope
{
public:
    char* Payload;
    char* Signature;
};

struct Batch
{
public:
    Envelope* Messages;
    int MsgCount;
};

How do I overcome these language differences in order to successfully make the DLL call in C#?

borrrden

Pointers in an unmanaged language do not map to managed arrays as you have done, this is why it is complaining. char is (almost always, with very limited exceptions) an 8 bit value that maps well to byte in C# as you've noticed, but you need to make them pointers in the managed struct as well:

unsafe struct Envelope
{
    public byte* Payload;
    public byte* Signature;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to treat slightly different types as the same in C++?

From Java

Math "pow" in Java and C# return slightly different results?

From Dev

How to return different types of variable from switch statement in c++

From Dev

Tensorflow C++ inference result is slightly different from Keras inference

From Dev

Calling C++ dll from python

From Java

Calling C++ dll from Java

From Dev

Calling dll implemented JNI from C++

From Dev

Function Pointers with Different Return Types C

From Dev

C++ function with different return types

From Dev

Calling C++ DLL from C++ and C#

From Dev

Calling a function from a DLL which is developed in C++ from C

From Dev

Elegant way to return different value types from function in c++ / c++11

From Dev

Calling C++ dll from C#. "Cannot marshal 'return value': Invalid managed/unmanaged type combination."

From Dev

return struct from .net dll (C#)

From Dev

How to return a C struct from a D DLL?

From Dev

return array from c++ dll to matlab

From Dev

FileNotFoundException when calling C# dll from C++/CLI

From Dev

Calling a C++ DLL from C# with unions

From Dev

Calling C++ dll Method from C#

From Dev

Crash when calling c++ dll function from c#

From Dev

in C# calling from dll a C function with both wchar and char

From Dev

C # calling generic methods mutiple times with different types

From Dev

How to return multiple types from a function in C?

From Dev

Reading different types in C from File

From Dev

C# returning different types from function

From Dev

Calling dll from a Win32 Console Application [C]

From Dev

Properly calling C++ functions in DLL from Delphi "Access Violation"

From Dev

Error when calling c++ dll from delphi

From Dev

Error while calling C DLL function from Python

Related Related

  1. 1

    How to treat slightly different types as the same in C++?

  2. 2

    Math "pow" in Java and C# return slightly different results?

  3. 3

    How to return different types of variable from switch statement in c++

  4. 4

    Tensorflow C++ inference result is slightly different from Keras inference

  5. 5

    Calling C++ dll from python

  6. 6

    Calling C++ dll from Java

  7. 7

    Calling dll implemented JNI from C++

  8. 8

    Function Pointers with Different Return Types C

  9. 9

    C++ function with different return types

  10. 10

    Calling C++ DLL from C++ and C#

  11. 11

    Calling a function from a DLL which is developed in C++ from C

  12. 12

    Elegant way to return different value types from function in c++ / c++11

  13. 13

    Calling C++ dll from C#. "Cannot marshal 'return value': Invalid managed/unmanaged type combination."

  14. 14

    return struct from .net dll (C#)

  15. 15

    How to return a C struct from a D DLL?

  16. 16

    return array from c++ dll to matlab

  17. 17

    FileNotFoundException when calling C# dll from C++/CLI

  18. 18

    Calling a C++ DLL from C# with unions

  19. 19

    Calling C++ dll Method from C#

  20. 20

    Crash when calling c++ dll function from c#

  21. 21

    in C# calling from dll a C function with both wchar and char

  22. 22

    C # calling generic methods mutiple times with different types

  23. 23

    How to return multiple types from a function in C?

  24. 24

    Reading different types in C from File

  25. 25

    C# returning different types from function

  26. 26

    Calling dll from a Win32 Console Application [C]

  27. 27

    Properly calling C++ functions in DLL from Delphi "Access Violation"

  28. 28

    Error when calling c++ dll from delphi

  29. 29

    Error while calling C DLL function from Python

HotTag

Archive