Compiler cannot convert the arguments passed to swprintf function, winapi - visual-studio-2010

can you help me with this code? I cannot fix it. There is problem with swprintf function.
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
LPTSTR strPrincipal = new TCHAR[20],
strInterest = new TCHAR[20], strPeriods = new TCHAR[20],
strInterestEarned = new TCHAR[20], strAmountEarned = new TCHAR[20];
double Principal, AnnualRate, InterestEarned;
double FutureValue, RatePerPeriod;
int NumberOfPeriods, CompoundType;
double i;
int n;
swprintf(strInterestEarned, "$%.2f", InterestEarned);
swprintf(strAmountEarned, "$%.2f", FutureValue);
}
Here are errors:
Error 1 error C2665: 'swprintf' : none of the 2 overloads could convert all the argument types
Error 2 error C2665: 'swprintf' : none of the 2 overloads could convert all the argument types

I think you've got your parameters wrong / in the wrong order. Have you checked the MSDN page?
Method signature:
int swprintf(
wchar_t *buffer,
size_t count,
const wchar_t *format [,
argument]...
);
That page gives the following example for swprintf:
// crt_swprintf.c
// wide character example
// also demonstrates swprintf returning error code
#include <stdio.h>
int main( void )
{
wchar_t buf[100];
int len = swprintf( buf, 100, L"%s", L"Hello world" );
printf( "wrote %d characters\n", len );
len = swprintf( buf, 100, L"%s", L"Hello\xffff world" );
// swprintf fails because string contains WEOF (\xffff)
printf( "wrote %d characters\n", len );
}
I think you'd want something along the lines of:
swprintf( strInterestEarned, 20, L"%.2f", InterestEarned );

Related

Wide-character string converted from multibyte string using MultiByteToWideChar print and write to file error

I can print the wide-character string correctly, writing to text file is also OK, using the following code:
const wchar_t wcs[] = L"Hello大家好";
_setmode(_fileno(stdout), _O_WTEXT);
wprintf(L"%ls", wcs);
FILE *pf;
const wchar_t _FileName[] = L"wcs.txt";
pf = _wfopen(_FileName, L"a, ccs=UTF-8");
fwprintf(pf, L"%ls", wcs);
fflush(pf);
fclose(pf);
But I need to convert the multibyte string to wide-character string, using MultiByteToWideChar, and than print or write to text file, error occurred, the result displayed or the text file is all messy code. My code is below.
const char cs[] = "Hello大家好";
wchar_t lpWideCharStr[64];
MultiByteToWideChar(CP_ACP, 0, cs, 0, lpWideCharStr, 64);
wprintf(L"%ls", lpWideCharStr);
FILE *pf;
pf = _wfopen(_FileName, L"a, ccs=UTF-8");
fwprintf(pf, L"%ls", lpWideCharStr);
fflush(pf);
fclose(pf);
Can anybody help? Thanks in advance.
This works for me.
#include <Windows.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
bool ConvertToWideFromUTF8orACP(char* strData, wchar_t* w_strData, int w_strDataLen)
{
int nSize = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, strData, -1, w_strData, w_strDataLen);
if (nSize == 0)
{
nSize = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, strData, -1, w_strData, w_strDataLen);
}
return nSize - 1;
}
int main()
{
char cs[] = "Hello大家好";
printf("%s\n", cs);
wchar_t* lpWideCharStr = new wchar_t[sizeof(cs)];
memset(lpWideCharStr, 0, sizeof(cs));
int nSize = ConvertToWideFromUTF8orACP(cs, lpWideCharStr,sizeof(cs));
_setmode(_fileno(stdout), _O_U16TEXT);
if (nSize > 0)
{
wprintf(L"%s", lpWideCharStr);
}
FILE* pf;
pf = _wfopen(_FileName, L"a, ccs=UTF-8");
fwprintf(pf, L"%ls", lpWideCharStr);
delete[] lpWideCharStr;
fflush(pf);
fclose(pf);
}
Thanks #Remy Lebeau for help:)

I cant print a number inside a formatted MessageBox

