简体   繁体   English

为什么我找不到目录名称

[英]Why I can't get directory name

My program, where I work with Win API: 我使用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”. 我在这里这里使用示例,但是我收到一条错误消息:“ char *”类型的参数与“ LPWSTR”类型的参数不兼容。

Where is my mistake? 我的错误在哪里?

A string defined as below is called an ANSI string. 如下定义的字符串称为ANSI字符串。

char* fileExt = NULL;

And a string defined as below can be an ANSI string or a Unicode string. 并且如下定义的字符串可以是ANSI字符串或Unicode字符串。 Your project is compiled with the UNICODE/_UNICODE macro, so it is a Unicode string. 您的项目是使用UNICODE/_UNICODE宏编译的,因此它是Unicode字符串。

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 . 您不能将它们混合在一起,有关VC ++中的数据类型标识符(例如TCHARLPCTSTR ,请参阅本文

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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