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. The address of pointer that pass to COM object is not right (only in x64)

The address of pointer that pass to COM object is not right (only in x64)

Scheduled Pinned Locked Moved C#
csharpcomdata-structureshelpquestion
3 Posts 2 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.
  • R Offline
    R Offline
    RYU
    wrote on last edited by
    #1

    Hi, I have a COM object that accept an array of integer.

    HRESULT __stdcall CTestCOM::setValues(int* arr, int arrSize)
    {
        for(int i=0; i<arrSize; i++)
        {
            MyDebugTextOut("C:\\Temp\\TestCOM.txt", "arr[%d] = %d\n", i, arr[i]);
        }
    }
    

    In my C# code:

    int[] arrTest = new int[5];
    for(int i=0; i<5; i++)
    {
        arrTest[i] = i;
    }
    TestCOM testCom = new TestCOM();
    testCOM.setValues(ref arrTest[0]);
    

    The result of my text out in x86 is correct:

    arr[0] = 0
    arr[1] = 1
    arr[2] = 2
    arr[3] = 3
    arr[4] = 4
    arr[5] = 5

    But in my x64, it is wrong:

    arr[0] = 0
    arr[1] = -321451
    arr[2] = -14537642
    arr[3] = 0
    arr[4] = 0
    arr[5] = 0

    I've checked the address of the array, in x86, it pass a same address from C# to COM object. But in x64, address of the array in the COM object is different from C# array. Any idea why? If yes, is it possible to fix it? Thanks :)

    L 1 Reply Last reply
    0
    • R RYU

      Hi, I have a COM object that accept an array of integer.

      HRESULT __stdcall CTestCOM::setValues(int* arr, int arrSize)
      {
          for(int i=0; i<arrSize; i++)
          {
              MyDebugTextOut("C:\\Temp\\TestCOM.txt", "arr[%d] = %d\n", i, arr[i]);
          }
      }
      

      In my C# code:

      int[] arrTest = new int[5];
      for(int i=0; i<5; i++)
      {
          arrTest[i] = i;
      }
      TestCOM testCom = new TestCOM();
      testCOM.setValues(ref arrTest[0]);
      

      The result of my text out in x86 is correct:

      arr[0] = 0
      arr[1] = 1
      arr[2] = 2
      arr[3] = 3
      arr[4] = 4
      arr[5] = 5

      But in my x64, it is wrong:

      arr[0] = 0
      arr[1] = -321451
      arr[2] = -14537642
      arr[3] = 0
      arr[4] = 0
      arr[5] = 0

      I've checked the address of the array, in x86, it pass a same address from C# to COM object. But in x64, address of the array in the COM object is different from C# array. Any idea why? If yes, is it possible to fix it? Thanks :)

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Although I do not know COM interop, there are a lot of things wrong here...

      RYU^^ wrote:

      testCOM.setValues(ref arrTest[0]);

      Why are you passing the reference to the first element in? A .NET array isn't just a int buffer! If you fixed it, then maybe you can use it. (does COM not require you to pass the length parameter too?)

      RYU^^ wrote:

      arr[5] = 5

      Your array length is only 5, so how can you read the 6th element?

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 alpha 4a out now (29 May 2008)

      R 1 Reply Last reply
      0
      • L leppie

        Although I do not know COM interop, there are a lot of things wrong here...

        RYU^^ wrote:

        testCOM.setValues(ref arrTest[0]);

        Why are you passing the reference to the first element in? A .NET array isn't just a int buffer! If you fixed it, then maybe you can use it. (does COM not require you to pass the length parameter too?)

        RYU^^ wrote:

        arr[5] = 5

        Your array length is only 5, so how can you read the 6th element?

        xacc.ide - now with TabsToSpaces support
        IronScheme - 1.0 alpha 4a out now (29 May 2008)

        R Offline
        R Offline
        RYU
        wrote on last edited by
        #3

        Hi Leppie, Thanks for the reply. The reason I pass the first element address because I want to manipulate the value in my COM object. It works fantastically in x86, but doesn't work in x64.

        leppie wrote:

        (does COM not require you to pass the length parameter too?)

        You were right, the length of the array must be passed in to the COM object. But in my case (not in this example), it is actually determine by another attribute that is set previously.

        leppie wrote:

        Your array length is only 5, so how can you read the 6th element?

        Uppppsss... you were right again. It should be new int[6]. Sorry...

        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