2

I get the following error :

argument of type "const char *" is incompatible with parameter of type "LPCWSTR"

Here is my code :

static char *getFmuPath(const char *fileName) {
    char pathName[MAX_PATH];
    int n = GetFullPathName(fileName, MAX_PATH, pathName, NULL);
    return n ? strdup(pathName) : NULL;
}

I have declared MAX_PATH but it still shows error in pathname

#define MAX_PATH 4096

What is the problem ?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
PrajwalBhat
  • 51
  • 2
  • 3
  • 10
  • 1
    You basically have two options. 1. Avoid `TCHAR*`, `LPTSTR`, `LPCTSTR` and friends, or any APIs that reference them in any form. If you see any such API, use its ...W or ...A counterpart explicitly (`GetFullPathNameA` in this case). 2. Avoid `char*` and `wchar_t*`, use `TCHAR*`. When you need to interface `TCHAR*` with `char*` and `wchar_t*`, use explicit conversions. Option 2 is less recommended. – n. m. could be an AI Jul 13 '15 at 10:34
  • i have done changing from **char** to **TCHAR** now the the error is gone but now in **strdup(pathname)** it is showing same error – PrajwalBhat Jul 13 '15 at 10:47
  • Your program would benefit from choosing between C or C++. Mixing the two tends to produce a program that's suffers from the worst of the two languages, and gets few of the benefits of either. – MSalters Jul 13 '15 at 11:16
  • "now in strdup(pathname)" That's because `strdup` is a `char*` function. That is, you have not finished replacing `char` yet. The `TCHAR*` counterpart would be `_tcsdup`. BTW have you missed the remark that Option 2 is less recommended? Let me say it again so you won't miss it: **Option 2 is less recommended**. – n. m. could be an AI Jul 13 '15 at 14:45

3 Answers3

5

GetFullPathName doesn't take a char *. Look at the docs, it takes LPTSTR and LPCTSTR.

Depending on your build settings, LPTSTR and related types will become either char* (ANSI builds) or wchar_t* (Unicode builds). You are building as Unicode.

Also, I don't know why you are defining MAX_PATH. That is a Windows constant so you should not re-define it.

tenfour
  • 36,141
  • 15
  • 83
  • 142
0

I agree with @tenfour, if you would still like to enforce your system work with ANSI chars so char* would work, change your code to call directly GetFullPathNameA
Or, better yet, use unicode character set under Project->Properties->Configuration Properties->General->Character Set.

rkellerm
  • 5,362
  • 8
  • 58
  • 95
0

I was having the same problem (VS2017). Changed the project to compile 32-bit and it compiled great! My problem was that I deleted all the extra files in the directory to clean it up and it defaulted to 64-bit because I was building on a 64-bit machine.

Eric
  • 1