.NETGURU
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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification