1

I am trying to use GetFullPathName to get the full path of a file, but I have errors. The error is:

cannot convert argument 1 from "char *" to 'LPCWSTR'.

Could someone help me? Here's the code:

int main(int argc, char **argv)
{
    char* fileExt;
    char szDir[256];
    GetFullPathName(argv[0], 256, szDir, &fileExt);
}
David Hoelzer
  • 15,862
  • 4
  • 48
  • 67
Sebi95
  • 161
  • 4
  • 13

1 Answers1

1

The main problem is that you using ANSI strings instead of UNICODE-UTF16. The main solution is to use TCHAR compatible entry point _tmain, it will be compatible either with UNICODE or ANSI consider project settings as the same GetFullPathName will be GetFullPathNameA for ANSI configuration and GetFullPathNameW for UNICODE.

Example

int _tmain(int argc, _TCHAR* argv[])
{
    TCHAR* fileExt;
    TCHAR szDir[256];

    GetFullPathName(argv[0], 256, szDir, &fileExt);

    return 0;
}

To display ANSI or UNICODE string to your program you may use this statement in the begining of your main function

#if defined(UNICODE) || defined(_UNICODE)
#define consoleOut  std::wcout
#else
#define consoleOut  std::cout
#endif

than display your strings as

consoleOut << szDir << std::endl;

the overall program will be

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

#if defined(UNICODE) || defined(_UNICODE)
#define consoleOut  std::wcout
#else
#define consoleOut  std::cout
#endif

int _tmain(int argc, _TCHAR* argv[])
{
    TCHAR* fileExt;
    TCHAR szDir[256];

    GetFullPathName(argv[0], 256, szDir, &fileExt);

    consoleOut << szDir << std::endl;

    return 0;
}

And the result.

enter image description here

Mykola
  • 3,343
  • 6
  • 23
  • 39
  • Yes, now i don`t have any errors, but when i ran, my output is a adress suchs us: 001BFBE8 – Sebi95 Dec 18 '15 at 23:56
  • @Sebi95: How did you output your string? – Mykola Dec 18 '15 at 23:58
  • @Sebi95: It is certanly because you using a wrong version of output string (possible ASCII only compatible `printf` or `cout`. – Mykola Dec 19 '15 at 00:00
  • How should i make the output? – Sebi95 Dec 19 '15 at 00:02
  • No, my output is made with cout, as u said. How should i do? – Sebi95 Dec 19 '15 at 00:03
  • @Sebi95: Wait for a minute. – Mykola Dec 19 '15 at 00:06
  • I've tried with wcout, but it doesn`t show anything at the console. – Sebi95 Dec 19 '15 at 00:08
  • You should say ANSI, not ASCII. ANSI means any one of several characters sets with between 128 and 256 characters. Which character set it is depends on the OS language/user language and can be changed by the process or thread. (It's probably not [code page 20127](https://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx)). – Tom Blodget Dec 19 '15 at 00:16
  • I rlly thx u for trying to help me, but my code refuses to work...it works with that wcout, but the output is nothing... – Sebi95 Dec 19 '15 at 00:19
  • @Sebi95: Its realy strange that it not works. Did you try to output incoming `arg[0]` without getting full path? – Mykola Dec 19 '15 at 00:24
  • @Sebi95: If you didnot suppose to use your application in unicode environment you can still disable it. And work with ASCII or MBCS. – Mykola Dec 19 '15 at 00:28
  • The idea is that i didn`t need to use it in unicode environment but i was asking to make a function for getting the full path of a file, and i made what i shew to you. I need tchar for getfullpathname, is the only place where i need....i don`t know what to do – Sebi95 Dec 19 '15 at 00:33
  • @Sebi95: You can use ASCII version of `GetFullPathName` - `GetFullPathNameA` it must work with simple `main`. – Mykola Dec 19 '15 at 00:35
  • @Sebi95: But I still think that is something wrong with `arg[0]`, and strongly recomend you to make some debug output such as `consoleOut << argv[0] << std::endl;` to test correctness of your args. – Mykola Dec 19 '15 at 00:38
  • With argv[0] there is not any problems, it works. I have problems with argv[1], argv[2] etc.. – Sebi95 Dec 19 '15 at 00:48
  • @Sebi95: Than try to output them. In for loop for example. – Mykola Dec 19 '15 at 00:51
  • @Sebi95: Ensure that your args is in the normal state. If not that OS didnot post comand args to app, and this is nothing wrong with your code. – Mykola Dec 19 '15 at 00:54