Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > Как узнать путь к программе из нее самой


Автор: Alexey_2007 17.1.2007, 21:31
Как узнать путь к программе из нее самой?

Автор: Anikmar 17.1.2007, 21:32
В функцию main первым аргументом передается как раз имя программы с путем

Автор: archimed7592 17.1.2007, 21:35
не всегда...бывает, что без пути - только имя программы...

Автор: Anikmar 17.1.2007, 21:43
Цитата(archimed7592 @  17.1.2007,  21:35 Найти цитируемый пост)
не всегда...бывает, что без пути - только имя программы... 

Это в каких случаях?

Конечно все зависит от компилятора. На примере Билдера:
Цитата

Three parameters (arguments) are passed to main by the Borland C++Builder startup routine: argc, argv, and env.

argc, an integer, is the number of command-line arguments passed to main, including the name of the executable itself.
    argv is an array of pointers to strings (char *[]).

-    argv[0] is the  full path name of the program being run. 
                                       ^^^^^^
-    argv[1] points to the first string typed on the operating system command line after the program name.
-    argv[2] points to the second string typed after the program name.


Автор: zkv 17.1.2007, 21:57
в Windows 
Код

DWORD GetModuleFileName(
  HMODULE hModule, 
  LPTSTR lpFilename,
  DWORD nSize
);

Цитата(MSDN)

hModule 
[in] Handle to the module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path of the executable file of the current process. 

Автор: Romikgy 17.1.2007, 22:15
Код

#include <iostream>
#include <windows.h>


    char buf[260]={0};
    GetModuleFileName(0,buf,sizeof(buf));
    cout<<buf<<endl;

Автор: Sartorius 17.1.2007, 22:19
дублируем http://forum.vingrad.ru/topic-132143.html

Автор: Alexey_2007 17.1.2007, 22:42
спасибо всем!

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)