I have an unmanaged dll that contains a function to read a data from a file. I wrote a CLI wrapper for it.
To test whether it is working, I wrote a simple CLI program calling the wrapper class and the data read method. It worked fine.
I used that dll in a c# program but the data is not read properly. It reads but the data read is not proper.
CLI function:
void FileReader::ReadFile()
{
int ret;
STRUCT head;
STRUCT1 *sqar;
memset ( ( void * )&head, 0, sizeof ( STRUCT) );
sqar = ( STRUCT1 * )NULL;
ret = Read_func( "somefile.someformat", &head, &sqar );
}
CLI EXE:
int main(array<System::String ^> ^args)
{
FileReader reader;
reader.ReadFile();
Console::WriteLine(L"Hello World");
return 0;
}
This works fine. When I debug, the code flows through and reads the data properly.
C# code:
{
FileReader filereader= new FileReader ();
filereader.ReadFile();
}
When I import the CLI dll in the C# project and access the code as above the data in not read properly.
STRUCT has int as members and it is read properly.STRUCT1 has int, double and structure pointers as members which is not read properly. What could be possibly wrong?
The solution was to change the
Struct Member Alignment propertyunderC/C++ > Code Generationfrom 1byte1 byte /Zp1toDefault. Simple.