Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

Incorrect conversion function result [C]

$
0
0

I am new to C programming language, so apologize if I do something totally wrong. :)

I want to convert byte string to wide string to use it in CopyFileW. When I do everything in main function, all works perfectly. But as soon as I move all conversion to separated function, the program is crashed.

Here's my code:

#define USE_TEST
#include <stdio.h>
#include <wtypes.h>
#include <Windows.h>
#include <tchar.h>

void GetUnicodeString(char* ansiString, LPWSTR wideString)
{
    int len = lstrlenA(ansiString);
    wideString = SysAllocStringLen(NULL, len);
    MultiByteToWideChar(CP_ACP, 0, ansiString, len, wideString, len);
}

#ifdef USE_TEST
int main(int argc, char* argv[])
{
    LPWSTR wideString = 0;
    printf_s("parameter = %s\n", argv[1]);
    GetUnicodeString(argv[1], wideString);
    if (wideString == NULL)
        printf_s("wideString is NULL\n");
    else
        _tprintf_s(L"wideString = %s", wideString);
    return EXIT_SUCCESS;
}
#else
int main(int argc, char* argv[])
{
    int len;
    LPWSTR f1 = 0, f2 = 0;
    len = lstrlenA(argv[1]);
    f1 = SysAllocStringLen(NULL, len);
    MultiByteToWideChar(CP_ACP, 0, argv[1], len, f1, len);
    //GetUnicodeString(argv[1], f1);

    len = lstrlenA(argv[2]);
    f2 = SysAllocStringLen(NULL, len);
    MultiByteToWideChar(CP_ACP, 0, argv[2], len, f2, len);
    //GetUnicodeString(argv[2], f2);

    printf_s("copying file...\n");
    if (CopyFileW(f1, f2, FALSE))
        printf_s("file copied successfully");
    else
        printf_s("copy error: %x", GetLastError());

    return 0;
}
#endif

The non-test version work perfectly - it copies file successfully. But if I use test main, then I get "wideString is NULL". I guess, it's something with pointers. :) Please, help me to correct error. Thanks beforehand!


There is no knowledge that is not power.


Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>