MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / Init

Method Init

source/script.cpp:676–823  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

674
675
676ResultType Script::Init(global_struct &g, LPTSTR aScriptFilename, bool aIsRestart)
677// Returns OK or FAIL.
678// Caller has provided an empty string for aScriptFilename if this is a compiled script.
679// Otherwise, aScriptFilename can be NULL if caller hasn't determined the filename of the script yet.
680{
681 mIsRestart = aIsRestart;
682 TCHAR buf[UorA(T_MAX_PATH, 2048)]; // Just to make sure we have plenty of room to do things with.
683 size_t buf_length;
684
685 // It may be better to get the module name this way rather than reading it from the registry
686 // in case the user has moved it to a folder other than the install folder, hasn't installed it,
687 // or has renamed the EXE file itself.
688 if (buf_length = GetModuleFileName(NULL, buf, _countof(buf)))
689 {
690 // Any path longer than MAX_PATH is probably impossible as of 2018 since testing indicates
691 // the program can't start if its path is longer than MAX_PATH-1 even with Windows 10 long
692 // path awareness enabled.
693 if (buf_length == _countof(buf)) // It was truncated.
694 return FAIL; // Seems the safest option for this unlikely case.
695 mOurEXE = SimpleHeap::Alloc(buf, buf_length);
696 LPTSTR last_backslash = _tcsrchr(buf, '\\');
697 if (last_backslash) // Probably always true due to the nature of GetModuleFileName().
698 mOurEXEDir = SimpleHeap::Alloc(buf, last_backslash - buf);
699 }
700
701#ifdef AUTOHOTKEYSC
702 mKind = ScriptKindResource;
703 // Fix for v1.0.29: Override the caller's use of __argv[0] by using GetModuleFileName(),
704 // so that when the script is started from the command line but the user didn't type the
705 // extension, the extension will be included. This necessary because otherwise
706 // #SingleInstance wouldn't be able to detect duplicate versions in every case.
707 // It also provides more consistency.
708 aScriptFilename = buf;
709#else
710 TCHAR def_buf[513]; // Enough for max Documents path (256 chars, according to testing on 20H2), slash and max NTFS filename (255 chars).
711#ifdef _DEBUG
712 if (!aScriptFilename)
713 aScriptFilename = _T("Test\\Test.ahk");
714#endif
715 if (!aScriptFilename) // v1.0.46.08: Change in policy: store the default script in the My Documents directory rather than in Program Files. It's more correct and solves issues that occur due to Vista's file-protection scheme.
716 {
717 // Since no script-file was specified on the command line, use the default name.
718 // For portability, first check if there's an <EXENAME>.ahk file in the current directory.
719 LPTSTR suffix, dot;
720 if ( (suffix = _tcsrchr(buf, '\\')) // Find name part of path.
721 && (dot = _tcsrchr(suffix, '.')) // Find extension part of name.
722 && dot - buf + 5 < _countof(buf) ) // Enough space in buffer?
723 {
724 _tcscpy(dot, EXT_AUTOHOTKEY);
725 }
726 else // Very unlikely.
727 return FAIL;
728
729 aScriptFilename = buf; // Use the entire path, including the exe's directory.
730 if (GetFileAttributes(aScriptFilename) == 0xFFFFFFFF) // File doesn't exist, so fall back to new method.
731 {
732 // The only known cause of failure for SHGetKnownFolderPath is having a path longer than
733 // 256 characters; i.e. by sabotaging the "...\User Shell Folders\Personal" registry value.

Callers 1

AutoHotkey.cppFile · 0.45

Calls 3

GetDocumentsFolderFunction · 0.85
sntprintfcatFunction · 0.85

Tested by

no test coverage detected