marshalling - Calling C DLL from C# caused Attempted to read or write protected memory -


i'm trying use zabbix_sender function in c# program. here c# code:

[structlayout(layoutkind.sequential, charset = charset.ansi)] public struct zabbix_sender_value_t {     [marshalas(unmanagedtype.lpstr)] public string host;     [marshalas(unmanagedtype.lpstr)] public string key;     [marshalas(unmanagedtype.lpstr)] public string value; }  [dllimport("zabbix_sender.dll", charset = charset.ansi)] [return: marshalas(unmanagedtype.u4)] public static extern int zabbix_sender_send_values(     [param: marshalas(unmanagedtype.lpstr)] string address,     [param: marshalas(unmanagedtype.u2)] ushort port,      [param: marshalas(unmanagedtype.lpstr)] string source,      [param: marshalas(unmanagedtype.struct)] zabbix_sender_value_t values,     [param: marshalas(unmanagedtype.u4)] int count,     [param: marshalas(unmanagedtype.lpstr)] string result );  static void main() {     zabbix_sender_value_t values;     values.host = "some_hostname";     values.key = "agent.version";     values.value = "2.0.0";     string res = "";     zabbix_sender_send_values("172.16.1.1", 10051, "127.0.0.1", values, 1, res); } 

this definition of c function, i'm trying call dll:

int zabbix_sender_send_values(     const char *address,      unsigned short port,     const char *source,      const zabbix_sender_value_t *values,      int count,     char **result);  typedef struct {     char    *host;     char    *key;     char    *value; } zabbix_sender_value_t; 

execution of c# code ends exception "attempted read or write protected memory. indication other memory corrupt."

i played lot marshalling parameters no luck.

it first time i'm facing of use native dll code c# program, , don't know have now.

please help, oppinion, kick direction find out clue?

thanks

just off-the-cuff guess did enable "allow unsafe code" in project properties , trying wrapping call in unsafe { } clause?


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -