2

Using Multibyte character set in building the example from MSDN we get the "initializer is not a constant" error for the last line. VS10 SP1, no CLR.

        #define arraysize 30
        TCHAR pszDest[arraysize]; 
        size_t cbDest = arraysize * sizeof(TCHAR);

        LPCTSTR pszFormat = TEXT("%s %d + %d = %d.");
        TCHAR* pszTxt = TEXT("The answer is");

        HRESULT hr = StringCbPrintf(pszDest, cbDest, pszFormat, pszTxt, 1, 2, 3);

        // The resultant string at pszDest is "The answer is 1 + 2 = 3."

Other CLR options fail. Anyway of removing the error?

Laurie Stearn
  • 959
  • 1
  • 13
  • 34
  • 1
    The code snippet you used is marked with C++, not C. Rename your source code file from .c to .cpp – Hans Passant Aug 27 '15 at 15:36
  • Hans, I am deeply honoured. :) Ah yes, getting used to the "silent phase out" of C by Microsoft. Have to be careful here.The project was based on theForger's excellent [API tutorial](http://www.winprog.org/tutorial/dialogs.html) solution and vcproj (erroneously not vcxproj) files. My answer here is based on creating a WIN32 project in VS. – Laurie Stearn Aug 30 '15 at 12:09
  • It is not "phased out", VS2013 and up support C99 syntax. – Hans Passant Aug 30 '15 at 12:29

1 Answers1

0

The C based code (viz Hans' comment) can function in a VS Win32 project. The module-wide declarations are:

#include <windows.h>
#include "stdafx.h"
#include <Strsafe.h> 
#include <stdio.h>
#include "msgbox.h"
int hr = 0;
#define arraysize 30
wchar_t hrtext[128];
HWND hWnd;

The code was inserted in the _tWinMain procedure:

    TCHAR pszDest[arraysize]; 
    size_t cbDest = arraysize * sizeof(TCHAR);

    LPCTSTR pszFormat = TEXT("%s %d + %d = %d.");
    TCHAR* pszTxt = TEXT("The answer is");

    HRESULT hr = StringCbPrintf(pszDest, cbDest, pszFormat, pszTxt, 1, 2, 3);

                if(hr == S_OK)
                {
                MessageBox(hWnd, pszDest, L"Information", MB_OK);

                }
                else
                {

                swprintf_s(hrtext, sizeof(hrtext), L"StringCbPrintf didn't work, quitting: code %#08X" , hr);

                MessageBox(hWnd, hrtext, L"Warning", MB_OK);
                }
    EndDialog(hWnd, 0);
    return FALSE;
Laurie Stearn
  • 959
  • 1
  • 13
  • 34