Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4324182
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:05:52+00:00 2026-05-21T09:05:52+00:00

I currently have a .NET program initiating a connection to a server. Sometimes I

  • 0

I currently have a .NET program initiating a connection to a server. Sometimes I need to call a special unmanaged C++ code, which uses the connection to the server.

How to pass and use socket connection from .NET in unmanaged C++ code?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:05:52+00:00Added an answer on May 21, 2026 at 9:05 am

    The Socket class has the Handle property, which could be used.

    Socket.Handle @ MSDN

    I was skeptical about whether this would work, but I was able to get it to work with no fuss at all.

    To start, I made an unmanaged C++ dll to export a single function that can do something with a socket. Here’s the function I created.

    #include <WinSock.h>
    
    // This is an example of an exported function.
    extern "C" __declspec(dllexport) void __stdcall DoStuffWithSocket(DWORD sock)
    {
      const char *data = "woot\r\n";
      send((SOCKET)sock, data, strlen(data), 0);
    }
    

    The project outputs a dll named UnmanagedSocketHandler.dll, which is the library mentioned in the P/Invoke signature in the next snippet.

    Here’s a quick and dirty C# console app to call that function as a Server.

    using System.Net;
    using System.Net.Sockets;
    using System.Runtime.InteropServices;
    
    namespace SocketHandleShareTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                IPEndPoint ep = new IPEndPoint(IPAddress.Any, 5353);
                Socket sListen = new Socket(AddressFamily.InterNetwork, 
                                            SocketType.Stream, ProtocolType.Tcp);
                sListen.Bind(ep);
                sListen.Listen(10);
                Socket sClient = sListen.Accept();
                Console.WriteLine("DoStuffWithSocket() enter");
                Console.ReadLine();
                DoStuffWithSocket(sClient.Handle);
                Console.WriteLine("DoStuffWithSocket() exit");
                Console.ReadLine();
                sClient.Close();
                sListen.Close();
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
    
            [DllImport("UnmanagedSocketHandler.dll")]
            static extern void DoStuffWithSocket(IntPtr sock);
        }
    }    
    

    Last, a quick and dirty C# client app to talk to the server. I was unable to find any documentation on why this works, but it works. I’d be wary about what you try to do with the socket.

    using System.Net;
    using System.Net.Sockets;
    
    namespace SocketHandleShareTestClient
    {
        class Program
        {
            static void Main(string[] args)
            {
                byte[] buf = new byte[40];
                Socket s = new Socket(AddressFamily.InterNetwork,
                                      SocketType.Stream, ProtocolType.IP);
                s.Connect("localhost", 5353);
                int len = s.Receive(buf);
                Console.WriteLine("{0} bytes read.", len);
                if (len > 0)
                {
                    string data = Encoding.ASCII.GetString(buf, 0, len);
                    Console.WriteLine(data);
                }
                s.Close();
                Console.ReadLine();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to execute a schema file on a SQL Server machine. I currently
I am fairly new to the .NET programming and I am currently developing a
I'm approaching the end of a C#/ASP.NET project I am developing, and I'll have
I am currently working on one of my college project which requires the implementation
I am writing a program that must communicate with a PHP / web based
I have a Access database that I am using for a back-end to my
I am currently in the thinking process of the data structure I might use
I'm writing a serial port software for Windows. To improve performance I'm trying to
So I recently updated my application to support a new feature. In the past

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.