Issue with api_GetModuleFileName Windows API func

Blitz3D Forums/Blitz3D Programming/Issue with api_GetModuleFileName Windows API func

Zethrax(Posted 2015) [#1]
So, what am I bollixing up here?

Basically I'm trying to use the Kernel32.dll function GetModuleFileNameA via the kernel32.decls found at: http://www.blitzbasic.com/codearcs/codearcs.php?code=1180

The point of my GetAppName$() function is to get the executable filename of the Blitz3D program. I'm having a couple of problems with this though.

Problem 1: While the 'size' value returned by 'api_GetModuleFileName' is non-zero (which should indicate that no errors were encountered), the string returned contains all zero values.

Problem 2: If I compile this code as an executable and run it I get a MAV followed by a hard crash. This happens in both debug and non-debug mode. This problem seems to be caused by the call to 'api_GetModuleFileName' (commenting out that call eliminates the problem), but the crash doesn't happen immediately after that call. The program will usually run the 'While' loop a few times before the crash occurs.

I've tried setting a larger than required buffer and string size for 'api_GetModuleFileName' to return the filepath string in, but that doesn't stop the problems.

I've tried using SystemProperty( "AppHWND" ) as the first parameter to use the app handle rather than the Null that signals the function to return the filename of the calling app. That results in size = 0, which is probably a permission issue.

The definition for 'api_GetModuleFileName' in the 'kernel32.decls' decls file seems to be using a string for 'lpFileName$' when it should possibly be using an integer so that a pointer value can be sent. Changing this to an int results in a mav when doing a quickie compile and run though.


This isn't a huge problem for the project I'm working on as I can simply hard code the filename of the app rather than using 'api_GetModuleFileName' to derive it. I'm curious about what's gone pear shaped here though.


; REQUIRES:-
; - Userlib 'kernel32.decls' decls file for 'Kernel32.dll' - http://www.blitzbasic.com/codearcs/codearcs.php?code=1180

; REFERENCES:-
; - https://msdn.microsoft.com/en-us/library/ms683197(VS.85).aspx
; - http://stackoverflow.com/questions/4000877/how-can-i-get-the-current-instances-executable-file-name-from-native-win32-c




Jimmy(Posted 2015) [#2]
Donīt have a computer to try the code on, does the Window API do something special?

Otherwise I would suggest try using Videz & Rross code GetExeName$ ( http://www.blitzbasic.com/codearcs/codearcs.php?code=3177 ) this
does the trick equally well I think and It has never failed me, seems rockksolid.


Guy Fawkes(Posted 2015) [#3]
http://www.blitzbasic.com/codearcs/codearcs.php?code=3177 *


Zethrax(Posted 2015) [#4]
Sending a string as the middle parameter for api_GetModuleFileName seems to eliminate the mav and crash issues, but I still end up with an unmodified string.

I ended up writing this code as a substitute. I works fine, but that 'GetCommandLine' in the posted code archive link looks interesting, so thanks for that.




EDIT: I ended up going with the code below, which is based on the 'GetCommandLine' Windows API function. This gets the absolute path of the executable enclosed in quote marks, which works for what I'm trying to achieve.