Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

GetFullPathName

Nick2
New Contributor I
1,047 Views

Hello,

I am trying to get a full path where the executable is located at run-time. So here's what I'm trying:

program main
use KERNEL32

CHARACTER*500 path
integer(LPSTR) path1
CHARACTER*200 filename
integer(DWORD) dum
path1=0
filename='WideString.exe'
dum = GetFullPathName(filename, 500, path, path1)

if(dum .eq. 0)then
dum=GetLastError()
print *, dum
endif

print *, path

end


'WideString.exe' is the name of the executable. Similar code works well in C++, but not in Fortran.

Any idea what I'm doing wrong or a better way to accomplish this?

I forgot the mention...GetFullPathName and GetLastError both return 0, and the variable path ends up being a blank string.

0 Kudos
5 Replies
Steven_L_Intel1
Employee
1,047 Views
You want:

filename='WideString.exe'C

You also need to either search for the trailing NUL in the return value or use the function return value for the length.
0 Kudos
Nick2
New Contributor I
1,047 Views

Ah, thank you!

Only GetFullPathName returned the current path, rather than executable's path. Any better functions to accomplish this?

0 Kudos
Paul_Curtis
Valued Contributor I
1,047 Views

The routine you want is

INTEGER :: nc

CHARACTER(LEN=255) :: fpname

nc = GetModuleFileName(GetModuleHandle('MYPROG.EXE'C), fpname, LEN(fpname))

where you supply the name of your running executable and fpname is returned with the full UNC path.

0 Kudos
Steven_L_Intel1
Employee
1,047 Views
Even better, pass NULL to GetModuleHandle to get the handle to the running EXE, whatever its name is.
0 Kudos
Nick2
New Contributor I
1,047 Views
Awesome! I love it how the function returns the number of characters in the path too!
0 Kudos
Reply