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

GetFullPathName

Nick2
新規コントリビューター I
1,052件の閲覧回数

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 件の賞賛
5 返答(返信)
Steven_L_Intel1
従業員
1,052件の閲覧回数
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.
Nick2
新規コントリビューター I
1,052件の閲覧回数

Ah, thank you!

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

Paul_Curtis
高評価コントリビューター I
1,052件の閲覧回数

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.

Steven_L_Intel1
従業員
1,052件の閲覧回数
Even better, pass NULL to GetModuleHandle to get the handle to the running EXE, whatever its name is.
Nick2
新規コントリビューター I
1,052件の閲覧回数
Awesome! I love it how the function returns the number of characters in the path too!
返信