473,395 Members | 1,422 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,395 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( );
}
}

Nov 17 '05 #1
4 4279
NanoWizard wrote:

[snip]

Out of curiosity: Is this an exercise in P/Invoke programming, or why
don't you use System.Net.Dns?

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 17 '05 #2
Can you retrieve the corresponding port using only a service name using
System.Net.Dns? I didn't see it in there. Seems like that class is
only used for domain name retrieval.

Nov 17 '05 #3
NanoWizard wrote:
Can you retrieve the corresponding port using only a service name
using System.Net.Dns? I didn't see it in there. Seems like that
class is only used for domain name retrieval.


My error -- I somehow figured that getservbyname is a DNS API :-S

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 17 '05 #4
No prob.. Maybe it's in .NET Framework 2.0..

Nov 17 '05 #5

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...
0
by: Robert Colfin | 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: Carlos Guzmán Álvarez | last post by:
Hello: I'm trying to execute a function of a unmanaged dll using PInvoke, i have definied the function as: public static extern int isc_dsql_prepare( int status_vector, ref int...
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. ...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.