473,324 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Little Problem

Hi folks! I'm having a little problem and I'm a bit confused about it.
See... I'm working on this VoIP project (I need to use make direct calls to
Windows API). I started working using C# but since I've got to access the
API constantly I'd rather write all low-level stuff in C++ and create the UI
using C#. So far so good... I created a sniffer socket (in order to
determine if it's H.323 compliant). This routine was written firstly in C#
but since I want to have all low level stuff in C++ I ported it to. Ok... My
problem is the next...

I have somthing like this (This code works OK)...

protected virtual unsafe object[] ParseIPHeader(byte[] packet, int
length) {
short src_port = 0, dst_port = 0;
object[] retval = new object[6];

fixed(byte* packetdata = packet) {
CCommon.IPHeader* ipheader = (CCommon.IPHeader*) packetdata;
// Rest of code goes here...
}

Here IPHeader is a struct I use to store IP packet's header... Then I have
the new C++ code...

Object* CRawSocket::ParseIPHeader(Byte packet[], int length)[] {
short src_port = 0, dst_port = 0;
System::Text::StringBuilder *src_address, *dst_address;
Object *retval[] = new Object*[6];
src_address = new System::Text::StringBuilder();
dst_address = new System::Text::StringBuilder();

Byte *packeddata = __try_cast< Byte *>(&packet[0]) ;
CCommon::IPHeader *ipheader = (CCommon::IPHeader *) &packeddata;
// Rest of code goes here...
}

My problem is that I don't get the same results even though it's almost the
same code :s

As we all know in C# we pass "addresses" so there's no need to write

fixed(byte* packetdata = &packet) // wrong!

but in the C++ world I need to pass the address

I've tried this...

Byte* packeddata = packet; // I get error C2440: 'initializing' : cannot
convert from 'unsigned char __gc[]' to 'unsigned char __gc *'
Byte *packeddata = __try_cast< Byte *>(&packet[0]); // This compiles OK
but it doesn't get the address I need :but the char value instead :s

I need some help with this... Any Ideas?

Thanks in advance
Byte *packeddata = __try_cast< Byte *>(&packet[0]) ;



Nov 17 '05 #1
3 1151
fixed(byte* packetdata = &packet) // wrong!

but in the C++ world I need to pass the address

I've tried this...


I'm not entirly sure what you're trying to do but instead of c#'s fixed
what you'll probably want to do is use __pin something like

int __pin* p = &packet[0];

to get the address of packet and also pin the entire array

hth
--
Ben
http://bschwehn.de
Nov 17 '05 #2
Hi there... That's not the problem...

I've got a code written in C# and works fine but I call a lot of API
functions so I decided to write the low-level stuff in C++. My problem is
(not about __pin cuz I already try that). I have a struct which is
responsible for storing packet's IP Header. It's defined like this...

[StructLayout(LayoutKind::Explicit)]
__value struct IPHeader {
[FieldOffset(0)] Byte verlen; // IP Version & IP Header
length
[FieldOffset(1)] Byte tos; // Type of service
[FieldOffset(2)] UInt16 length; // Packet's total length
[FieldOffset(4)] UInt16 id; // Unique identifier
[FieldOffset(6)] UInt16 offset; // Flags & Offset
[FieldOffset(8)] Byte ttl; // Time To Live
[FieldOffset(9)] Byte protocol; // Protocol (TCP, UDP, any
other)
[FieldOffset(10)] UInt16 checksum; // IP Header checksum
[FieldOffset(12)] Int64 source; // Source address
[FieldOffset(16)] Int64 destination; // Destination address
};

What I want to do (like I did in C# code) is... Fill all of the structs
fields by using a pointer... In C# I have this code

protected virtual unsafe object[] ParseIPHeader(byte[] packet, int length)
{
short src_port = 0, dst_port = 0;
object[] retval = new object[6];

fixed(byte* packetdata = packet) {
CCommon.IPHeader* ipheader = (CCommon.IPHeader*) packetdata;
// Rest of code goes here...
}

As you can see I fix my pointer and it's initialized with packet's address.
In C# I don't have to use the & operator for an operation like the one shown
above. My problem is that... I want to get the same result in C++.

Object* CRawSocket::ParseIPHeader(Byte packet[], int length)[] {
short src_port = 0, dst_port = 0;
System::Text::StringBuilder *src_address, *dst_address;
Object *retval[] = new Object*[6];
src_address = new System::Text::StringBuilder();
dst_address = new System::Text::StringBuilder();

Byte *packeddata = __try_cast< Byte *>(&packet[0]) ;
CCommon::IPHeader *ipheader = (CCommon::IPHeader *) &packeddata;
// Rest of code goes here...
}

I open two instances of VS Debugger (C# & C++ projects). The ipheader points
to packeddata which in turn points to the first element of the array
packet. This works perfectly in C# but it doesn't seem to work in C++. When
I compare both structures (C# & C++) the information contaned in them is
totally different when the data received from the socket is the same.
TIA
"Ben Schwehn" <b.*******@gmx.net> escribió en el mensaje
news:%2***************@TK2MSFTNGP10.phx.gbl...
fixed(byte* packetdata = &packet) // wrong!

but in the C++ world I need to pass the address

I've tried this...


I'm not entirly sure what you're trying to do but instead of c#'s fixed
what you'll probably want to do is use __pin something like

int __pin* p = &packet[0];

to get the address of packet and also pin the entire array

hth
--
Ben
http://bschwehn.de

Nov 17 '05 #3
I would either use __pin to get an raw pointer and then use an unmanaged
native c++ struct (not a __value class) to reinterpret_cast to, or use
Marshal::PtrToStructure to get a managed struct. Don't know if this
helps you at all, sorry if it does not.
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ClimberBear | last post by:
Hi, I've got a very strange problem with a Websphere 5.1 cluster attached to DB2 database in Mainframe z/OS. I have a J2EE deployed application running normally fine agains the DB2 host. But,...
38
by: Martin Marcher | last post by:
Hi, I've read several questions and often the answer was 'C knows nothing about .' So if C knows that little as some people say, what are the benefits, I mean do other languages know more...
8
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
14
by: Henk | last post by:
Hi Guys, (see actual code below) I wrote a little program that is supposed to read a file (input.txt) into memory (using a stack) and then reverse the file and display it to output. It works,...
0
by: Angel J. Hernández M | last post by:
Hi folks! I'm having a little problem and I'm a bit confused about it. See... I'm working on this VoIP project (I need to use make direct calls to Windows API). I started working using C# but since...
5
by: Bit byte | last post by:
I have the following methods: static void Foo::setBar(const Bar*) ; //store a copy of Bar static const Bar* Foo::getBar(void) const ; //return an UNMODIFIABLE ptr to our internal copy In...
2
by: petermichaux | last post by:
Hi, It seems like determining element position in a web page is a difficult task. In the position reporting source code I've looked at there are special fixes for at least some versions of...
4
by: Batmanuel | last post by:
Good evening people, little question here... I'm trying to get this file upload script to work but it tells me that move_uploaded_file() fails because it doesn't have permission for the /tmp...
8
by: Andrew Savige | last post by:
I'm learning Python by reading David Beazley's "Python Essential Reference" book and writing a few toy programs. To get a feel for hashes and sorting, I set myself this little problem today (not...
3
by: Ethan Furman | last post by:
len wrote: I've never had the (mis?)fortune to work with COBOL -- what are the files like? Fixed format, or something like a dBase III style? I
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.