MFC: GetCurrentDirectory function - mfc

I know that GetCurrentDirectory() and SetCurrentDirectory() functions exist on the MFC framework, but I don't have a CFtpConnection object in my application. I have a simple CWinApp-derived class, and I would like to retrieve its working directory upon program startup. What's the easiest method to achieve this goal? Thanks in advance for the advices.

GetCurrentDirectory is a simple Win32 API function, so just call it like this:
TCHAR currentDir[MAX_PATH];
GetCurrentDirectory( MAX_PATH, currentDir );

I assume you are trying to get the directory where your .exe file is located instead of the current directory. This directory can be different from the current directory.
TCHAR buff[MAX_PATH];
memset(buff, 0, MAX_PATH);
::GetModuleFileName(NULL,buff,sizeof(buff));
CString strFolder = buff;
strFolder = strFolder.Left(strFolder.ReverseFind(_T('\\'))+1);

Related

ShellExecuteA - executable won't run from %appdata% [duplicate]

As above, how do I get the AppData folder in Windows using C?
I know that for C# you use Environment.SpecialFolder.ApplicationData
Use SHGetSpecialFolderPath with a CSIDL set to the desired folder (probably CSIDL_APPDATA or CSIDL_LOCAL_APPDATA).
You can also use the newer SHGetFolderPath() and SHGetKnownFolderPath() functions.
There's also SHGetKnownFolderIDList() and if you like COM there's IKnownFolder::GetPath().
If I recall correctly it should just be
#include <stdlib.h>
getenv("APPDATA");
Edit: Just double-checked, works fine!
Using the %APPDATA% environment variable will probably work most of the time. However, if you want to do this the official Windows way, you should use use the SHGetFolderPath function, passing the CSIDL value CSIDL_APPDATA or CSIDL_LOCAL_APPDATA, depending on your needs.
This is what the Environment.GetFolderPath() method is using in .NET.
EDIT: Joey correctly points out that this has been replaced by SHGetKnownFolderPath in Windows Vista. News to me :-).
You might use these functions:
#include <stdlib.h>
char *getenv(
const char *varname
);
wchar_t *_wgetenv(
const wchar_t *varname
);
Like so:
#include <stdio.h>
char *appData = getenv("AppData");
printf("%s\n", appData);
Sample code from MSDN:
TCHAR szPath[MAX_PATH];
if (SUCCEEDED(SHGetFolderPath(NULL,
CSIDL_APPDATA | CSIDL_FLAG_CREATE,
NULL,
0,
szPath)))
{
PathAppend(szPath, TEXT("MySettings.xml"));
HANDLE hFile = CreateFile(szPath, ...);
}
CSIDL_APPDATA = username\Application Data. In Window 10 is: username\AppData\Roaming
CSIDL_FLAG_CREATE = combine with CSIDL_ value to force folder creation in SHGetFolderPath()
You can also use:
CSIDL_LOCAL_APPDATA = username\Local Settings\Application Data (non roaming)

c++ GetPrivateProfileString read ini file from current directory

I'm creating a dll on c++. It is a Visual Studio project. The dll reads some data from ini file. I have decided to use GetPrivateProfileString function. It works almost completely. It does not see file in current directory. How can I provide this parameter (variable called path)?
How can I pass last parameter (path)
Code:
LPCTSTR path = L"\\test.ini";
TCHAR protocolChar[32];
int a = GetPrivateProfileString(_T("Connection"), _T("Protocol"), _T(""), protocolChar, 32, path);
String from test.ini:
[Connection]
Protocol = HTTP
I also tried this:
LPCTSTR path = L"test.ini";
But it did not help me
LPCTSTR path = _T(".\\test.ini");
. symbolises current directory. Hope this will work for you.
WCHAR cfg_IniName[256];
GetCurrentDirectory (MAX_PATH, cfg_IniName );
wcscat ( cfg_IniName, L"\\test.ini" );
way to get full path

How to get the "temp folder" in Windows 7?

In Windows 7, how can I get programatically the system temporary folder?
The GetTempPath function is probably what you're looking for.
TCHAR buf [MAX_PATH];
if (GetTempPath (MAX_PATH, buf) != 0)
MessageBox (0, buf, _T("Temp path"), 0);
Have you given a try to GetTempPath()?
Retrieves the path of the directory designated for temporary files.
You can find a code sample here.
You could get the environment variable for the temp folder:
http://msdn.microsoft.com/en-us/library/ms683188%28VS.85%29.aspx

How can I get dll location path inside the same dll in C++?

Suppose that I have a dll called it MyDll.dll
It is in the d:\MyWorks\MyDll.dll [it is directshow dll]
I want to get path of its location from inside the MyDll code.
I used boost for this: FileSystem
string path = "";
boost::filesystem::path full_path( boost::filesystem::current_path() );
path = full_path.string();
But this give me its execution path, which is C:\Windows\system32, and not its location path which is d:\MyWorks\MyDll.dll.
How can I get a dll's location inside the same dll?
Update: By Get Module:
TCHAR path[2048];
GetModuleFileName( NULL, path, 2048 );
ostringstream file;
file << path ;
string const pathString =file.str();
cout << "Path: " << pathString << endl;
Gives me just hex-like string : 0049EA95....
In your DllMain you receive an HINSTANCE parameter; that is actually the HMODULE of your dll, that you can use with GetModuleFileName to retrieve the fully-qualified path of your dll. To get just the directory that contains it you just have to remove the file name (you can do that with boost::filesystem, with shell path functions as well as just with a strrchr).
Your problem is trying to see a Unicode string on an Ansi console output window. If you really want to see the result, you need to cast your strings to Ansi (with some loss of course) or you can directly use;
char path[2048];
GetModuleFileNameA(NULL, path, 2048);
cout << path;
If you want to use Unicode, use TCHAR and GetModuleFileNameW (or GetModuleFileName since your application is in unicode mode) but don't try to output to console window without casting to Ansi.
You can use GetModuleFileName in order to get the full path of a module.
The first argument is a handle to the required module. If this parameter is NULL, GetModuleFileName retrieves the path of the executable file of the current process.
If you want the path to other module, you may use GetModuleHandle to get a handle. For example:
TCHAR path[_MAX_PATH+1];
GetModuleFileName(GetModuleHandle(_T("MyDll.dll")), path, sizeof(path)/sizeof(path[0]));
TCHAR s[MAX_PATH+1];
GetModuleFileName(hInstance, s, _countof(s));
where hInstance is a parameter of DllMain. Despite the name, it returns the full path.

How to set the application path to the running program?

I have a program that is executed by another program. The program that is being executed needs files located at its own location [same folder]. If I call myfile.open("xpo.dll") I might get an error because I am not passing the [fullpath + name + extension]. The program that is being executed can vary paths depending on the installation path. Therefore, I was wondering if there is a way to get the application path [where the application is located] and set it so that when another program executes from another path everything might work properly...?
[Using C++ without .NET Framework]
Thanks.
Use GetModuleFileName and pass NULL for hModule.
DWORD GetModuleFileName(
HMODULE hModule, // handle to module
LPTSTR lpFilename, // path buffer
DWORD nSize // size of buffer
);
First off, I run into this problem in other languages a lot, and find Process Monitor (http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx) very useful for finding out what folder it is currently trying to access.
There is no standard function for doing this.
Just a thought, have you tried doing myfile.open "./xpo.dll"?
If it's a console application, you can use the POSIX getcwd function: http://www.dreamincode.net/code/snippet77.htm
If it's a Windows app and you can use the windows API, you can use GetModuleFileName... see the second reply to this question here: How do I get the directory that a program is running from?