-1

I am debugging some old C code written in our company. The function is something like this --

uint32
ExecuteCommand(wchar_t *dnsName,           // IN
                                wchar_t *dnsUser,   // IN
                                PNameValue *nameValues, // OUT
                                size_t *valuesSize)     // OUT
{
   ....

To debug this, I need to examine the values of dnsUser and dnsName being passed into this function. I tried the below options --

    Util_Log(L"Printing the userName %S", dnsUser);
    Util_Log(L"Printing the userName ", dnsUser);
    Util_Log(L"Printing the userName %ls", dnsUser);
    Util_Log(L"Printing the userName %lS",dnsUser);

Util_Log is a proprietary logging API used in our company. However, none of the above is working and the userName is getting printed in some garbled form in the generated logs. I tried using printf and wprintf but that didnt print anything in the logs--

  printf("Printing the userName %S",userName);
  wprintf("Printing the userName %lS  %S", userName);

Just to add: Below is the signature of SimCfg_Log API.

void
Util_Log(const wchar_t *fmt,  // IN
         ...)                   // IN 

What is the correct way to print the wchar_t arguments in the method ? Also, any idea why using printf and wprintf is not printing anything in the logs?

user496934
  • 3,822
  • 10
  • 45
  • 64

2 Answers2

2

My psychic powers suggest this is all you need to do:

Util_Log(L"Printing the userName %s", dnsUser);

My psychic powers are aided by two hints:

The first hint being that the the first param to Util_Log is a wide-char (L"") string. Hence the format specifier, %s, would also get interpreted as a wide-char string.

The the second hint is that it's he only format specifier you didn't mention as having already tried. ;)

selbie
  • 100,020
  • 15
  • 103
  • 173
  • Thanks for your comments, but it didnt work, FYI the signature of the method is this,-void Util_Log(const wchar_t *fmt, // IN ...) , so I was thinking it should work – user496934 Sep 25 '19 at 09:48
  • 2
    That's the problem with psychic powers, they sometimes fail you. Obviously, anyone on your team would suggest you consult with the owner of the `Util_Log` API within your company or inspect the source code yourself. Are you saying you have no access to either and must link with a pre-compiled lib? Otherwise, your last resort is to do what Mathieu suggest below. `swprintf` to a temp buffer. Then pass that to your logging function. – selbie Sep 25 '19 at 22:36
1

You could not use the formatting options of your internal function and rely on swprintf:

#define SIZE 256
wchar_t buffer[SIZE];
swprintf(buffer, SIZE, L"Printing the userName %s",userName);

Util_Log(buffer);
Mathieu
  • 8,840
  • 7
  • 32
  • 45
  • Since the OP indicated my suggestion did not work, I'm upvoting this answer. My advice to OP is to be careful with string lengths (use snprintf) and when `userName` itself has a `%s` escape sequence. I'd hate for your logging statement to become a security hole. – selbie Sep 25 '19 at 22:38