简体   繁体   中英

Why I can't get directory name

My program, where I work with Win API:

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
using namespace std;

int _main(int argc, _TCHAR* argv[]) {
    char *fileExt = NULL;
    TCHAR szDir[MAX_PATH];
    GetFullPathName(argv[0], MAX_PATH, szDir, &fileExt);
    printf("Full path: %s\nFilename: %s\n", szDir, fileExt);

    return 0;
}

I use example from here and here , but I've got an error message: Argument of type “char *” is incompatible with parameter of type “LPWSTR”.

Where is my mistake?

A string defined as below is called an ANSI string.

char* fileExt = NULL;

And a string defined as below can be an ANSI string or a Unicode string. Your project is compiled with the UNICODE/_UNICODE macro, so it is a Unicode string.

TCHAR szDir[MAX_PATH];

You can't mix them together, for an introduction to the data type identifiers in VC++ like TCHAR and LPCTSTR , please refer to this article .

I made several modifications to your code, as following.

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    TCHAR *fileExt = NULL; 
    TCHAR szDir[MAX_PATH];
    GetFullPathName(argv[0], MAX_PATH, szDir, &fileExt);
    _tprintf(_T("Full path: %s\nFilename: %s\n"), szDir, fileExt); 
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM