Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C code



C#
// The cJSON structure:
typedef struct cJSON {
struct cJSON *next,*prev;
struct cJSON *child;
int type; // The type of the item, as above.

char *valuestring; // The item's string, if type==cJSON_String
int valueint; // The item's number, if type==cJSON_Number
double valuedouble; // The item's number, if type==cJSON_Number

char *string; // The item's name string, if this item is the child of, or is in the list of subitems of an object.
} cJSON;

typedef struct cJSON_Hooks {
  void *(*malloc_fn)(size_t sz);
  void (*free_fn)(void *ptr);
} cJSON_Hooks;

// Supply malloc, realloc and free functions to cJSON
__declspec(dllexport) void cJSON_InitHooks(cJSON_Hooks* hooks);





C# code


namespace DeviceApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

[DllImport("cJson.dll")]
public static extern void cJSON_InitHooks(cJSON_Hooks* hooks);
}
}


cJSON_Hooks and cJSON is a struct in cJson.h, but in C# code ,cJSON_Hooks and cJSON can't be incognizanced. If someone could walk me through it or point me in the right direction it would be greatly appreciated. Thanks
Posted

I wouldn't do it the pinvoke way in this case... Use C++/CLI to create a managed ref class which references and manages the lifetime of those cJSON objects and exposes appropriate methods and properties to query them. These will be visible to any .NET program. Then, in your C# program use only this CLR class.
 
Share this answer
 
thanks!but, I don't konw how to overwirte "struct cJSON *next,*prev;struct cJSON *child;"
who can help me overwirte this struct to C#? thanks very much!!!
 
Share this answer
 
Comments
Christian Graus 2-Sep-10 22:08pm    
You can use pointers in C#, just declare them in an unsafe block. I'm sure pinvoke has examples of this.

Please do not push 'answer' if you're not posting an answer, this is a comment, which is what you should have done.
you need to also create a struct that is the same in it's layout, in C#, so you can pass it in. www.pinvoke.net has lots of examples how this is done.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900