F
Frank
Hello,
i have a problem with the data exchange between a managed C# application and
an unmanged C++ DLL. The C# app fills some values of a structure and calls
the C++ DLL. This DLL reads the filled data from the structure and fills the
results into the same structure. This code above works, but sometimes the
data array is empty, although the GetMemory function returns true. For
example: i read 250kByte (~1000 function calls) 5 - 15 times the data array
is empty.
The DLL works in some other non .NET applications perfectly. So i think the
error must be in the C# application.
My code:
Definition of the structure in C#:
[StructLayout(LayoutKind.Sequential)] public class MemoryObject {
public volatile uint Address;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=256)] public volatile
byte[] Data;
public volatile byte Length;
}
DLL-Import:
[DllImport("bus.dll", EntryPoint="GetMemory"
,ExactSpelling=false,CallingConvention=CallingConvention.Cdecl)] static
extern bool GetMemory(uint DeviceNr, [In,Out] MemoryObject Memory);
Function call:
public bool GetXMemory(uint DeviceNr, uint Address, byte Length, ref byte[]
Data) {
MemoryObject MemData = new MemoryObject();
bool RetVal;
MemData.Data = new byte[256];
MemData.Address = Address;
MemData.Length = Length;
RetVal = GetMemory(DeviceNr, MemData);
System.Array.Copy(MemData.Data, 0, Data, 0, Length);
return RetVal;
}
i have a problem with the data exchange between a managed C# application and
an unmanged C++ DLL. The C# app fills some values of a structure and calls
the C++ DLL. This DLL reads the filled data from the structure and fills the
results into the same structure. This code above works, but sometimes the
data array is empty, although the GetMemory function returns true. For
example: i read 250kByte (~1000 function calls) 5 - 15 times the data array
is empty.
The DLL works in some other non .NET applications perfectly. So i think the
error must be in the C# application.
My code:
Definition of the structure in C#:
[StructLayout(LayoutKind.Sequential)] public class MemoryObject {
public volatile uint Address;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=256)] public volatile
byte[] Data;
public volatile byte Length;
}
DLL-Import:
[DllImport("bus.dll", EntryPoint="GetMemory"
,ExactSpelling=false,CallingConvention=CallingConvention.Cdecl)] static
extern bool GetMemory(uint DeviceNr, [In,Out] MemoryObject Memory);
Function call:
public bool GetXMemory(uint DeviceNr, uint Address, byte Length, ref byte[]
Data) {
MemoryObject MemData = new MemoryObject();
bool RetVal;
MemData.Data = new byte[256];
MemData.Address = Address;
MemData.Length = Length;
RetVal = GetMemory(DeviceNr, MemData);
System.Array.Copy(MemData.Data, 0, Data, 0, Length);
return RetVal;
}