2

I'm doing a little project using Fortran. A part of the code is designed to check the PC's mac address. Currently, I'm using call system command as follows:

CALL SYSTEM("ipconfig -all >result.tmp") Above code will invoke the windows ipconfig-all command and output the information to an external file result.tmp. Later this file will be read to check the mac address.

https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-fo...

I tried that solution, it works fine for the system command "ipconfig -all", but I did not figure out how to output the result to an external file. Does anyone can give me some hints on how to achieve that ?

Above works, except one annoying thing. The Fortran code will be compiled as a DLL and used by another C# program. The annoying thing is, whenever above code is executed in the C# program, a console window will be prompted shortly and then closed. I searched the forum to find if there is some way to disable the window prompt, it turns out there is some solution in the following link:

2
  • Your link is wrong. The following link at the and is missing altogether. What happens when you do your CALL SYSTEM("ipconfig -all >result.tmp")? Is the result file not created? What exactly happens instead? Commented Jan 7, 2019 at 10:19
  • SYSTEM doesn't invoke a shell and thus redirection won't work. I tried ShellExecute, but couldn't get redirection to work there either. In general I would recommend instead using Windows API routines to retrieve the MAC address programatically rather than trying to parse the output of ipconfig (which may change or may have multiple adapters.) The GetAdaptersAddresses function will do this, but you'll have to write your own interface as Intel doesn't include it. Commented Jan 7, 2019 at 16:58

1 Answer 1

3

You also posted this at the Intel forum and user Paul Curtis replied with an example of how to get the MAC address directly using the Windows API.

Since StackOverflow prefers answers that aren't just links, I've included the code below.

MODULE MAC USE ifwinty USE charfunc IMPLICIT NONE PUBLIC GetMacInfo !, PortExists PRIVATE SAVE INTEGER, PARAMETER :: MAX_ADAPTER_DESCRIPTION_LENGTH = 128 INTEGER, PARAMETER :: MAX_ADAPTER_NAME_LENGTH = 256 INTEGER, PARAMETER :: MAX_ADAPTER_ADDRESS_LENGTH = 8 INTEGER, PARAMETER :: MIB_IF_TYPE_ETHERNET = 6 ! Ipifcons.h TYPE IP_ADDRESS_STRING CHARACTER(LEN=16) :: String END TYPE IP_ADDRESS_STRING TYPE IP_MASK_STRING CHARACTER(LEN=16) :: String END TYPE IP_MASK_STRING TYPE t_IP_ADDR_STRING INTEGER (LPLONG) :: pNext TYPE (IP_ADDRESS_STRING) :: IpAddress TYPE (IP_MASK_STRING) :: IpMask INTEGER (DWORD) :: Context END TYPE t_IP_ADDR_STRING TYPE t_IP_ADAPTER_INFO INTEGER(LPLONG) :: pNext INTEGER(DWORD) :: ComboIndex CHARACTER(LEN=MAX_ADAPTER_NAME_LENGTH+4) :: AdapterName CHARACTER(LEN=MAX_ADAPTER_DESCRIPTION_LENGTH+4) :: Description INTEGER(UINT) :: AddressLength INTEGER(BYTE) :: Address(MAX_ADAPTER_ADDRESS_LENGTH) INTEGER(DWORD) :: Index INTEGER(ULONG) :: iType INTEGER(ULONG) :: DhcpEnabled INTEGER(LPLONG) :: pCurrentIpAddress TYPE(t_IP_ADDR_STRING) :: IpAddressList TYPE(t_IP_ADDR_STRING) :: GatewayList TYPE(t_IP_ADDR_STRING) :: DhcpServer INTEGER(BOOL) :: HaveWins TYPE(t_IP_ADDR_STRING) :: PrimaryWinsServer TYPE(t_IP_ADDR_STRING) :: SecondaryWinsServer INTEGER(ULONG) :: LeaseObtained INTEGER(ULONG) :: LeaseExpires END TYPE t_IP_ADAPTER_INFO ! must link with IpHlpApi.lib to access this API function; ! this interface is not included in ifwinty INTERFACE INTEGER(BOOL) FUNCTION GetAdaptersInfo (arg1, arg2) USE ifwinty !DEC$ ATTRIBUTES DEFAULT, STDCALL, DECORATE, ALIAS:'GetAdaptersInfo' :: GetAdaptersInfo INTEGER(LPLONG) :: arg1 INTEGER(LPLONG) :: arg2 END FUNCTION END INTERFACE CONTAINS SUBROUTINE GetMacInfo (hwnd, id) USE contwrap IMPLICIT NONE ! dialog window handle and set of static-text IDs for display INTEGER(HANDLE), INTENT(IN) :: hwnd INTEGER, INTENT(IN), DIMENSION(4) :: id CHARACTER(LEN=200) :: msg INTEGER :: i, nc, count INTEGER, PARAMETER :: acount = 16 TYPE(t_IP_ADAPTER_INFO),ALLOCATABLE :: ai(:) count = 0 ! allow for multiple adapters ALLOCATE (ai(acount)) nc = SIZEOF(ai) IF (GetAdaptersInfo(LOC(ai), LOC(nc)) == 0) THEN DO i = 1, acount SELECT CASE (ai(i)%iType) CASE (MIB_IF_TYPE_ETHERNET) ! line 1: description and MAC address !nc = INDEX(ai(i)%Description, CHAR(0)) - 1 !WRITE (msg, '(A,", ",5(Z2.2,"-"),Z2.2)') & ! ai(i)%Description(1:nc), & ! ai(i)%Address(1:ai(i)%AddressLength) nc = INDEX(ai(i)%Description, CHAR(0)) msg = ai(i)%Description(1:nc) count = count + 1 CALL StaticSetText (hwnd, id(count), msg) CALL ControlSetVisible (hwnd, id(count), .TRUE.) ! line 2: IP and Gateway addresses WRITE (msg, '("IP Addr: ",A," Gateway: ",A)') & ai(i)%IpAddressList%IpAddress%string, & ai(i)%GatewayList%IpAddress%string CALL remove_nulls (msg) count = count + 1 CALL StaticSetText (hwnd, id(count), msg) CALL ControlSetVisible (hwnd, id(count), .TRUE.) IF (count >= 4) EXIT END SELECT IF (ai(i)%pNext == NULL) EXIT END DO END IF DEALLOCATE (ai) END SUBROUTINE GetMacInfo END MODULE MAC 
Sign up to request clarification or add additional context in comments.

3 Comments

Truly appreciate for your help Doctor Fortran. And also Paul's help. I will try above codes to see if it works.
There is a much easier way if 1) You still want to parse ipconfig output, 2) You have Intel Fortran 16 or later: call execute_command_line ('ipconfig -all > ipx.txt')
Is Intel Fortran 16 completely backward compatible with IVF 2013? If so I may consider to change to Fortran 16 instead

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.