Hi
I have created a DLL in C# and using the same in VC++ using COM interop. However i need to pass an array of user defined structures to VC++. The signature of the IDL File shows the return type of an array element as SAFEARRAY. I am able to get the elements from the SAFEARRAY but not able to get the exact values. Am i missing something
Please help
VC++ code
hr = CoInitialize(NULL);
ReaderXML::ISampleXMLSerializeDemoPtr
xmlSerializerptr(__uuidof(SampleXMLSerializerDemo));
IEmployeePtr emp(__uuidof(Employee));
struct IEmployee *_result;
if (SUCCEEDED(hr))
{
HRESULT hr= xmlSerializerptr->ConvertXMLToEmployee();
if(SUCCEEDED(hr))
xmlSerializerptr->raw_GetEmployeeDetails(&_result); //_result will have the Employee object
//returned from C# dll
}
long lIndex =0;
long pinCode=0;
SAFEARRAY *pSa;
pSa= _result->GetEmpAddress(); //GetEmpAddress returns an array of Address objects
Address HUGEP *pAddressList= NULL;
//Problem in the following part .. i am getting value for pinCode but not what is being received from the dll
hr = SafeArrayAccessData(pSa,(void HUGEP**) &pAddressList);
if(hr == S_OK)
{
for (lIndex =0; lIndex < pSa->rgsabound[0].cElements; lIndex++ )
{
pinCode = (long)pAddressList[lIndex].PinCode; //Pincode is something like 17629104 instead of 100
}
}
SafeArrayUnaccessData(pSa);
//C# Code
public interface IAddress
{
int PinCode { get; set; }
}
[ClassInterface(ClassInterfaceType.None)]
[StructLayout(LayoutKind.Sequential)]
[Serializable]
public class Address:IAddress //ADDRESS CLASS
{
private int pinCode;
public int PinCode
{
get { return pinCode; }
set { pinCode = value; }
}
public Address()
{
}
}
public interface IEmployee
{
string Name { get; set; }
Address[] EmpAddress { get; set; }
int Age { get;set; }
}
[ClassInterface(ClassInterfaceType.None)]
[Serializable]
public class Employee : IEmployee
{
private string name;
private Address[] address;
private int age;
[System.Xml.Serialization.XmlElementAttribute("EmpAddress")] //this attribute is required for accessing an array
public Address[] EmpAddress
{
get { return this.address ; }
set { this.address = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public Employee()
{
address = new Address[5];
age = 0;
name = "";
}
public Employee(Employee e)
{
this.age = e.Age;
this.name = e.Name;
this.address = e.EmpAddress;
}
}
public class SampleXMLSerializerDemo:ISampleXMLSerializeDemo
{
Employee emp;
public Employee GetEmployeeDetails()
{
return emp;
}
public void ConvertXMLToEmployee()
{
try
{
Employee e = new Employee();
e.Age = 2;
e.Name = "Hello";
Address []addr = new Address[2];
addr[0].PinCode = 100;
addr[1].PinCode = 111;
e.EmpAddress = addr;
emp = e;
}
catch (Exception ex)
{
throw ex;
}
}
public SampleXMLSerializerDemo()
{
}
}
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->