Functions with pointers in c#.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobLefevre
    New Member
    • Jun 2008
    • 4

    Functions with pointers in c#.net

    I am trying to communicate with an outside instrument using a .dll. I am new to working with dll's and am not sure how to do this. The function calls for the following parameters which I kind of think I am doing right:

    [DllImport("MNW. dll", EntryPoint = "mnPollDevices" )]
    public static extern unsafe int mnPollDevices([MarshalAs(Unman agedType.LPArra y)] int*[] device_flag,
    int* port,
    int* buffer,
    [MarshalAs(Unman agedType.LPArra y)] char*[] message,
    int * devicesFound);

    The function should pass an int array of size 128 into the function and when it comes out, the array should contain some values. My problem is actually calling the function. I have tried every combination of pointers and no pointers being sent to the function but am not able to compile. Does anyone know the right syntax? Hardware is the class containing the function. Here is what I am currently using (I know they are not pointers):

    int[] flag = new int[128];
    int port = 0; //COM1 is 0
    int buffer = 128;
    int device_Count = 0;
    char[] message = new char*[80];
    int returncode = 0;

    returncode = Hardware.mnPoll Devices(flag, port, buffer, message, device_Count);
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Well I think for char* you can just pass in a string?
    You probably also need to pass by reference (see the ref parameter keyword)
    What are the complie errors?

    Comment

    • RobLefevre
      New Member
      • Jun 2008
      • 4

      #3
      I was actually able to make this work using the ref command, but now I am getting an exception saying the following:

      The runtime has encountered a fatal error. The address of the error was at 0x79e71bd7, on thread 0x510. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

      I think it has to do with passing the int/char array. I have no idea what the marshall is (I found some code segments online that use this nomenclature for passing arrays to and from .dll's in c#).

      Here is the segment I am calling:
      [code=c#]
      [DllImport("MNW. dll", EntryPoint = "mnPollDevices" )]
      public static extern int mnPollDevices([MarshalAs(Unman agedType.LPArra y)]ref int[] device_flag,

      ref int port,
      ref int buffer,
      [MarshalAs(Unman agedType.LPArra y)] ref char[] message,
      ref int devicesFound);
      [/code]
      And the following is the call:
      [code=c#]
      int[] flag = new int[128];
      int port = 0; //COM1 is 0
      int buffer = 128;
      int device_Count = 0;
      char[] message = new char[80];
      int returncode = 0;
      int BaudRate = 5;
      try
      {
      //INIT PORT
      returncode = Hardware.mnInit Comm(ref port, ref BaudRate, ref buffer); //5 is baudrate 9600
      returncode = Hardware.mnSetB aud(ref port, ref BaudRate);
      returncode = Hardware.mnPoll Devices(ref flag,ref port,ref buffer, ref message,ref device_Count);
      }
      catch(Exception ex) {
      MessageBox.Show (ex.ToString()) ;
      }
      [/code]
      Has anybody ever ran into this before?

      Comment

      • RobLefevre
        New Member
        • Jun 2008
        • 4

        #4
        OMG! I am a moron!

        I found the answer.
        Arrays are already passed by referance in C#, so I don't need to pass by referance when I use it.

        Thank you.

        Comment

        Working...