Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2004

Tip: Looking for answers? Try searching our database.

C# and Byte Stream

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeff - 02 Sep 2004 15:43 GMT
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
"Peter Huang" - 03 Sep 2004 08:05 GMT
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

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jeff - 07 Sep 2004 11:41 GMT
Hi Peter,

I'll try your MC++ sample code.

Thanks again,  Jeff
Jeff - 08 Sep 2004 14:09 GMT
Hi Peter,

Your MCPP sample code seems to work well for me.

Thanks again for your help,

Jeff
"Peter Huang" - 09 Sep 2004 03:36 GMT
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

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.