473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

getservbyname C# PInvoke

Can anyone verify if this is the correct way to do this? Thanks.

using System;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace WSockImport
{
/// <summary>
/// Summary description for WSock.
/// </summary>
public class WSock
{
// Used to shutdown Winsock
private static WSock sm_WSock = new WSock();
private static int sm_ciInitializeCounter = 0;

public WSock()
{
if ( Interlocked.Increment( ref sm_ciInitializeCounter ) == 1 )
{
Initialize();
}
}

~WSock()
{
if ( Interlocked.Decrement( ref sm_ciInitializeCounter ) == 0 )
{
Shutdown();
}
}

private const int WSADESCRIPTION_LEN = 256;

public static void Initialize()
{
WSADATA WSAD = new WSADATA();
ushort wVersion = MAKEWORD(1,1);
int iVal = WSAStartup( wVersion, ref WSAD );
if ( iVal != 0 )
{
Marshal.ThrowExceptionForHR( WSAGetLastError( ) );
}
}

public static void Shutdown()
{
int iVal = WSACleanup();
if ( iVal != 0 )
{
Marshal.ThrowExceptionForHR( WSAGetLastError( ) );
}
}

public static ushort MAKEWORD(byte LoByte,byte HiByte)
{
return (ushort)(LoByte + (HiByte << 8) );
}

public static uint MAKEDWORD(ushort LoWord,ushort HiWord)
{
return (uint)(LoWord + (HiWord << 16));
}

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct servent
{
public string s_name;
public IntPtr s_aliases;
public short s_port;
public string s_proto;
}

public static servent GetServByName( string strName, string strProto
)
{
IntPtr ptr = getservbyname( strName, strProto );
if ( ptr != IntPtr.Zero )
{
servent s = (servent)Marshal.PtrToStructure( ptr, typeof(servent)
);
string strAliases = Marshal.PtrToStringAnsi( s.s_aliases );
return s;
}
int iLastError = WSAGetLastError( );
Marshal.ThrowExceptionForHR( iLastError );
throw new ApplicationException( "Unable to retrieve Servent
structure." );
}

[DllImport("Ws2_32.dll", EntryPoint="getservbyname",
SetLastError=true,
CharSet=CharSet.Ansi, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern IntPtr getservbyname( string strName, string
strProto );

[DllImport("Ws2_32.dll", EntryPoint="WSAGetLastError",
SetLastError=true,
CharSet=CharSet.Ansi, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern int WSAGetLastError( );

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct WSADATA
{
public short wVersion;
public short wHighVersion;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=WSADES CRIPTION_LEN +
1)]
public string szDescription ;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=WSADES CRIPTION_LEN +
1)]
public string szSystemStatus ;
public int wMaxSockets;
public int wMAXUDPDG;
public IntPtr dwVendorInfo;
}

[DllImport("wsock32.dll")]
public static extern int WSAStartup( ushort wVersionRequired, ref
WSADATA lpWSDATA);

[DllImport("wsock32.dll")]
public static extern int WSACleanup( );
}
}

Aug 15 '05 #1
0 1729

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ted | last post by:
Is it possible to use mailslots in .NET using PInvoke? I have a VC++ 6.0 based app that creates and listens to a mailslot. I have a second VC++ 6.0 based app that opens the mailslot and writes...
3
by: Brett Robichaud | last post by:
I have created a simple background thread to make one pinvoke call into a DLL I've created. My Winforms app spawns the thread in the form load event then go about it's business. The problem is...
5
by: vertigo | last post by:
Hello I use some win 32 API function for example: HANDLE CreateFile( LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD...
1
by: Wilfried Mestdagh | last post by:
Hi, Is there a NET equivalent for the winsock function getservbyname ? This one returns the portnum given by a string. eg 'smtp', 'ft', etc and gets his information from the 'services' file. ...
4
by: NanoWizard | last post by:
Can anyone verify if this is the correct way to do this? Thanks. using System; using System.Threading; using System.Diagnostics; using System.Runtime.InteropServices; namespace WSockImport...
5
by: _iycrd | last post by:
After numerous problems, I'm having second thoughts about using C++/CLI to wrap a native DLL. On the other hand, PInvoke seems like it will take a huge amount of work, if it will work at all. ...
8
by: Rajesh Soni | last post by:
Hi! I'm getting a PInvoke error while trying to execute the following code... declaration: Structure POINTAPI Dim x As IntPtr
0
by: Benosham | last post by:
I have been playing around with trying to PInvoke GDI+ from C#, I made recently made the transition from C/C++ to C# and I really like the language, however being the old fashioned programmer I am I...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.