how to pass void * (pointing to double or int) from C to C#

char m

I have general callback type in C:

typedef int(*OverrideFieldValueSetCB_t)(const char *Dialog, const char *FieldName, void *Value);

and callback:

OverrideFieldValueSetCB_t       gOverrideFieldValueSetCB;

and function I call in C-code to pass value to C# :

int DllGuiSetFieldValue(const char *Dialog, const char *FieldName, void *pValue)
{   
    return gOverrideFieldValueSetCB(Dialog, FieldName, pValue);
}

In C# code I set this kind of delegate:

private static int OverrideFieldValueSetCb(string dialogName, string fieldName, IntPtr value)
{
    ///...
}

In above I would like to marshal/cast value to int or double depending on fieldName.

Questions:

  1. Is IntPtr correct?
  2. If IntPtr is correct, how to cast/marshal it to double or int ?
Luaan

"Pointing to double or int" is just asking for trouble.

But if you're certain that's the way you want to go, have a look at the Marshal class - Marshal.ReadInt32 for int, and Marshal.PtrToStructure<double> for double. Make sure you don't mess the two up :)

Of course, if you can use unsafe code, you don't need to use Marshal. Just do the cast as you would in C.

Example:

double val = 123.45d;
double second;
double third;

unsafe
{
  void* ptr = &val;

  second = *(double*)ptr;
  third = Marshal.PtrToStructure<double>(new IntPtr(&val));
}

second.Dump();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C: Accessing (int) via (void *) pointing to (void *) pointing to (int) by typecasting and dereferencing

From Dev

C: Accessing (int) via (void *) pointing to (void *) pointing to (int) by typecasting and dereferencing

From Dev

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

From Dev

With C++ how do I Pass a value from a void function?

From Dev

JNA: How to pass a string as void* from Java to C

From Dev

How to pass a double pointer from C++ to C#

From Dev

check what type of pointer void* is pointing to in C

From Dev

check what type of pointer void* is pointing to in C

From Dev

c++ cannot convert 'double' to 'double*' for argument 1 to 'void sort(double*,int)' error

From Dev

How to pass legacy C++ void pointers to and from a C++ /CX class?

From Dev

Void * to char or int in C

From Dev

C++ invalid cast from type ‘void*’ to type ‘double’

From Dev

How to pass const void *bytes to an Objective-C block from Swift

From Dev

C++ - How to correctly cast double to int

From Dev

double star pointer pointing to a pointer of arrays; How do I access the pointer of arrays? C++

From Dev

How to dereference a double void pointer to an int pointer

From Dev

C/C++ casting void* to int ( * () ) (int,...);

From Dev

double to int in c

From Dev

double to int in c

From Dev

Double to int conversion in C

From Dev

How should I pass and return unsigned int by value from Java to C/C++ in jna

From Dev

How to copy array char into string or int* (void *) in c++?

From Dev

Pass int by reference from C++/CLI to C#

From Dev

pass Int to C function

From Dev

Pass double array by reference from C to Delphi DLL

From Dev

C++ memcpy: double incompatible with const void

From Dev

How can I pass a struct and an int from an function and back to main? C

From Dev

C: How to dereference void**?

From Dev

C: How to dereference void**?

Related Related

  1. 1

    C: Accessing (int) via (void *) pointing to (void *) pointing to (int) by typecasting and dereferencing

  2. 2

    C: Accessing (int) via (void *) pointing to (void *) pointing to (int) by typecasting and dereferencing

  3. 3

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

  4. 4

    With C++ how do I Pass a value from a void function?

  5. 5

    JNA: How to pass a string as void* from Java to C

  6. 6

    How to pass a double pointer from C++ to C#

  7. 7

    check what type of pointer void* is pointing to in C

  8. 8

    check what type of pointer void* is pointing to in C

  9. 9

    c++ cannot convert 'double' to 'double*' for argument 1 to 'void sort(double*,int)' error

  10. 10

    How to pass legacy C++ void pointers to and from a C++ /CX class?

  11. 11

    Void * to char or int in C

  12. 12

    C++ invalid cast from type ‘void*’ to type ‘double’

  13. 13

    How to pass const void *bytes to an Objective-C block from Swift

  14. 14

    C++ - How to correctly cast double to int

  15. 15

    double star pointer pointing to a pointer of arrays; How do I access the pointer of arrays? C++

  16. 16

    How to dereference a double void pointer to an int pointer

  17. 17

    C/C++ casting void* to int ( * () ) (int,...);

  18. 18

    double to int in c

  19. 19

    double to int in c

  20. 20

    Double to int conversion in C

  21. 21

    How should I pass and return unsigned int by value from Java to C/C++ in jna

  22. 22

    How to copy array char into string or int* (void *) in c++?

  23. 23

    Pass int by reference from C++/CLI to C#

  24. 24

    pass Int to C function

  25. 25

    Pass double array by reference from C to Delphi DLL

  26. 26

    C++ memcpy: double incompatible with const void

  27. 27

    How can I pass a struct and an int from an function and back to main? C

  28. 28

    C: How to dereference void**?

  29. 29

    C: How to dereference void**?

HotTag

Archive