Pass a structure by reference to C++ dll in .Net Compact Framework

M

Maloney

I'm trying to pass a structer from C# to a C dll in .Net Compact Framework

This partially works. If in the GetStatus I only set -- status->fileLength
= 10; -- and not the second variable then filelength = 10 and fileposition =
0, but if i set the second one only or both of them then they are fileLength
= 47244640266 and filePosition = 0; Like the memory is getting over wrote.
What is the correct way of passing a structure by reference on the Compact
Framework.

here is my code

In the C dll
-------------------------------------------

----------------in the header
struct FileStatus
{
long fileLength;
long filePosition;
};

extern "C" __declspec(dllexport) bool GetStatus (FileStatus* status);
----------------

---------------in the cpp
bool GetStatus (FileStatus* status)
{
status->fileLength = 10;
status->filePosition = 11;
return true;
}
--------------------------------------------

int my C# code i have
--------------------------------------------
namespace Controls
{
[StructLayout(LayoutKind.Sequential)]
public struct SStatus
{
public long fileLength;
public long filePosition;
}
public class VitoSoundExplorer
{

[DllImport("TestDll.dll", EntryPoint="GetStatus")]
public static extern bool GetStatus(ref SStatus status);


public bool GetStatus()
{
SStatus st= new SStatus();
GetStatus(ref st);
return true;
}
}
}
 
L

Lloyd Dupont

since when a long in C is 8 bytes long ?
maybe on windows 64, but certainly not on PPC, or so I assume....

I would rather write:
[StructLayout(LayoutKind.Sequential)]
public struct SStatus
{
public int fileLength;
public int filePosition;
}
 
L

Lloyd Dupont

FYI:
here is the whole story, I believe:
C:
char: 1 bytes
short: 2 bytes
long: 4 bytes
long long (gcc): 8 bytes
int: size of register (4 on PPC)

C#:
int: 4 bytes
long: 8 bytes
see ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top