I am a total newb at .net, and I have not been able to search out a best
practice answer to what must be a common problem.
My app must process binary data from a UDP socket, a MSMQ queue and a file.
In C, the data is in nested structs, with mixed types, floats, ints, char
arrays, int arrays, variable length arrays of structs etc.
My preference would be to access the data in a similar fashion to C, casting
the byte array of received data to the struct I want, and then sending out
the struct when it is time to transmit.
How can I format a struct to be able to mimic legacy structs, especially
with arrays? I see how I can do it for marshalled code, with [ MarshalAs(
UnmanagedType.B yValArray, SizeConst=XX )] attribute, but does that only work
with unmanaged code?
Thanks,
Seth 2 1915
"SethInMI" <Se******@discu ssions.microsof t.comwrote in message
news:CA******** *************** ***********@mic rosoft.com...
>I understand how to at least get started using the Marshal attributes in
defining my struct in C#, but I am still I think missing one step. Does
marshalling work if no unmanaged code or memory is accessed?
here is my pseudo code, where myStruct is defined using LayoutSequentia l
and
appropriate Marshalling attributes to mimic the layout of legacy C struct.
Byte[] RawData = sock.Receive(re f RawAddr);
If (RawData.Length == Marshal.SizeOf( myStruct) {
Convert RawData into myStruct (using marshalling? just a cast?)
}
To convert RawData into myStruct, you would do something like:
IntPtr adr = Marshal.UnsafeA ddrOfPinnedArra yElement(RawDat a, 0);
MyStruct myStruct = Marshal.PtrToSt ructure(adr, typeof(MyStruct ));
When you use the LayoutSequentia l and Marshaling attributes, you aren't
actually changing the structure, you're just providing instructions that are
used when marshaling the structure to/from unmanaged bytes.
Thanks John.
You mentioned UnsafeAddrOfPin nedArrayElement in your first post, I just
didn't pick up how to use it.
"John Vottero" wrote:
"SethInMI" <Se******@discu ssions.microsof t.comwrote in message
news:CA******** *************** ***********@mic rosoft.com...
I understand how to at least get started using the Marshal attributes in
defining my struct in C#, but I am still I think missing one step. Does
marshalling work if no unmanaged code or memory is accessed?
here is my pseudo code, where myStruct is defined using LayoutSequentia l
and
appropriate Marshalling attributes to mimic the layout of legacy C struct.
Byte[] RawData = sock.Receive(re f RawAddr);
If (RawData.Length == Marshal.SizeOf( myStruct) {
Convert RawData into myStruct (using marshalling? just a cast?)
}
To convert RawData into myStruct, you would do something like:
IntPtr adr = Marshal.UnsafeA ddrOfPinnedArra yElement(RawDat a, 0);
MyStruct myStruct = Marshal.PtrToSt ructure(adr, typeof(MyStruct ));
When you use the LayoutSequentia l and Marshaling attributes, you aren't
actually changing the structure, you're just providing instructions that are
used when marshaling the structure to/from unmanaged bytes.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Keith MacDonald |
last post by:
Hello,
Is there a portable (at least for VC.Net and g++) method to convert text
between
wchar_t and char, using the standard library? I may have missed something
obvious, but the section on codecvt, in Josuttis' "The Standard C++
Library", did not help, and I'm still awaiting delivery of Langer's
"Standard C++ IOStreams and Locales".
Thanks,
|
by: codymanix |
last post by:
Last night I had several thought about RAII and want to discuss a bit.
Why doesn't CSharp support destructors in structs? Wouldn't that make RAII
possible like in C++? When the struct goes out of scope, the dtor could be
immediately be called (no GC needed).
For that, you don't have to declare the whole File class as a struct (which
would be not good for performance when File has a lot of data-members).
Instead creating a thin wrapper...
|
by: masood.iqbal |
last post by:
In this day and age, you never say no to any work that is thrown at you
---- so when I was offered this short-term contract to convert legacy C
code to C++, I did not say no. Personally I believed that it was a
somewhat futile exercise since one of the main requirements was for the
existing API (a functional interface written in C) to remain the same.
I would have much rathered that the mandate be ab-initio, but that was
not the case...
|
by: Philip Townsend |
last post by:
I have a large "legacy" web application written in asp.old that relied
on some COM objects. While I am aware that it is possible to use legacy
COM objects within an asp.net application, is it possible to do the
reverse? In other words, would it be possible to rewrite some of the
legacy COM objects in .NET (provided all the methods & properties have
the same definitions) and integrate it into the asp.old application? If
so, are registration...
|
by: FS Liu |
last post by:
Hi,
I am writing ATL Service application (XML Web service) in
VS.NET C++.
Are there any sample programs that accept XML as input and
XML as output
in the web service?
Thank you very much.
| |
by: genc_ymeri |
last post by:
Hi,
Well, I'm looking around for another opinion. We have two webservers, the
legacy one writen in ASP and the new one in ASP.Net.
Once a user logs in the ASP.Net web app, the session of the Login page gets
stored in a DB (in a binary format). Meantime, if the user chooses to go and
run the web legacy system, he doesn't want to log in again, so an "option"
we are considering is to go and get the data from session of Login Page (of...
|
by: Bit Byter |
last post by:
I am hacking some legacy code and have put together a simple test to
test some hashing funcs I've written. I now want to do a simplistic
timing between the various structs.
Here's a snippet:
struct item_{
char key;
char data;
};
|
by: sherifffruitfly |
last post by:
Hi,
I'm just learning cpp, and the exercise I'm working on is basically as
follows:
1) Create a struct type with 4 members (char, char, char, int).
2) Create an array of, say 3 instances of the struct, and populate them
with data.
3) cin 1, 2, 3, or 4 from the user
4) If the user selected, say, 2, display the contents of the 2nd data
|
by: Bit Byte |
last post by:
I have the following struct:
typedef struct
{
string symbol;
string synonym;
Synonym(string _synonym, string _symbol) {
synonym = _synonym;
symbol = _symbol;
}
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |