-3

Can't figure out how to work with all these "wide unicode strings". Can anyone tell me what am i doing wrong? I Just want to get all local user groups, so i do:

LPBYTE buffer;
DWORD entries, total_entries;
NetUserGetLocalGroups(NULL, L"rovnyart", 0, LG_INCLUDE_INDIRECT, &buffer, MAX_PREFERRED_SIZE, &entries, &total_entries);
LOCALGROUP_USERS_INFO_0 *groups = (LOCALGROUP_USERS_INFO_0 *) buffer;

unsigned int i;
for (i=0; i<entries;i++)
    wprintf(L"%s\n", groups[i].lgrui0_name);

And this is what i get:

t
╝4╝<╝8╝=╝8╝A╝B╝@╝0╝B╝>╝@╝K╝
╝>╝;╝L╝7╝>╝2╝0╝B╝5╝;╝8╝

Process finished with exit code 0

My windows language is Russian, but i created one group called "testgroup1", and as you can see it doesn't display correct too.

i tried wprintf() - result was the same :(

What am i doing wrong?

UPD:

Ok, I changed the code to fit your advices. I created a group called "test" which is non-cyrillic and put my user there.

sorry for non-english text

Here's my code:

LPBYTE pBuf = NULL;

NET_API_STATUS nStatus;
DWORD entries, total_entries;
nStatus = NetUserGetLocalGroups(NULL, L"rovnyart", 0, LG_INCLUDE_INDIRECT,  &pBuf, MAX_PREFERRED_LENGTH, &entries, &total_entries);

LOCALGROUP_USERS_INFO_0 *groups = (LOCALGROUP_USERS_INFO_0 *) pBuf;
if (nStatus == 0) {
    unsigned int i;

    for (i = 0; i < entries; i++)
        wprintf(L"%s\n", groups[i].lgrui0_name);


    NetApiBufferFree(pBuf);
}

Here is the output:

t
╝4╝<╝8╝=╝8╝A╝B╝@╝0╝B╝>╝@╝K╝
╝>╝;╝L╝7╝>╝2╝0╝B╝5╝;╝8╝

Process finished with exit code 0
Rovny Art
  • 167
  • 8
  • 3
    There's fully working sample code in the [documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/aa370655.aspx). – IInspectable Aug 22 '16 at 11:51
  • 1
    I'd note that your code isn't checking for success, isn't zeroing the outputs, and is passing `-1` as `prefmaxlen`. (The documentation's sample uses `MAX_PREFERRED_LENGTH `) – Hasturkun Aug 22 '16 at 11:56
  • Astounding that people attempt to code against an API without reading the documentation first. Does that ever work out well for anybody? – David Heffernan Aug 22 '16 at 12:29
  • i read documentation. sample from there doesn't work on my system too. i am not stupid to ask questions without reading docs first. – Rovny Art Aug 22 '16 at 12:40
  • 1
    At least one thing you are doing wrong is passing a wide char array to '%s' (which wants a narrow char pointer). You'll either need to use `%S`, or `wprintf`. What happens when you fix all the errors that have been identified so far? – Martin Bonner supports Monica Aug 22 '16 at 13:01
  • The sample code uses `wprintf`, where your's goes with `printf`. You don't even have to understand the sample, to get it right. The issue here really is, that you didn't understand the sample code, and tried to change it. That's not going to work, ever. Please read [Unicode in the Windows API](https://msdn.microsoft.com/en-us/library/windows/desktop/dd374089.aspx). All of it. – IInspectable Aug 22 '16 at 14:27
  • ok, i changed code to use wprintf. result is the same - these awful symbols instead of group names. – Rovny Art Aug 22 '16 at 14:40
  • And still, disregard for the documentation. Where did you check the return value of the function call? Perhaps it failed. How would you know? – David Heffernan Aug 22 '16 at 15:31
  • No, function returns zero. the value of `entries` is correct. The problem is that group names are cyrillic, i tried to use `setlocale(LC_ALL, "Russian")` - nothing helpes. – Rovny Art Aug 22 '16 at 15:58
  • 1
    You really need to read [Unicode in the Windows API](https://msdn.microsoft.com/en-us/library/windows/desktop/dd374089.aspx). It's not like cyrillic is somehow different from latin characters. Both are in the BMP. It seems you should also read [The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)](http://www.joelonsoftware.com/articles/Unicode.html). – IInspectable Aug 22 '16 at 16:23
  • 1
    It is very likely that the console window is the problem here. Its Unicode support is lacking out of the box. Use the debugger, insert a breakpoint on the `wprintf` line. What does the buffer contain? – Cody Gray - on strike Aug 22 '16 at 16:31
  • Better now that you added error checking. If this is C++ you need to stop using printf and use iostreams. Then the compiler can help you and make sure the text encoding is interpretation correctly. – David Heffernan Aug 22 '16 at 16:57

1 Answers1

0

Thanks everyone, including guys who downvoted me. The solution of my problem was in %s format specifier. I needed to use %S.My program correctly outputs group names with:

wprintf(L"%S\n", groups[i].lgruiu0_name);

Although this code is error-higlighted in my CLion, it works for some reason.

Thanks for all helpful links.

Rovny Art
  • 167
  • 8
  • This is highly improbable. [LOCALGROUP_USERS_INFO_0](https://msdn.microsoft.com/en-us/library/windows/desktop/aa370282.aspx) stores a wide character string. The `%S` specifier instructs `wprintf` to interpret its string parameter as an MBCS (narrow) string. Is this a bug in your build environment? What results do you get using Visual Studio instead? – IInspectable Aug 22 '16 at 18:34
  • Change it back to `wprintfs(L"%s\n",...)`. Use `_setmode` as suggested [here](http://stackoverflow.com/a/36527398/4603670) for printing Unicode on console. Probably `groups[i].lgruiu0_name` looks okay if you test it with `MessageBoxW` – Barmak Shemirani Aug 22 '16 at 20:44
  • I recommend avoiding the whole mess by using MessageBoxW() for your diagnostic output rather than outputting to the console. – Harry Johnston Aug 22 '16 at 23:45