EDDYKT
asked on
array of structure and interop
I add the COM object into my c# program.
The COM object is written as c++ and i have no control on this
One of the function is defined in c++ is
setVariable(int count, STRUCT *p);
where STRUCT is typedef to structure and allow to pass an array of the STRUCT *
However, when i import to my c# program,
The prototype is shown as
setVariable(System.Int32 count , ref STRUCT p);
In c#, i declare the
STRUCT [] p = new STRUCT[10];
int count = 10;
Here is my question:
How do i can pass the array to the function?
i tried
setVariable(count , ref p[0]);
but in c++, it only see the first one
The COM object is written as c++ and i have no control on this
One of the function is defined in c++ is
setVariable(int count, STRUCT *p);
where STRUCT is typedef to structure and allow to pass an array of the STRUCT *
However, when i import to my c# program,
The prototype is shown as
setVariable(System.Int32 count , ref STRUCT p);
In c#, i declare the
STRUCT [] p = new STRUCT[10];
int count = 10;
Here is my question:
How do i can pass the array to the function?
i tried
setVariable(count , ref p[0]);
but in c++, it only see the first one
Yes, since the expected input is just one object and not array. Also you are passing p[0] so the c++ sees only the first one.
Try
setVariable(count , &p);
setVariable(count , &p);
ASKER
>>Yes, since the expected input is just one object and not array. Also you are passing p[0] so the c++ sees only the first one.
Yes, I ran out of idea. Don't know why after add reference the prototype shows that what i don't expect.
>>setVariable(count , &p);
failed on compile, it requires ref in front
Yes, I ran out of idea. Don't know why after add reference the prototype shows that what i don't expect.
>>setVariable(count , &p);
failed on compile, it requires ref in front
Couple of points to consider:
1. You ned to declare the struct as an out parameter if the c++ function is going to modify the variable.
2. You are passing managed memory to unmanaged code, so it needs to be marshalled.
one suggestion would be;
const int TotalBytesInStruct = 22;
[StructLayout( LayoutKind.Sequential, CharSet = CharSet.Unicode, Size = TotalBytesInStruct ), Serializable]
public struct MyStruct
{
[MarshalAs( UnmanagedType.ByValTStr, SizeConst = 20 )]
public string var1;
public int var2;
}
MyStruct p;
setVariable( count, out ref p );
1. You ned to declare the struct as an out parameter if the c++ function is going to modify the variable.
2. You are passing managed memory to unmanaged code, so it needs to be marshalled.
one suggestion would be;
const int TotalBytesInStruct = 22;
[StructLayout( LayoutKind.Sequential, CharSet = CharSet.Unicode, Size = TotalBytesInStruct ), Serializable]
public struct MyStruct
{
[MarshalAs( UnmanagedType.ByValTStr, SizeConst = 20 )]
public string var1;
public int var2;
}
MyStruct p;
setVariable( count, out ref p );
ASKER
I know i need to marshal. However it is a COM object and is not native dll
The structure already defined in the object browser when i perform add reference.
How can i apply the above if it is COM object?
Again the prototype shown on object browser is
setVariable(System.Int32 count , ref STRUCT p);
how can i use setVariable( count, out ref p ); ?
SetVariable is the function inside the c++ class not native dll
The structure already defined in the object browser when i perform add reference.
How can i apply the above if it is COM object?
Again the prototype shown on object browser is
setVariable(System.Int32 count , ref STRUCT p);
how can i use setVariable( count, out ref p ); ?
SetVariable is the function inside the c++ class not native dll
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I finally use unmanage c++
ASKER
In COM object, there are more than 1 classes inside and the function I posted is the function inside one of the class