The following code does not work as it should. %i does not connect with its variable.
#include<Windows.h>
#include<stdio.h>
#include<tchar.h>
/* SCREENSIZE.C
This project contains the code that Programming Windows
book by Charles Petzold is using.
*/
int cdecl MessageBoxPrintfW(TCHAR *msgBoxTitle, TCHAR *msgBoxFormat, ...)
{
//define buffer
TCHAR msgBoxBuffer[1024];
//define iteration pointer
va_list argPtr;
//start iteration
va_start(argPtr, msgBoxFormat);
/*int _vsnwprintf_s(
wchar_t *const _Buffer,
const size_t _BufferCount,
const size_t _MaxCount,
const wchar_t *const _Format,
va_list _ArgList)
*/
_vsntprintf_s(msgBoxBuffer, sizeof(msgBoxBuffer) / sizeof(TCHAR), (sizeof(msgBoxBuffer) / sizeof(TCHAR)) - 1, msgBoxFormat, argPtr);
//end iteration
va_end(argPtr);
//Use return type to pass preconfigured MessageBox(0,"Text","Title",MB_OKCANCEL);
//Whe we use L before a string example: L"text" it is considered UNICODE format.
return(MessageBox(0, msgBoxFormat, msgBoxTitle, MB_OKCANCEL));
}
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR pCmdLine,
int iCmdShow)
{
int cxScreen, cyScreen;
cxScreen = GetSystemMetrics(SM_CXSCREEN);
cyScreen = GetSystemMetrics(SM_CYSCREEN);
MessageBoxPrintfW(TEXT ("SCREENSIZE"), TEXT("The screen is %i pixels wide by %i pixels high."), cxScreen, cyScreen);
return(0);
}
Why does this happen?
Below is the output that I get:
TCHAR msgBoxBuffer[1024];
_vsntprintf_s(msgBoxBuffer,...
...
return(MessageBox(0, msgBoxFormat, msgBoxTitle, MB_OKCANCEL));
Note that you are writting to msgBoxBuffer, but you never use msgBoxBuffer. So your message box doesn't change anything. Try the following instead:
int MessageBoxPrintfW(const TCHAR *msgBoxTitle, const TCHAR* msgBoxFormat, ...)
{
va_list args;
va_start(args, msgBoxFormat);
int len = _vsctprintf(msgBoxFormat, args) + 1; // add terminating '\0'
TCHAR *buf = new TCHAR[len];
_vstprintf_s(buf, len, msgBoxFormat, args);
int result = MessageBox(0, buf, msgBoxTitle, MB_OKCANCEL);
delete[]buf;
return result;
}
For C version use malloc(len * sizeof(TCHAR)) instead of new, and use free instead of delete[]

System call execvp is not executing

Hello everyone i'm a newbie to system programming,please mind me if my doubt is very vague.
I'm actually following a book named Linux System Programming and I'm having a doubt in execvp() system call.As given by a book example I tried it on my machine and here is the following example ..
#include<unistd.h>
//int ret;
int main(){
const char *args[] = { "vi", "/home/kidd/hooks.txt", NULL };
int ret;
ret = execv ("/bin/vi", args);
if (ret == −1)
perror ("execvp");
}
And i'm receiving a foolwing error:
error: invalid conversion from ‘const char**’ to ‘char* const*’ [-fpermissive]
I've given a const char arrays name which is obviously const char**.
Why is it giving this error?**
The args array should not have type const char*, it should just be char* (in the same way that the argv argument to main is just a char*). So your code should look like this:
#include <unistd.h>
int main() {
char *args[] = { "vi", "/home/kidd/hooks.txt", NULL };
int ret;
ret = execv ("/bin/vi", args);
if (ret == -1)
perror ("execvp");
}

Segmentation fault when changing default gateway

I wrote a simple application on Qt4 that modifier network adapter parameters, for that I have a slot called setInterfaceParams, implemented as so:
DWORD WinNetInterface::setInterfaceParams(QString index, QString ip, QString netmask, QString gateway)
{
DWORD res = NULL;
HINSTANCE lib = (HINSTANCE) LoadLibrary((WCHAR *)"iphlpapi.dll");
_SetAdapterIpAddress SetAdapterIpAddress = (_SetAdapterIpAddress) GetProcAddress(lib, "SetAdapterIpAddress");
PWSTR pszGUID = NULL;
//char *szGUID = (char *)index.toStdString().c_str();
QByteArray a = index.toLocal8Bit();
char *szGUID = a.data();
WideCharToMultiByte(CP_ACP, 0, pszGUID, -1, szGUID, sizeof(szGUID), NULL, NULL);
// Method 01
res = SetAdapterIpAddress(szGUID,
0,
inet_addr(ip.toStdString().c_str()),
inet_addr(netmask.toStdString().c_str()),
inet_addr(gateway.toStdString().c_str()));
// End of method 01
// Method 02
/*res = SetAdapterIpAddress("{422C5689-A17B-402D-A6A2-22CE13E857B5}",
0,
inet_addr("192.168.1.10"),
inet_addr("255.255.255.0"),
inet_addr("192.168.1.1"));*/
// End of method 02
return res;
}
When I click on button that connected to slot setInterfaceParams, I get segmentation fault. If I comment method01, nothing happen, the some thing happen when I use method02.
I tried this function on a simple c++ application and it is work fine, test on Windows XP SP3.
#include <windows.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <iostream>
typedef DWORD (WINAPI *_SetAdapterIpAddress )(char *szAdapterGUID,
DWORD dwDHCP,
DWORD dwIP,
DWORD dwMask,
DWORD dwGateway);
int main()
{
HINSTANCE lib = (HINSTANCE) LoadLibrary("iphlpapi.dll");
_SetAdapterIpAddress SetAdapterIpAddress = (_SetAdapterIpAddress) GetProcAddress(lib, "SetAdapterIpAddress");
PWSTR pszGUID = NULL;
char szGUID[] = "{422C5689-A17B-402D-A6A2-22CE13E857B5}";
DWORD dwSize = 0;
WideCharToMultiByte(CP_ACP, 0, pszGUID, -1, szGUID, sizeof(szGUID), NULL, NULL);
DWORD res = SetAdapterIpAddress(szGUID,
0,
inet_addr("192.168.1.10"),
inet_addr("255.255.255.0"),
inet_addr("192.168.1.1"));
std::cout << res;
return 0;
}
LoadLibrary((WCHAR *)"iphlpapi.dll");
That can't work, the literal string is in 8-bits, casting it without real conversion doesn't make it wide, so the dll loading probably failed.
You should use the TEXT or _T macro around most of the literal strings passed to WinAPI functions to make them regular or wide depending on the compilation options:
LoadLibrary(_T("iphlpapi.dll"));
which will translate to either LoadLibrary("iphlpapi.dll"); or LoadLibrary(L"iphlpapi.dll");.
Also you should always check the value returned by the LoadLibrary and GetProcAddress functions, which return NULL if the call is unsuccessful.

Basic Windows Programming question in VS2005

I created a win32 console application (without enable precompiled header option).
And now my source code as this, there is two compiler errors.
// AFormattingMsgBox.cpp : Defines the entry point for the console application.
//
#include <windows.h>
#include "stdafx.h"
//int _tmain(int argc, _TCHAR* argv[])
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
int cxScreen, cyScreen;
cxScreen = GetSystemMetrics(SM_CXSCREEN);
cyScreen = GetSystemMetrics(SM_CYSCREEN);
MessageBoxPrintf(TEXT("ScrnSize"), TEXT("The screen is %i pixels wide by %i pixels high."), cxScreen, cyScreen);
return 0;
}
int CDECL MessageBoxPrintf(TCHAR * szCaption, TCHAR * szFormat, int x, int y)
{
TCHAR szBuffer [1024];
va_list pArgList;
va_start(pArgList, szFormat);
_vsntprintf(szBuffer, sizeof(szBuffer) / sizeof(TCHAR),
szFormat, pArgList);
va_end(pArgList);
return MessageBox(NULL, szBuffer, szCaption, 0);
}
Compiler Errors.
error C3861: 'MessageBoxPrintf': identifier not found
error C2365: 'MessageBoxPrintf' : redefinition; previous definition was 'formerly
unknown identifier
How can I fix the errors. Thanks for your reading and replies.
Either put the function MessageBoxPrintf before the WinMain function or add a prototype before winMain. You add a prototype by entering the following line:
int CDECL MessageBoxPrintf(TCHAR * szCaption, TCHAR * szFormat, int x, int y);

Resources