Multimobile Development: Building Applications for any Smartphone
PInvoke, Does anyone know ?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.


Koos du Preez
I'm trying to call a API with c# and i'm having some trouble.. could
someone please help on calling this function with c#

I dont know how to call the API with these structures that use pointers etc.

PLEASE help ...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tcpip/tcpip
_26sl.asp

this is the function:
DWORD GetIpAddrTable(
PMIB_IPADDRTABLE pIpAddrTable, // [out] buffer for mapping table
PULONG pdwSize, // [in/out] size of buffer
BOOL bOrder // [in] sort the table
);
//table mapping sruct:
typedef struct _MIB_IPADDRTABLE {
DWORD dwNumEntries; // number of entries in the table
MIB_IPADDRROW table[ANY_SIZE]; // array of IP address entries
} MIB_IPADDRTABLE, *PMIB_IPADDRTABLE;//IP Address struct:typedef struct
_MIB_IPADDRROW {
DWORD dwAddr; // IP address
DWORD dwIndex; // interface index
DWORD dwMask; // subnet mask
DWORD dwBCastAddr; // broadcast address
DWORD dwReasmSize; // re-assembly size
unsigned short unused1; // not currently used
unsigned short unused2; // not currently used
} MIB_IPADDRROW, *PMIB_IPADDRROW;Members

Reply to this message...
Vote that this is a GOOD answer...
 
Really good experience at the Apple Store
MonoDroid – looking *awesome*
 
    
Mattias Sjögren
Koos,

Try the code below.

Mattias

===
Mattias Sjögren (VB MVP)
mattias @ mvps.org
http://www.msjogren.net/dotnet/

using System;
using System.Net;
using System.Runtime.InteropServices;

struct MIB_IPADDRROW
{
public int dwAddr;
public int dwIndex;
public int dwMask;
public int dwBCastAddr;
public int dwReasmSize;
public short unused1;
public short unused2;
}

class C
{
[DllImport("iphlpapi.dll")]
static extern int GetIpAddrTable(
IntPtr pIpAddrTable,
ref int pdwSize,
bool bOrder);

static string IPToString(int ipaddr)
{
return String.Format( "{0}.{1}.{2}.{3}",
(ipaddr >> 24) & 0xFF, (ipaddr >> 16) & 0xFF,
(ipaddr >> 8) & 0xFF, ipaddr & 0xFF);
}

static void Main()
{
IntPtr pBuf = IntPtr.Zero;
int nBufSize = 0;

GetIpAddrTable( IntPtr.Zero, ref nBufSize, false );
try {
pBuf = Marshal.AllocHGlobal( nBufSize );
int r = GetIpAddrTable( pBuf, ref nBufSize, false );
if ( r != 0 )
throw new System.ComponentModel.Win32Exception( r );

#if USE_UNSAFE_CODE

unsafe {
int nNumRows = *((int*)(void*)pBuf);

MIB_IPADDRROW* pRow =
(MIB_IPADDRROW*)(void*)(IntPtr)((int)pBuf + sizeof(int));
while ( nNumRows-- > 0 ) {
Console.WriteLine( "---" );
Console.WriteLine( IPToString( IPAddress.NetworkToHostOrder(
pRow->dwAddr ) ) );
Console.WriteLine( pRow->dwIndex );
Console.WriteLine( IPToString( IPAddress.NetworkToHostOrder(
pRow->dwMask ) ) );
Console.WriteLine( IPToString( IPAddress.NetworkToHostOrder(
pRow->dwBCastAddr ) ) );
Console.WriteLine( pRow->dwReasmSize );
pRow++;
}
}

#else

Type tIPAddrRow = typeof(MIB_IPADDRROW);
int nNumRows = Marshal.ReadInt32( pBuf );
IntPtr pRow = unchecked((IntPtr)((int)pBuf + 4));
while ( nNumRows-- > 0 ) {
MIB_IPADDRROW row = (MIB_IPADDRROW)Marshal.PtrToStructure(
pTmp, tIPAddrRow );
Console.WriteLine( "---" );
Console.WriteLine( IPToString( IPAddress.NetworkToHostOrder(
row.dwAddr ) ) );
Console.WriteLine( row.dwIndex );
Console.WriteLine( IPToString( IPAddress.NetworkToHostOrder(
row.dwMask ) ) );
Console.WriteLine( IPToString( IPAddress.NetworkToHostOrder(
row.dwBCastAddr ) ) );
Console.WriteLine( row.dwReasmSize );
pRow = unchecked((IntPtr)((int)pRow + Marshal.SizeOf(
tIPAddrRow )));
}

#endif

}
catch (Exception ex) {
Console.WriteLine( ex.Message );
}
finally {
if ( pBuf != IntPtr.Zero )
Marshal.FreeHGlobal( pBuf );
}
}

}

