www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - GetInterfaceInfo function of win32 api

reply Benny <saretbenny gmail.com> writes:
Hello,
I'm trying to call the function GetInterfaceInfo of 
iphlpapi.dll/runtimes of D, and it didn't work.
I don't know how to call it, or what parameters I should put in. 
I checked the Microsoft Windows APIs, the D documentation of the 
function, and still, don't succeed.
Can someone please explain me with an example how to use it?
Jun 07 2023
parent reply novice2 <sorryno em.ail> writes:
On Thursday, 8 June 2023 at 05:29:07 UTC, Benny wrote:
 Hello,
Hi!
 I'm trying to call the function GetInterfaceInfo
it would be nice to see the code
 didn't work.
it would be nice to see specifics (error code, results, etc)
Jun 07 2023
parent reply Benny <saretbenny gmail.com> writes:
On Thursday, 8 June 2023 at 05:42:20 UTC, novice2 wrote:
 On Thursday, 8 June 2023 at 05:29:07 UTC, Benny wrote:
 Hello,
Hi!
 I'm trying to call the function GetInterfaceInfo
it would be nice to see the code
 didn't work.
it would be nice to see specifics (error code, results, etc)
``` import core.sys.windows.iphlpapi; import std.stdio; void main() { auto h = GetInterfaceInfo(null, null); writeln("Hello, World!"); } ``` ``` app.obj : error LNK2019: unresolved external symbol GetInterfaceInfo referenced in function _Dmain app.exe : fatal error LNK1120: 1 unresolved externals Error: linker exited with status 1120 ```
Jun 07 2023
parent reply Mike Parker <aldacron gmail.com> writes:
On Thursday, 8 June 2023 at 05:52:43 UTC, Benny wrote:

 ```
 ```
 app.obj : error LNK2019: unresolved external symbol 
 GetInterfaceInfo referenced in function _Dmain
 app.exe : fatal error LNK1120: 1 unresolved externals
 Error: linker exited with status 1120
 ```
That's a linker error telling you that you aren't linking anything that contains the symbol for `GetInterfaceInfo`. You need to link `Iphlpapi.lib`.
Jun 07 2023
parent reply Benny <saretbenny gmail.com> writes:
On Thursday, 8 June 2023 at 06:22:08 UTC, Mike Parker wrote:
 On Thursday, 8 June 2023 at 05:52:43 UTC, Benny wrote:

 ```
 ```
 app.obj : error LNK2019: unresolved external symbol 
 GetInterfaceInfo referenced in function _Dmain
 app.exe : fatal error LNK1120: 1 unresolved externals
 Error: linker exited with status 1120
 ```
That's a linker error telling you that you aren't linking anything that contains the symbol for `GetInterfaceInfo`. You need to link `Iphlpapi.lib`.
I got something! thank you. now I need to understand how can I get the properties of the GetInterfaceInfo ``` import core.sys.windows.iphlpapi; import std.stdio; import core.stdc.stdlib; void main() { uint* i; i = cast(uint*) malloc(10); auto h = GetInterfaceInfo(null, i); writefln("%s", h); writeln("Hello, World!"); } ``` this got me 122
Jun 08 2023
next sibling parent Mike Parker <aldacron gmail.com> writes:
On Thursday, 8 June 2023 at 07:01:44 UTC, Benny wrote:

 I got something! thank you.
 now I need to understand how can I get the properties of the 
 GetInterfaceInfo

 ```
 import core.sys.windows.iphlpapi;
 import std.stdio;
 import core.stdc.stdlib;

     void main()
     {
     	uint* i;
     	i = cast(uint*) malloc(10);
     	auto h = GetInterfaceInfo(null, i);
     	writefln("%s", h);
     	writeln("Hello, World!");
     }

 ```

 this got me 122
Alright. I've never used this function, so I referenced the documentation here: https://learn.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getinterfaceinfo First, you don't need to allocate i with malloc. Just declare it as a uint and give the function a pointer to it. Second, the function is intended to be called twice. The first time with null for the first parameter as you've done here. After that call, the value in i should be the size of the memory you need to allocate for the first parameter on the second call. In the second call, the data you want will be stored in that pointer. Third, the first parameter is of type PIP_INTERACE_INFO, which in Windows lingo means "pointer to IP_INTERFACE_INFO". That's declared in the ipexport module. Here's a working example. I chose to use the GC rather than malloc. ```d import core.sys.windows.windows, // For the error codes core.sys.windows.iphlpapi, core.sys.windows.ipexport; import std.stdio, std.string; void main() { IP_INTERFACE_INFO* pinfo; uint buflen; // Get the size needed to alloc pinfo uint ret = GetInterfaceInfo(null, &buflen); if(ret == ERROR_INSUFFICIENT_BUFFER) { // Allocate pinfo, but let's not use malloc ubyte[] buf = new ubyte[](buflen); pinfo = cast(IP_INTERFACE_INFO*)buf.ptr; } // Call the function a second time to get the data ret = GetInterfaceInfo(pinfo, &buflen); if(ret == NO_ERROR) { writeln("Number of adapters: ", pinfo.NumAdapters); for(size_t i = 0; i<pinfo.NumAdapters; ++i) { writefln("Adapter Index[%s]: %s", i, pinfo.Adapter[i].Index); writefln("Adapter Index[%s]: %s", i, fromStringz(pinfo.Adapter[i].Name)); } } else if(ret == ERROR_NO_DATA) { writeln("No network adapters found"); } else { writeln("GetInterfaceInfo failure: ", ret); } } ```
Jun 08 2023
prev sibling parent a11e99z <black80 bk.ru> writes:
On Thursday, 8 June 2023 at 07:01:44 UTC, Benny wrote:

 this got me 122
don be shy, try to do something yourself, be curiosity! use C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\errlook.exe or https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499- to find what means some error code
Jun 08 2023