Search:
Namespaces
Discussions
.NET v1.1
Feedback
C# and Byte Stream
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.framework.interop
.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...
Jeff
Hello,
I have a socket connection to an unmanged application. Application
messages arrive as a stream of bytes that get added to a byte array.
The structures that define these messages are defined in a C/C++ header
file.
I would like to use managed C++ to expose a Message object to a C#
application.
For example:
byte[] rawMsg =
Socket
.Read( );
//Call Manged C++ constructor
AppMessage appMsg = new AppMessage(rawMsg);
int value = appMsg.GetValue( );
---------------------------------------------
Does anyone know where I could find code snips and/or
articles that would get me started?
Thanks, Jeff
Reply to this message...
Peter Huang (VIP)
Hi Jeff,
Why not redefine the C++ structure in the C# which will save a lot of job
to do the interop.
Also here is a sample you may take a look.
[MCPP]
typedef struct TestSC
{
int i;
int j;
}SCC;
using namespace System;
using namespace System::Runtime::InteropServices;
namespace TestMCPPMC
{
public __gc class TestClass
{
public:
int i;
int j;
TestClass(Byte bts[])
{
SCC tx;
IntPtr pTs=(IntPtr)&tx;
Marshal::Copy(bts,0,pTs,sizeof(SCC));
i = tx.i;
j = tx.j;
}
};
}
[C#]
[STAThread]
static void Main(string[] args)
{
byte[] bts = new byte[8];
bts[0] = 1;
bts[4] = 1;
TestMCPPMC.TestClass tc = new TestMCPPMC.TestClass(bts);
System.Diagnostics.
Debug
.WriteLine(tc.i);
System.Diagnostics.
Debug
.WriteLine(tc.j);
}
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Reply to this message...
Jeff
Hi Peter,
Your MCPP sample code seems to work well for me.
Thanks again for your help,
Jeff
Reply to this message...
Peter Huang (VIP)
Hi Jeff,
I am glad that my suggestion is of help, if you still have any concern,
please feel free to post here.
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Reply to this message...
Jeff
Hi Peter,
I'll try your MC++ sample code.
Thanks again, Jeff
Reply to this message...
System.Diagnostics.Debug
System.IntPtr
System.Net.Sockets.Socket
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com