unable to print string value using sprintf in c++ - c++

i have function that takes two parameters of type LPTSTR, and i am trying print both the values using sprintf like below and i am not able to print the exact values.
int __stdcall Logon(LPTSTR UserName, LPTSTR Password)
{
char Buffer[2048];
sprintf(Buffer,"UserName: %s\n m_Password: %s\n",UserName,Password);
FILE *Ls=fopen("lo.log",a);
fprintf(Ls,Buffer);
fclose(Ls);
}

Either fix "Use Unicode strings" in you project settings or use the
_stprintf(Buffer, _T("UserName: %S\n m_Password: %S\n"), UserName, Password);
and
#include <tchar.h>
If you use Unicode (and you do), use the '%S' format.

Assuming that UserName and Password are stored as wide strings (wchar_t) and you are trying to put them into a char[] buffer, then visual c++ uses %S to accomplish this.
sprintf(Buffer,"UserName: %S\n m_Password: %S\n",UserName,Password);

Related

How do I get username and appname for file path

How am I able to define a path like "C:/Users/<USER>/AppData/Local/<APPNAME>", for different username and app? How do I set this to automatically get the user and the appname? Thank you.
You can use SHGetKnownFolderPath to get the full path of App Local:
...
#include <KnownFolders.h>
#include <ShlObj.h>
...
SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_SIMPLE_IDLIST, NULL, &path); // NULL for current user
...
To get the Local AppData path for a given user, use SHGetFolderPath() specifying CSIDL_LOCAL_APPDATA, or SHGetKnownFolderPath() specifying FOLDERID_LocalAppData. Both take an optional user token for the desired user account to query. If you don't provide a token, the user associated with the calling thread is used.
To get the username:
char username[MAX_PATH];
DWORD size = MAX_PATH;
GetUserName(username,&size);
To get the appname(Executable File Name without ".exe"):
char appname[MAX_PATH];
char buffer[MAX_PATH];
GetModuleFileName(NULL, appname,MAX_PATH); //get the string: "PATH\\appname.exe"
char *szExe = NULL;
//Remove prefix
GetFullPathName(appname, MAX_PATH, buffer, &szExe);
//Remove suffix
strncpy_s(appname, szExe, strlen(szExe) - strlen(".exe"));

NSIS Plugin with bad string

I've tried to develop an NSIS Plugin in C++.
Here is my method to set informations from NSIS:
void __declspec(dllexport) SetCredentials(HWND hWndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra) {
EXDLL_INIT();
server = getuservariable(INST_0);
port = myatou(getuservariable(INST_1));
database = getuservariable(INST_2);
username = getuservariable(INST_3);
password = getuservariable(INST_4);
MessageBox(hWndParent, server, L"Info", MB_OK); // Here is the problem
setuservariable(INST_0, server);
}
Sample:
OutFile "Example.exe"
BrandingText " "
Section
MySQL::SetCredentials "localhost" 3306 "banananode" "root" ""
Pop $0
MessageBox MB_OK "Server: $0" ; Returns the server value...
SectionEnd
The Problem is, when i try to print out the server variable, chinese characters will be shown, not the correct text:
When i return the value with setuservariable(INST_0, server);, NSIS will be display it correctly:
Can you tell me, whats wrong?
I've tried to build the resulted .dll with pluginapi-x86-ansi.lib and pluginapi-x86-unicode.lib but the result is the same...
ANSI characters interpreted as Unicode (UTF-16LE) tends to look Chinese.
When you create a Unicode plug-in you need to:
Make sure UNICODE and _UNICODE is defined in your project/.C files.
Link with pluginapi-x86-unicode.lib
Add Unicode True to your .NSI
Place the plug-in in \NSIS\x86-unicode
In your case you most likely forgot about Unicode True. Returning the value works because you never actually changed the memory of server, it is still the same input string.

C++ ShellExecute a URL

Tried googling for a few hours, testing different solutions for hours but still just cannot get this to work.
I need a const url base (Ex. http://www.google.com)
Then I need a string input from the user (ex. Mountain Dew) and then combine them.
I've tried making the URL a LPCWSTR, wstring, wchar_t, doing a function to convert them and combine them but I cannot get it to work at all.
std::string baseUrl = "http://www.google.com/";
std::string userAdd;
getline(std::cin, userAdd)
ShellExecute(NULL, TEXT("open"), baseUrl + userAdd, NULL, NULL, SW_SHOWNORMAL);
There's no automatic conversion from std::string to const char*.
Try this: (baseUrl + userAdd).c_str()
and try using ShellExecuteA

sprintf() access violation reading location

I don't really understand this, here's what's going on:
char buffer1[100];
sprintf_s(buffer1, "whatever %s", "something");
Works just fine.
But the following doesn't:
char buffer1[100];
sprintf_s(buffer1, "whatever %s %s", "something", "somethingelse");
Error: Unhandled exception Access violation reading location 0x00000005.
I think if I try to split it up with strcpy_s first then use sprintf_s it would work but that would be a waste of everything.
Thanks in advance.
PS: I am using Visual Studio
EDIT:
Code Update, I wrote the wrong thing really fast. Here's the actual code that's giving the error:
sprintf(query, "INSERT INTO `members` (`id`, `username`, `email`) VALUES ('%s', '%s', '%s')", id, username, email);
The problem is that I was trying to append an integer to the char array using %s.
There's 2 solutions to the problem, it's wither you use %d or to_string to make the integer a string value which will fix the problem.
Solution 1:
sprintf(query, "INSERT INTO `members` (`id`, `username`, `email`) VALUES ('%d', '%s', '%s')", id, username, email);
Solution 2:
sprintf(query, "INSERT INTO `members` (`id`, `username`, `email`) VALUES ('%s', '%s', '%s')", to_string(id).c_str(), username, email);

How to print the current user and system name in Unix?

Please i am looking forward to learn how to print the current logged-in user and system name in Unix.
#include <unistd.h>
#include <fcntl.h>
using namespace std;
int main(int argc, char **argv)
{
//Print the current logged-in user / username.
//Print the name of the system / computer name.
return 0;
}
I would be grateful if you can provide a line of code or two as demonstration. Thanks
User --> getuid() (see also geteuid()).
Machine name --> gethostname().
That is pure C. I don't know whether C++ has other library calls for that.
You need to call the uname, gethostname, getuid (and perhaps getgid) system calls, and to convert the numerical uid with getpwent function.
getuid() gets the id not the username. To get the username you'll have to additionally use getpwuid():
struct passwd *passwd;
passwd = getpwuid ( getuid());
printf("The Login Name is %s ", passwd->pw_name);
See it
And for getting the hostname you can use the gethostname() function.