Getting folder that holds EXE

closed account (D9yvqMoL)
Hello, i am wondering how to get folder, this worked, but it tells me EXE name too:
1
2
3
HMODULE hModule = GetModuleHandleW(NULL);
WCHAR path[MAX_PATH];
GetModuleFileNameW(hModule, path, MAX_PATH);


It Is C:\Users\Arsen\Documents\Visual Studio 2010\C++ Projects\A++ runtime Evernoment\Debug\A++ runtime Evernoment.exe, and i wanna:
C:\Users\Arsen\Documents\Visual Studio 2010\C++ Projects\A++ runtime Evernoment\Debug
or
C:\Users\Arsen\Documents\Visual Studio 2010\C++ Projects\A++ runtime Evernoment\Debug\
Welcome to the world of string parsing. You need to locate the last backslash in the string and either create a new string that stops at that point, or null terminate the string at that point.

Or, just use GetCurrentDirectory().
Last edited on
closed account (D9yvqMoL)
My question is how to remove last X letters from path, where X is argv[0].length.
Last edited on
closed account (z05DSL3A)
Have a look at the PathCchRemoveFileSpec function.

Or PathRemoveFileSpec (deprecated but if you don't use Windows 8...)
Last edited on
closed account (D9yvqMoL)
Ok:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// A++ runtime Evernoment.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Shlwapi.h>;
#include <windows.h>;
#include <iostream>;
#include <string>
#include <vector>
using namespace std;

bool hasEnding(std::string const &fullString, std::string const &ending)
{
	if (fullString.length() >= ending.length()) {
		return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
	} else {
		return false;
	}
}


void PressEnterToContinue(const char* message){ 
	cout<<message<< " \r\nPress ENTER to exit";
	char c=getchar();
	while (c != '\n' && c != EOF){
		c=getchar(); 
	}
}

int _tmain(int argc, _TCHAR *argv[])
{
	HMODULE hModule = GetModuleHandleW(NULL);
	WCHAR path[MAX_PATH];
	GetModuleFileNameW(hModule, path, MAX_PATH);
	PathRemoveFileSpecW(path);
	std::wstring Commands = L"java -jar ";
	Commands.append(path);
	Commands = Commands + L"mylang.jar ";
	wcout<<path<<endl;
	std::wstring Command; 
	
	system("color 7F");
	if(argc == 1){
	std::cout<<"No Variables. Please use Open With";
	PressEnterToContinue("\r\nArsen++ Runtime Evernoment ended!");
	exit(0);
	}
	Commands.append(argv[1]);
	const wchar_t *cmd = Commands.c_str();
	std::wcout<<cmd;
	//_wsystem(cmd);
	PressEnterToContinue("\r\nArsen++ Runtime Evernoment ended!");
	return 0;
}


Error:
1>------ Build started: Project: A++ runtime Evernoment, Configuration: Debug Win32 ------
1> A++ runtime Evernoment.cpp
1>c:\users\arsen\documents\visual studio 2010\c++ projects\a++ runtime evernoment\a++ runtime evernoment\a++ runtime evernoment.cpp(4): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>c:\users\arsen\documents\visual studio 2010\c++ projects\a++ runtime evernoment\a++ runtime evernoment\a++ runtime evernoment.cpp(5): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>c:\users\arsen\documents\visual studio 2010\c++ projects\a++ runtime evernoment\a++ runtime evernoment\a++ runtime evernoment.cpp(6): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>A++ runtime Evernoment.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
1>A++ runtime Evernoment.obj : error LNK2019: unresolved external symbol __imp__PathRemoveFileSpecW@4 referenced in function _wmain
1>C:\Users\Arsen\Documents\Visual Studio 2010\C++ Projects\A++ runtime Evernoment\Debug\A++ runtime Evernoment.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
@ OP: Have you ever seen anyone else end an #include macro statement with a semicolon? Then remove them from Lines 4, 5 and 6.
closed account (z05DSL3A)
You also need to link to the correct library, #pragma comment(lib, "Shlwapi.lib") should do the trick.
Topic archived. No new replies allowed.