pointer to pointer(C style) to int array(C# style)?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • profactory
    New Member
    • Nov 2009
    • 2

    pointer to pointer(C style) to int array(C# style)?

    Hi all,

    I have to include a C style DLL into C#. The dll has a function which should give me back a string, an array of integer and a single integer.
    The DllImport looks like this:
    Code:
    [DllImport("key2.dll", EntryPoint = "#4")]
    public static extern bool measureKey(ref StringBuilder msg, int[] Params,byte[] Result,ref int rSize);
    The C# source to call the function:
    Code:
    int[] parameter = new int[5];
    parameter[0] = 5300;
    parameter[1] = 4700;
    parameter[2] = 4000;
    parameter[3] = 0;
    parameter[4] = 80;
    StringBuilder msg = new StringBuilder(128);
    int rSize = 0;
    int[] result = new int[7];
    [B]
    bool value = measureKey(ref msg, parameter, ref result, ref rSize);[/B]
    The value, the msg and the rSize is correct.
    Result should contain 7 values greater than 0, but I only get the first one, and the size of the array also changed from 7 to 1.
    The first value of result is correct.

    I also got the function in C style, here you have to use a pointer to pointer to get the values:
    Code:
    int * Result;
    int rSize;
    char * Msg;
    int Params[EXPECTEDVALUES];
    retval = measureKey(&Msg, Params, &Result, &rSize);
    if (!retval) {[INDENT]printf("Error: ");
    printf(Msg);[/INDENT]
    }else{[INDENT]printf("Success: ");
    for(y = 0; y < rSize; y++){[/INDENT][INDENT][INDENT]printf("%d  ",Result[y]);[/INDENT][/INDENT][INDENT]}[/INDENT]}
    printf(Msg);
    free(Result);
    }
    Can anybody help me how to get the other 6 values of result?

    Thanks!
    Greetings!
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      I'm not sure about this but hopefully this helps...

      I think a C# array is different than a C/C++ array when it comes to pointers; however, you can still access pointers in C#, you just need to use "unsafe" code. Here's some info I found via google.

      Discover the cutting-edge technology and expert services offered by CIS - Cyber Infrastructure, CIS, Learn more about our Company and our innovative digital solutions.


      Since rSize comes out correctly, you may need to use the alternative pointer notation... it's been a while since I've done this, but I think it's something like...

      Code:
      ...
      for (byte i = 0; i < rSize; i++)
      {
        int val = *(result + i);
        ...
      }
      But you might want to look into that. For example, I'm not sure if i should be a byte or an int... memory fuzzy... ack! ;) As I said, it's been a while. Hopefully this is more current for you and you know what the heck I'm talking about.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well your dllimport lists result as a byte[] but you send in a reference to an int[]
        int is 32bits, byte is 8. Try correcting that.
        Although I would suspect you have to do more fiddling, possibly using IntPtr instead.

        Comment

        • profactory
          New Member
          • Nov 2009
          • 2

          #5
          Hi all!


          first of all thanks for the tips!

          I've found a solution by using the unsafe method.

          for everyone, who's interested:

          Code:
                  [DllImport("key2.dll", EntryPoint = "#4")]
                  unsafe public static extern bool measureKey(ref StringBuilder msg, int[] Params,int** Result,int* rSize);
          
                      int[] result = new int[7];
                      unsafe
                      {
                          int* ptrResult = stackalloc int[7];
          
                          bool value = measureKey(ref msg, parameter, &ptrResult, &rSize);
          
                          for (int i = 0; i < rSize; i++)
                          {
                              result[i] = *ptrResult;
                              ptrResult++;
                          }
          
                      }
          Best regards!

          Comment

          Working...