Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Invoking native dlls in C#

Invoking native dlls in C#

Scheduled Pinned Locked Moved C#
csharpc++helpperformancequestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    madhusri
    wrote on last edited by
    #1

    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

    M J E 3 Replies Last reply
    0
    • M madhusri

      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

      M Offline
      M Offline
      mikanu
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • M madhusri

        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

        J Offline
        J Offline
        Judah Gabriel Himango
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • M madhusri

          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

          E Offline
          E Offline
          Eric Dahlvang
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups