xsharp.eu • ExecName() behaviour
Page 1 of 1

ExecName() behaviour

Posted: Wed Feb 27, 2019 6:39 pm
by Karl-Heinz
Guys,

i´m using the VO LogFile class and noticed that the ExecName() func doesn´t work as expected.

oLog := LogFile { "" , FALSE , TRUE }

If no path+file name is given the Logfile init() creates the logfile path+name by calling ExecName(true). Then the extension is removed and "log" added. But because the ExecName() func is part of the XSharp core dll the Logfile points to the GAC dir:

"C:windows...v4.0_2.0.0.9__ed555a0467764586XSharp.Core.log" - :woohoo:

Responsible for that is the core _ExecName() function which uses:

System.Reflection.Assembly.GetExecutingAssembly():Location

As a workaround I created the 2 funcs below and placed them in a DLL. Now, if ExecName2() is called within my exe the created path+file name is the same as with VO. Are there any pitfalls if "System.Reflection.Assembly.GetExecutingAssembly():Location" is simply replaced with "System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName" ?

Code: Select all


// ----------- DLL code ---

 STATIC METHOD _ExecName2()  AS STRING	
 RETURN System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
// instead of the currently used:
// RETURN System.Reflection.Assembly.GetExecutingAssembly():Location

// -------- 	
 
 STATIC METHOD ExecName2( lFull AS LOGIC )  AS STRING
    LOCAL   nPos        AS DWORD
    LOCAL   cPath       AS STRING

    cPath := _ExecName2()

    IF ! lFull
        nPos  := RAt( "", cPath )

        IF nPos != 0
            cPath := SubStr2( cPath, nPos + 1 )
        ENDIF
    ENDIF

RETURN cPath    
regards
Karl-Heinz

ExecName() behaviour

Posted: Wed Feb 27, 2019 6:57 pm
by lumberjack
Karl-Heinz wrote:Guys,
oLog := LogFile { "" , FALSE , TRUE }
I will never trust that rather do:

Code: Select all

oLog := LogFile { Application.ExecutablePath, FALSE , TRUE }
Regards,

ExecName() behaviour

Posted: Thu Feb 28, 2019 12:44 am
by Chris
Hi Karl-Heinz,

Instead of GetExecutingAssembly(), it should had been GetEntryAssembly(). Although as you say, there are more ways to obtain this directory. And one of the very good things with X# and .Net in general, is that we can do a little search in the web for the best option, advantages and disadvantages of all methods, which one people suggest to use, and all replies will apply to all the .Net languages, including X# of course. That's exactly what I will do right now, to make sure that using GetEntryAssembly() is the correct way to do it!

Chris