How to pass parameter that can be of type int/double/string/int[] from C# to native C?

char m

Can it be done something like this:

Native DLL:

void SetFieldValue(const char *Field, void *pValue, int Count)
{
    char *StrValue;
    int *IntArrayValue;
    if (!strcmp(Field, "StrField"))
    {
        StrValue = malloc((Count + 1) * sizeof(char)); 
        strcpy(StrValue, (char *)pValue);
        DoSomethingWithStringValue(StrValue);
        free(StrValue);
    }
    else if (!strcmp(Field, "IntArrayField"))
    {
        IntArrayValue = malloc(Count * sizeof(int)); 
        memcpy(IntArrayValue, pValue, Count);
        DoSomethingWithIntArrayValue(IntArrayValue);
        free(StrValue);
    }
    //... and so on
}

Managed:

[DllImport(DllName, CallingConvention = DllCallingConvention)]
private static extern void SetFieldValue(string fieldName, IntPtr value, int count);


public void SetIntArray()
{
    int[] intArray = { 1, 2, 3 };
    SetFieldValue("IntArrayField", intArray, 3);
}


public void SetString()
{
    SetFieldValue("StrField", "SomeValue", 9);
}

//... and so on
David Heffernan

One way is to use method overloading. On the C# side you would declare multiple versions of the imported function. For the two examples in the question that would look like this:

[DllImport(DllName, CallingConvention = DllCallingConvention)]
private static extern void SetFieldValue(string fieldName, int[] value, int count);

[DllImport(DllName, CallingConvention = DllCallingConvention)]
private static extern void SetFieldValue(string fieldName, string value, int count);

These two p/invoke functions are linked to the same single unmanaged function. For the first overload, the value argument is marshalled as a pointer to the first element of the array. For the second overload the value argument is marshalled as a pointer to a null-terminated character array. In both cases that is what you need.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do you pass a function as a parameter in C?

From Dev

How can I pass an attribute parameter type with List<string> in C#?

From Dev

How to pass lambda expressions as parameter in C#

From Dev

How to pass a class (not an object) as a parameter in C++

From Dev

How to pass an Object type to Type parameter in C#

From Dev

pass class type as parameter c# and used with dictionary

From Dev

How to pass a pointer from C# to native function in DLL?

From Dev

Pass table-type object as input parameter to Stored Procedure in Oracle from C#

From Dev

How to pass optional parameter to a method in C++?

From Dev

Pass a parameter from C# to Python

From Dev

How to pass method parameter when expected to be generic interface base type in c#?

From Dev

How to call C# delegate to pass array of strings from native C simplest way?

From Dev

Pass += or -= as a parameter in c#

From Dev

How I can pass lambda expression to c++ template as parameter

From Dev

How to pass parameter from an array in c#?

From Dev

How can i pass a file as a parameter in C++?

From Dev

How can get raw file from jni native c++

From Dev

How can I pass DOMDocument from VBA to C++ dll

From Dev

C pass reference to parameter

From Dev

How to pass a class (not an object) as a parameter in C++

From Dev

How do you pass a type as a parameter in c++?

From Dev

Can't pass parameter from C to Assembly code

From Dev

Pass table-type object as input parameter to Stored Procedure in Oracle from C#

From Dev

How to pass value to pointer parameter C++

From Dev

Pass a parameter from C# to Python

From Dev

How can i pass a file as a parameter in C++?

From Dev

How can I pass parameter of type HttpContent in web api?

From Dev

How to pass a contiguous array as a parameter in C?

From Dev

How can I pass a Vector from C++ to Fortran?

Related Related

  1. 1

    How do you pass a function as a parameter in C?

  2. 2

    How can I pass an attribute parameter type with List<string> in C#?

  3. 3

    How to pass lambda expressions as parameter in C#

  4. 4

    How to pass a class (not an object) as a parameter in C++

  5. 5

    How to pass an Object type to Type parameter in C#

  6. 6

    pass class type as parameter c# and used with dictionary

  7. 7

    How to pass a pointer from C# to native function in DLL?

  8. 8

    Pass table-type object as input parameter to Stored Procedure in Oracle from C#

  9. 9

    How to pass optional parameter to a method in C++?

  10. 10

    Pass a parameter from C# to Python

  11. 11

    How to pass method parameter when expected to be generic interface base type in c#?

  12. 12

    How to call C# delegate to pass array of strings from native C simplest way?

  13. 13

    Pass += or -= as a parameter in c#

  14. 14

    How I can pass lambda expression to c++ template as parameter

  15. 15

    How to pass parameter from an array in c#?

  16. 16

    How can i pass a file as a parameter in C++?

  17. 17

    How can get raw file from jni native c++

  18. 18

    How can I pass DOMDocument from VBA to C++ dll

  19. 19

    C pass reference to parameter

  20. 20

    How to pass a class (not an object) as a parameter in C++

  21. 21

    How do you pass a type as a parameter in c++?

  22. 22

    Can't pass parameter from C to Assembly code

  23. 23

    Pass table-type object as input parameter to Stored Procedure in Oracle from C#

  24. 24

    How to pass value to pointer parameter C++

  25. 25

    Pass a parameter from C# to Python

  26. 26

    How can i pass a file as a parameter in C++?

  27. 27

    How can I pass parameter of type HttpContent in web api?

  28. 28

    How to pass a contiguous array as a parameter in C?

  29. 29

    How can I pass a Vector from C++ to Fortran?

HotTag

Archive