473,789 Members | 2,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(b yte[] packet, int
length) {
short src_port = 0, dst_port = 0;
object[] retval = new object[6];

fixed(byte* packetdata = packet) {
CCommon.IPHeade r* ipheader = (CCommon.IPHead er*) 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::Par seIPHeader(Byte packet[], int length)[] {
short src_port = 0, dst_port = 0;
System::Text::S tringBuilder *src_address, *dst_address;
Object *retval[] = new Object*[6];
src_address = new System::Text::S tringBuilder();
dst_address = new System::Text::S tringBuilder();

Byte *packeddata = __try_cast< Byte *>(&packet[0]) ;
CCommon::IPHead er *ipheader = (CCommon::IPHea der *) &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 1168
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(La youtKind::Expli cit)]
__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(b yte[] packet, int length)
{
short src_port = 0, dst_port = 0;
object[] retval = new object[6];

fixed(byte* packetdata = packet) {
CCommon.IPHeade r* ipheader = (CCommon.IPHead er*) 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::Par seIPHeader(Byte packet[], int length)[] {
short src_port = 0, dst_port = 0;
System::Text::S tringBuilder *src_address, *dst_address;
Object *retval[] = new Object*[6];
src_address = new System::Text::S tringBuilder();
dst_address = new System::Text::S tringBuilder();

Byte *packeddata = __try_cast< Byte *>(&packet[0]) ;
CCommon::IPHead er *ipheader = (CCommon::IPHea der *) &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******** *******@TK2MSFT NGP10.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_cas t to, or use
Marshal::PtrToS tructure 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
1633
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, sometimes, the application stop working. In the moment that application fails, the only way to get it working again is to restart WebSphere server or JDBC pool.
38
3368
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 or is it a benefit that C knows nearly nothing (what I can think about is that C is the largest common divisor defined on most available platforms)?
8
27466
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
1855
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, but I still get some errors when i try to compile it: stack.c: In function 'reverseFile'
0
316
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 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#...
5
1641
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 another part of my code , I retrieved and used Bar as follows: .... const Bar* temp = NULL ;
2
1986
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 Safari and Opera. I am doing a lot of dragdrop experimentation and in some situations need a position reporting function. The function doesn't need to report the positions of exotic elements like images in button elements; however, I would like a...
4
2310
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 directory where the file is before the move. I would chmod /tmp itself but wouldn't that be a big security risk? is there another solution? Thanks for any help you can provide...
8
4091
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 homework, BTW): Given a string containing a space-separated list of names: names = "freddy fred bill jock kevin andrew kevin kevin jock" produce a frequency table of names, sorted descending by frequency. then ascending byname. For...
3
2330
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
9659
marktang
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...
0
10190
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10134
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9977
tracyyun
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...
0
5413
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...
0
5545
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4084
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
2
3692
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2903
bsmnconsultancy
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...

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.