tags:

views:

2017

answers:

5

Hello,

I've been doing this in C# and Delphi ,but C++ is evil.The purpose is to create a file in the current directory(where the executable is running).

My code:

LPTSTR NPath = NULL;
DWORD a = GetCurrentDirectory(MAX_PATH,NPath);
HANDLE hNewFile = CreateFile(NPath,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

I get exception at GetCurrentDirectory().

Please tell me why I get an exception and how do I make it easier in C++?

+7  A: 

GetCurrentDirectory does not allocate space for the result, it's up to you to do that.

TCHAR NPath[MAX_PATH];
GetCurrentDirectory(MAX_PATH, NPath);

Also, take a look at Boost.Filesystem library if you want to do this the C++ way.

avakar
Hmm,NPath points to another directory,how do I make it show the directory where the executable is placed in?
John
The current directory is not the same as the executable's directory, even under C# and Delphi. Perhaps you could make your question clearer?
anon
John, that's a little more involved and can't be simply answered in a comment. Perhaps you should follow Neil's advice (both of them).
avakar
+14  A: 

I would recommend reading a book on C++ before you go any further, as from your other posts here you are simply making it up as you go along. Accelerated C++ by Koenig and Moo is excellent. And calling their favourite language "evil" is not likely to make the C++ experts here want to help you.

To get the executable path use GetModuleFileName:

char buffer[MAX_PATH]
GetModuleFileName( NULL, buffer, MAX_PATH );

Here's a C++ function that gets the directory without the file name:

#include <windows.h>
#include <string>
#include <iostream>
using namespace std;;

string ExePath() {
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\\/" );
    return string( buffer ).substr( 0, pos);
}

int main() {
    cout << "my directory is " << ExePath() << "\n";
}
anon
+1, although it technically doesn't answer the question.
avakar
He asked how to make it easier in C++. My answer: learn some C++.
anon
I'm not yet considering to write my application in that language.I have to turn a C++ project into a C++ dll that I can use in my C# Application.
John
To do that you will meed a good understanding of C++ memory management. To get that, you will need to read a book.
anon
A: 

You should provide a valid buffer placeholder. that is:

TCHAR s[100];
DWORD a = GetCurrentDirectory(100, s);
yves Baumes
A: 

GetCurrentDirectory() gets the current directory which is where the exe is invoked from. To get the location of the exe, use GetModuleFileName(NULL ...). if you have the handle to the exe, or you can derive it from GetCommandLine() if you don't.

As Mr. Butterworth points out, you don't need a handle.

nickd
Actually, you don't need a real handle - a NULL handle gets the executable file name, with path.
anon
A: 

To find the directory where your executable is, you can use:

TCHAR szFilePath[_MAX_PATH];
::GetModuleFileName(NULL, szFilePath, _MAX_PATH);
Andomar