Click here to Skip to main content
15,893,337 members
Home / Discussions / C#
   

C#

 
GeneralRe: Menue by Marc Clifton Pin
Marc Clifton31-May-03 15:33
mvaMarc Clifton31-May-03 15:33 
GeneralRe: Menue by Marc Clifton Pin
Frenchi1-Jun-03 5:12
Frenchi1-Jun-03 5:12 
Questionto byte[] conversion? Pin
stonee7431-May-03 3:38
stonee7431-May-03 3:38 
AnswerRe: to byte[] conversion? Pin
Eric Gunnerson (msft)31-May-03 16:34
Eric Gunnerson (msft)31-May-03 16:34 
GeneralRe: to byte[] conversion? Pin
stonee7431-May-03 17:01
stonee7431-May-03 17:01 
GeneralRe: to byte[] conversion? Pin
Eric Gunnerson (msft)1-Jun-03 7:05
Eric Gunnerson (msft)1-Jun-03 7:05 
GeneralRe: to byte[] conversion? Pin
stonee741-Jun-03 8:04
stonee741-Jun-03 8:04 
GeneralRe: to byte[] conversion? Pin
Eric Gunnerson (msft)3-Jun-03 9:43
Eric Gunnerson (msft)3-Jun-03 9:43 
I'll try to be clearer:

1) Can I pass void* PacketStart, long PacketSize or Do I need to go the managed way?

You need to get from the the managed world to the pointer world somehow to use the pointer operations. If you pass in a MyStruct (for example), you would do the fixed statement in your routine. If you want to pass in a pointer, you'd have to do the fixed statement in the caller of the routine.

While you'll have to write a separate routine for each struct, I think that writing the managed version and putting all the ugliness in your routine is the rigth

2) Are those two method declarations equivalent?

No, though they are similar. I'm not sure the object version is feasible, though I've never tried it myself.


3) If i pass void*, what would be the substitute for MyStruct?

If you pass a byte* (on reflection, I don't think void* will work), you would need to copy the bytes over one by one to the byte buffer, based on the length you'd pass in.

4) *((object) pBuffer) = PacketStart; ( results in Cannot convert type 'byte*' to 'object')

Object won't work here, because the compiler does't know how big the actual object is.

5) Could you give an example how to go, to receive the respective byte[] in this specific case?

Here's some code I wrote up. It compiles, but I haven't actually tested it:

using System;
using System.IO;
using System.Net.Sockets;


namespace ConsoleApplication7
{
struct MyStruct
{
int i;
float j;
}


///
/// Summary description for Class1.
///

class Class1
{
///
/// The main entry point for the application.
///

[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
}

public unsafe void SendMyStruct(MyStruct myStruct, Socket socket)
{
byte[] buffer = new byte[sizeof(MyStruct)];

fixed (byte* pBuffer = buffer)
{
*((MyStruct*)pBuffer) = myStruct;
}
socket.Send(buffer);
}

public unsafe MyStruct ReadMyStruct(Socket socket)
{
byte[] buffer = new byte[sizeof(MyStruct)];
socket.Receive(buffer);

MyStruct myStruct;
fixed (byte* pBuffer = buffer)
{
myStruct = *((MyStruct*)pBuffer);
}
return myStruct;
}
}
}
GeneralResource Troubles (MissingManifestResourceException) Pin
gek_at31-May-03 2:02
gek_at31-May-03 2:02 
GeneralRe: Resource Troubles (MissingManifestResourceException) Pin
gek_at31-May-03 2:18
gek_at31-May-03 2:18 
GeneralRe: Resource Troubles (MissingManifestResourceException) Pin
David Stone31-May-03 6:23
sitebuilderDavid Stone31-May-03 6:23 
QuestionIf C# is Microsoft's answer to SUN's Java, then is C# Platform independent? Pin
Link260030-May-03 23:56
Link260030-May-03 23:56 
AnswerRe: If C# is Microsoft's answer to SUN's Java, then is C# Platform independent? Pin
Kannan Kalyanaraman31-May-03 0:56
Kannan Kalyanaraman31-May-03 0:56 
AnswerRe: If C# is Microsoft's answer to SUN's Java, then is C# Platform independent? Pin
David Stone31-May-03 6:15
sitebuilderDavid Stone31-May-03 6:15 
AnswerRe: If C# is Microsoft's answer to SUN's Java, then is C# Platform independent? Pin
FruitBatInShades31-May-03 23:57
FruitBatInShades31-May-03 23:57 
GeneralRe: If C# is Microsoft's answer to SUN's Java, then is C# Platform independent? Pin
Link26001-Jun-03 6:09
Link26001-Jun-03 6:09 
QuestionHow do I draw graphics on the StatusBar ? Pin
JeffSayHi30-May-03 21:27
JeffSayHi30-May-03 21:27 
AnswerRe: How do I draw graphics on the StatusBar ? Pin
James T. Johnson31-May-03 18:16
James T. Johnson31-May-03 18:16 
AnswerRe: How do I draw graphics on the StatusBar ? Pin
Manster2-Jun-03 9:45
Manster2-Jun-03 9:45 
GeneralHello Pin
Hussein Zahran30-May-03 8:04
Hussein Zahran30-May-03 8:04 
GeneralRe: Hello Pin
Jon Newman30-May-03 8:41
Jon Newman30-May-03 8:41 
GeneralRe: Hello Pin
Paul Watson31-May-03 4:11
sitebuilderPaul Watson31-May-03 4:11 
Generalconst vs static readonly Fields Pin
Nathan Blomquist30-May-03 6:46
Nathan Blomquist30-May-03 6:46 
GeneralRe: const vs static readonly Fields Pin
Eric Gunnerson (msft)30-May-03 13:19
Eric Gunnerson (msft)30-May-03 13:19 
GeneralApplication & Window positions Pin
BenjaminAlicea30-May-03 5:46
BenjaminAlicea30-May-03 5:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.