The address of pointer that pass to COM object is not right (only in x64)
-
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] = 5But in my x64, it is wrong:
arr[0] = 0
arr[1] = -321451
arr[2] = -14537642
arr[3] = 0
arr[4] = 0
arr[5] = 0I'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 :)
-
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] = 5But in my x64, it is wrong:
arr[0] = 0
arr[1] = -321451
arr[2] = -14537642
arr[3] = 0
arr[4] = 0
arr[5] = 0I'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 :)
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) -
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)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...