Question : exception with dllimport

Hello experts
I am having problems with importing a unmanaged dll,
I am getting an exception when trying to run a function that receives a struct (DB_Client).
this is the code,
I am using c# WinForm VS2005
Thanks.

c++ dll :

typedef char FieldStr[256];
typedef char FieldTime[20];

struct DB_Client
{
      unsigned long      client_id;
      unsigned long      parent_id;
      FieldStr            mask_name;
      FieldStr            comp_name;
      FieldStr            comp_type;
      FieldStr            ip;
      FieldStr            login_name;
      FieldStr            rdxrc_addr;
      FieldStr            net_name;
      FieldStr            os;
      FieldTime            diff_time;
      FieldStr            mac_address;
      FieldTime            handshake_time;
      unsigned int      is_online;
};

bool      DB_get_client(unsigned long client_id, DB_Client& client_obj);
//////////////////////////////////////

c# app

    public struct DB_Client
    {
        public ulong client_id;
        public ulong parent_id;
        public string mask_name;
        public string comp_name;
        public string comp_type;
        public string ip;
        public string login_name;
        public string rdxrc_addr;
        public string net_name;
        public string os;
        public string diff_time;
        public string mac_address;
        public string handshake_time;
        public uint is_online;
    };

[DllImport("rdxDB.dll")]
public static extern bool DB_get_client(ulong client_id,DB_Client client_obj);


private void form1_Load(object sender, EventArgs e)
{
      DB_Client dbcl = new DB_Client();
      DBDll.DB_get_client(14,dbcl);
}

exception:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Answer : exception with dllimport

Did you change the method to use 'ref'

And you've got a long in there - so the C# version needs to have an int instead...
Random Solutions  
 
programming4us programming4us