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=CallingConv ention.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;
} 2 1739
Frank,
A few things. I would first change the MemoryObject from a class to a
Struct. I imagine that you declare it as a class so that you can pass a
null pointer to the function at times.
Instead, I would declare the method to take an IntPtr, and then perform
the marshaling of the structure to/from unmanaged memory on your own (it
only takes a call to the static PtrToStructure and StructureToPtr methods on
the Marshal class).
Also, could it possibly be something in the GetMemory function itself
which is causing the problem?
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Frank" <fr**********@nospam.de> wrote in message
news:uS**************@TK2MSFTNGP10.phx.gbl... 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=CallingConv ention.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; }
Hello Nicholas,
thanks for your answer.
In the meantime i found the bug!
The error was the return value bool. It always returns true. A Visual C++
V6.0 (Win32-API) bool type is NOT the same as an .NET bool type. I changed
my function return value to a byte type at everything works.
Frank
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> schrieb
im Newsbeitrag news:uw*************@TK2MSFTNGP11.phx.gbl... Frank,
A few things. I would first change the MemoryObject from a class to a Struct. I imagine that you declare it as a class so that you can pass a null pointer to the function at times.
Instead, I would declare the method to take an IntPtr, and then
perform the marshaling of the structure to/from unmanaged memory on your own (it only takes a call to the static PtrToStructure and StructureToPtr methods
on the Marshal class).
Also, could it possibly be something in the GetMemory function itself which is causing the problem?
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Frank" <fr**********@nospam.de> wrote in message news:uS**************@TK2MSFTNGP10.phx.gbl... 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=CallingConv ention.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; }
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Fritz Bosch |
last post by:
Hi experts
Is is possible to import/manipulate a module such that
I can supply its __dict__?
I want to supply my own dict subclass object to be filled
by the import, e.g. a class like:
>>>...
|
by: ktxn1020 |
last post by:
Hi Experts,
I built a scrip file which cleans up the old log files starting from
the oldest till the necessary free space on the drive reaches.
The script ran well independently using Python's...
|
by: B.Desai |
last post by:
Hello Experts,
Can you please help me out? I am very frustrated!
I am trying to update import specification, and it does not let me do
that. I have saved specification and now I need to add...
|
by: Chanus |
last post by:
Hi experts,
Can anyone tell me how I can automate "Get External Data" command to
import a XML file? I'm looking to build a macro in ACCESS.
Any help will be greatly appreciated.
Thanks again.
|
by: zqhpnp |
last post by:
I build the following code,and generate a library,who can tell me how
to import the function fn() in my applications,thank you very much!
#ifdef _BUILD_DLL
#define DLLFUNCTION...
|
by: Vidya Bhagwath |
last post by:
Hello Experts,
I am porting the C++ code into the Visual C#.NET. My C++ code is
mainly based on the serial communication. So I am using the windows
structure such as DCB.. etc and the windows...
|
by: Martin Unsal |
last post by:
I'm using Python for what is becoming a sizeable project and I'm
already running into problems organizing code and importing packages.
I feel like the Python package system, in particular the...
|
by: narpet |
last post by:
Hello all...
I have a (probably very) easy question for the SQL experts here...
Lets say I have a table with the following rows and data:
Name Nickname Middlename...
|
by: a |
last post by:
Dear friends
I want import data from CSV file to mdb file How can I do that in vb.net?
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |