Invoking native dlls in C#
-
Hi I have faced one problem on calling the native dll's functions from C# .Net coding.Actually i wrote one dll to control the Digital Multimeter in a remote programming mode through GPIB interface. Initially i have imported the win32 dll provided with GPIB driver in my coding and tried to send the data to the Digital Multimeter, Whenever i send the data, it throws this Exception " System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." , but i will send the data from VC++ application,its working fine. Is there any specific settings needs to do? Please help me to solve this problem. For your reference i have pasted the header file function ,VC++ coding as well as C# coding here. Header file function ---------------------------- extern long int _cdecl ieee488_send (long int,char *,unsigned long,long int *); #define send(addr,s,status) ieee488_send(addr,(char *) (s),0xFFFF,(long int *) status) VC++ ---------- send(temp,info,&status); info is character pointer Status - int C# -- I have tried in the folowing ways ------------------------------------------------------ 1. [DllImport("IIEEE_32M")] public extern static int send(int address,string strCommand , ref int status); send(inGpibAddress, Command, ref inStatus); 2. [DllImport("IIEEE_32M")] public extern static int send(int address,string strCommand , out int status); send(inGpibAddress, Command, out inStatus); 3. [DllImport("IIEEE_32M")] public extern static int send(int address,[MarshalAs(UnmanagedType.LPStr)] string strCommand, out int status); send(inGpibAddress, Command, out inStatus); 4. [DllImport("IIEEE_32M")] public extern static int send(int address,[MarshalAs(UnmanagedType.LPStr)] string strCommand, intptr status); send(inGpibAddress, Command, out intptr inStatus); Thanks in Advance Thanks and Regards Madhu
-
Hi I have faced one problem on calling the native dll's functions from C# .Net coding.Actually i wrote one dll to control the Digital Multimeter in a remote programming mode through GPIB interface. Initially i have imported the win32 dll provided with GPIB driver in my coding and tried to send the data to the Digital Multimeter, Whenever i send the data, it throws this Exception " System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." , but i will send the data from VC++ application,its working fine. Is there any specific settings needs to do? Please help me to solve this problem. For your reference i have pasted the header file function ,VC++ coding as well as C# coding here. Header file function ---------------------------- extern long int _cdecl ieee488_send (long int,char *,unsigned long,long int *); #define send(addr,s,status) ieee488_send(addr,(char *) (s),0xFFFF,(long int *) status) VC++ ---------- send(temp,info,&status); info is character pointer Status - int C# -- I have tried in the folowing ways ------------------------------------------------------ 1. [DllImport("IIEEE_32M")] public extern static int send(int address,string strCommand , ref int status); send(inGpibAddress, Command, ref inStatus); 2. [DllImport("IIEEE_32M")] public extern static int send(int address,string strCommand , out int status); send(inGpibAddress, Command, out inStatus); 3. [DllImport("IIEEE_32M")] public extern static int send(int address,[MarshalAs(UnmanagedType.LPStr)] string strCommand, out int status); send(inGpibAddress, Command, out inStatus); 4. [DllImport("IIEEE_32M")] public extern static int send(int address,[MarshalAs(UnmanagedType.LPStr)] string strCommand, intptr status); send(inGpibAddress, Command, out intptr inStatus); Thanks in Advance Thanks and Regards Madhu
I think the problem is with the string type. Have you tried using a byte[] array? Also, you may want to write a wrapper function using MC++ and use the pin on the byte[] array to make sure the garbage collector doesn't move that memory since it's being used by a native sub-routine that can't be notified of pointer re-locations. Just a thought ---- www.muzikstor.com
-
Hi I have faced one problem on calling the native dll's functions from C# .Net coding.Actually i wrote one dll to control the Digital Multimeter in a remote programming mode through GPIB interface. Initially i have imported the win32 dll provided with GPIB driver in my coding and tried to send the data to the Digital Multimeter, Whenever i send the data, it throws this Exception " System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." , but i will send the data from VC++ application,its working fine. Is there any specific settings needs to do? Please help me to solve this problem. For your reference i have pasted the header file function ,VC++ coding as well as C# coding here. Header file function ---------------------------- extern long int _cdecl ieee488_send (long int,char *,unsigned long,long int *); #define send(addr,s,status) ieee488_send(addr,(char *) (s),0xFFFF,(long int *) status) VC++ ---------- send(temp,info,&status); info is character pointer Status - int C# -- I have tried in the folowing ways ------------------------------------------------------ 1. [DllImport("IIEEE_32M")] public extern static int send(int address,string strCommand , ref int status); send(inGpibAddress, Command, ref inStatus); 2. [DllImport("IIEEE_32M")] public extern static int send(int address,string strCommand , out int status); send(inGpibAddress, Command, out inStatus); 3. [DllImport("IIEEE_32M")] public extern static int send(int address,[MarshalAs(UnmanagedType.LPStr)] string strCommand, out int status); send(inGpibAddress, Command, out inStatus); 4. [DllImport("IIEEE_32M")] public extern static int send(int address,[MarshalAs(UnmanagedType.LPStr)] string strCommand, intptr status); send(inGpibAddress, Command, out intptr inStatus); Thanks in Advance Thanks and Regards Madhu
I've never tried using a #define with P/Invoke, but if I were to guess, I'd say that may your problem. I suggest you try having your C# calling the ieee488_send method instead of the define'd send:
public extern static int ieee488_send(int address,[MarshalAs(UnmanagedType.LPStr)]string param2, uint param3, out int status); // try ref int status if this doesn't work
-
Hi I have faced one problem on calling the native dll's functions from C# .Net coding.Actually i wrote one dll to control the Digital Multimeter in a remote programming mode through GPIB interface. Initially i have imported the win32 dll provided with GPIB driver in my coding and tried to send the data to the Digital Multimeter, Whenever i send the data, it throws this Exception " System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." , but i will send the data from VC++ application,its working fine. Is there any specific settings needs to do? Please help me to solve this problem. For your reference i have pasted the header file function ,VC++ coding as well as C# coding here. Header file function ---------------------------- extern long int _cdecl ieee488_send (long int,char *,unsigned long,long int *); #define send(addr,s,status) ieee488_send(addr,(char *) (s),0xFFFF,(long int *) status) VC++ ---------- send(temp,info,&status); info is character pointer Status - int C# -- I have tried in the folowing ways ------------------------------------------------------ 1. [DllImport("IIEEE_32M")] public extern static int send(int address,string strCommand , ref int status); send(inGpibAddress, Command, ref inStatus); 2. [DllImport("IIEEE_32M")] public extern static int send(int address,string strCommand , out int status); send(inGpibAddress, Command, out inStatus); 3. [DllImport("IIEEE_32M")] public extern static int send(int address,[MarshalAs(UnmanagedType.LPStr)] string strCommand, out int status); send(inGpibAddress, Command, out inStatus); 4. [DllImport("IIEEE_32M")] public extern static int send(int address,[MarshalAs(UnmanagedType.LPStr)] string strCommand, intptr status); send(inGpibAddress, Command, out intptr inStatus); Thanks in Advance Thanks and Regards Madhu
I'm no C# expert, but if the ieee488_send method is expecting a pointer for the second and last arguments, then I think you need to declare them BOTH using "ref" (as you did the last one).
[DllImport("IIEEE_32M")] public extern static int send(int address,ref string strCommand,int xx, ref int status); send(inGpibAddress, Command, 0, inStatus);
Also, does the method expect 3 or 4 arguments? In your VC++ example you have 4 arguments in the header file definition for ieee488_send. In C# you are only defining it as having 3 arguments... ---------- There go my people. I must find out where they are going so I can lead them. - Alexander Ledru-Rollin