I have a C ++ dll that provides 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 works successfully. When the C # .exe application is launched, the function func()is called successfully, and it can fill in the fields successfully inside the dll. But when the function returns to the application of C #, the variable mynameis still encodes zero values for structure fields ( firstnameand lastname).
Please suggest changes so that I can fill in the values of the fields myname(so that after the function completes, the func()variable myname->firstnamecontains "rakesh" and myname->lastnamecontains "agarwal".
Note: StringBuilder cannot be used inside a structure.
Rakesh Agarwal