I have a c++ dll which exposes the following function
long func(struct name * myname)
{
strcpy(myname->firstname,"rakesh");
strcpy(myname->lastname,"agarwal");
return S_OK;
}
struct name
{
char firstname[100];
char lastname[100];
}
I want to call this function from a C# application , so I do the following :
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
unsafe public struct name
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
public string firstname;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public string lastname;
} ;
[DllImport("C++Dll.dll")]
public unsafe static extern long func(name[] myname);
name[] myname = new name[1];
func(myname);
The application builds successfully . When the c# application .exe is run , the function func() is called successfully and it is able to populate the fields successfully inside the dll . But , when the function returns to C# application , the variable 'myname' still conatins null values for the struct fields(firstname and lastname).
Please suggest me changes such that I am able to populate the fields values of 'myname' ( so that after the function 'func' finishes execution , the variable 'myname->firstname' contains "rakesh " and myname->lastname contains "agarwal" .
Note : StringBuilder cannot be used inside the structure .