Reply to this message...
Vote that this is a GOOD answer...
 
First volume of Multimobile Development nearly ready to go to press
A mention on Developing for the iPhone and Android: The pros and cons
 
    
Koos du Preez
WOW !!!!! Thats Awesome !! How the hell do you know all this stuff, I had
NO idea about the Marshal class..

this is REALY awesome, Thats exactly what I had a hard time doing; getting
buffer pointer into a structure !!

you are the King and I salute you !!

thanks a bunch,

Regards,

Koos

"Mattias Sjögren" <Click here to reveal e-mail address> wrote in message
news:#z9ohUotBHA.1860@tkmsftngp04...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Russell Mangel
This looks like modem noise, or maybe you sent a sendmail configuration
file. Ha!

Say, Mr. PInvoke... Can you help with this:

Several people in the .NET newsgroups, have been asking on how to retrieve
the associated icon for a given file type, usually for display in a treeview
or listview control. I have been informed by a few C++ people who claim the
only way to get these icons using C# is to make a call to the WIN API
(shgetfileinfo, I think). Is there an easier way for us to get these icons
programmtically? If not, do you have any idea, or suggestion which might
lead us to some code that would help us out?

Thanks

Signed

- Scratching and Clawing my way through C#/.NET

"Mattias Sjögren" <Click here to reveal e-mail address> wrote in message
news:#z9ohUotBHA.1860@tkmsftngp04...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
First chapters of Multimobile Development book now available on Apress Alpha program
iPad
 
    
Mattias Sjögren
Russell,

>Is there an easier way for us to get these icons programmtically?

No

[Original message clipped]

As you said yourself, this question has been asked, and answered, a
number of times before. I'm sure you can find some code by searching
for SHGetFileInfo in the microsoft.public.dotnet.* hierarchy at
groups.google.com.

Mattias

===
Mattias Sjögren (VB MVP)
mattias @ mvps.org
http://www.msjogren.net/dotnet/
Reply to this message...
Vote that this is a GOOD answer...
 
New book project – Multimobile Development: Building Applications for any Smartphone
Dive into HTML5
 
    
David Ball
When I searched for examples of using iphlpapi.dll, I found your
example. Unfortunately, when I tried to compile this with the
release version of C#, I got an error (see below). Could you offer a
suggestion ?

BTW, thanks for all the helpful messages you've posted here.

TIA,

-- David Ball

On Sat, 16 Feb 2002 02:01:25 +0100, Mattias Sjögren
<Click here to reveal e-mail address> wrote:

[Original message clipped]

^^^^^ It complains that "The Name pTmp does not exist in the class
or namespace LearnIpHlpApi.Class1.C"

I tried removing the namespace directives so it would match your
example exactly but it only changed the error message to "The Name
pTmp does not exist in the class or namespace Class1.C"

[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Tom Kaiser
You may want to take a look at the P2P samples that Microsoft published on
gotdotnet.com. These samples have a lot of the
IP Helper apis made available.

"David Ball" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
Steve Jobs’ thoughtful/thought provoking Thoughts on Flash…
Handy list of countries in CSV format
 
    
David Ball
On Thu, 18 Apr 2002 22:49:01 -0700, "Tom Kaiser"
<Click here to reveal e-mail address> wrote:

[Original message clipped]

Thanks. I found them on the site and there's some really good info
there.

-- David Ball

Reply to this message...
Vote that this is a GOOD answer...
 
Really good experience at the Apple Store
MonoDroid – looking *awesome*
 
 
System.ComponentModel.Win32Exception
System.Console
System.IntPtr
System.Net.IPAddress
System.Runtime.InteropServices.Marshal
System.String




Multimobile Development: Building Applications for any Smartphone
Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734