I am writing a game. I am planning to store saves in the "saved games" directory.
How to find the location of the Saved Games folder programmatically?
It needs to work on non-English Windows. Hacks like %USERPROFILE%\Saved Games are not an option.
The Saved Games directory can be located with the SHGetKnownFolderPath() function, available since Windows Vista and Windows Server 2008.
Note that the FOLDERID_SavedGames argument is a C++ reference. Replace with &FOLDERID_SavedGames to call from C code.
Tested successfully on the first online MSVC compiler I could find:
https://rextester.com/l/cpp_online_compiler_visual
#define WINVER 0x0600
#define _WIN32_WINNT 0x0600
#include <stdio.h>
#include <shlobj.h>
#include <objbase.h>
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "ole32.lib")
int main(void)
{
PWSTR path = NULL;
HRESULT r;
r = SHGetKnownFolderPath(FOLDERID_SavedGames, KF_FLAG_CREATE, NULL, &path);
if (path != NULL)
{
printf("%ls", path);
CoTaskMemFree(path);
}
return 0;
}
Related
I'm using the VCL tools in C++Builder 11 for Windows desktop development. I am trying to get the C functions opendir and readdir to work in a 64-bit app. I have read the IBM website (link below) about the 64-bit version of opendir and readdir but I can't configure my code to work with the 64-bit versions. The code below shows a single button app with code that reads and displays the name of each file in a folder. This works as a 32-bit app. On the 64-bit platform this code fails in the while loop calling readdir. Can you show how to adjust this code so it works on the 64-bit VCL platform in C++Builder.
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <dirent.h>
#include <System.SysUtils.hpp>
//-----------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//----------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
DIR *pDir;
struct dirent *dirU;
pDir = opendir( "C:\\test\\" );
if (pDir == NULL) {
ShowMessage("error");
}else{
int count = 1;
while ((dirU = readdir(pDir)) != NULL) //fails here
{
ShowMessage(dirU->d_name);
count++;
}
}
}
//------------------------
IBM 64 bit opendir and readdir
I'm at a loss why you are looking at the IBM site, feels like a flashback of a long long time ago. Anyway, why not use native functions like GetFiles? See https://docwiki.embarcadero.com/Libraries/Alexandria/en/System.IOUtils.TDirectory.GetFiles
I have the following piece of code which I am trying to build statically, so I end up with a single executable.
#define LIBSSH_STATIC 1
#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <errno.h>
#include <string.h>
#pragma comment(lib, "mbedcrypto.lib")
#pragma comment(lib, "pthreadVSE3.lib")
#pragma comment(lib, "ssh.lib")
#pragma comment(lib, "Ws2_32.lib")
int main()
{
ssh_session my_ssh_session;
int method, rc;
int port = 22;
const char* password;
int verbosity = SSH_LOG_FUNCTIONS;
//int stricthostcheck = 0;
std::string host = "10.10.10.100";
std::string user = "user";
// Open session and set options
my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, host.c_str());
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, user.c_str());
ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
// Connect to server
rc = ssh_connect(my_ssh_session);
if (rc != SSH_OK)
{
fprintf(stderr, "Error: %s\n", ssh_get_error(my_ssh_session));
ssh_free(my_ssh_session);
exit(-99);
}
// Authenticate ourselves
password = "Password";
rc = ssh_userauth_password(my_ssh_session, NULL, password);
if (rc != SSH_AUTH_SUCCESS)
{
fprintf(stderr, "Error authenticating with password: %s\n",
ssh_get_error(my_ssh_session));
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
exit(-1);
}
ssh_disconnect(my_ssh_session);
ssh_
free(my_ssh_session);
}
I have installed the following libraries using VCPKG
libssh:x86-windows-static
zlib:x86-windows-static
openssh:x86-windows-static
I have manually linked the following include path, in the C/C++ section of project properties on the General tab under Additional include directories
C:\dev\vcpkg\installed\x64-windows-static\include
I have also under in the Linker section of project properties, also on its General tab, added an entry for Additional library directories
C:\dev\vcpkg\installed\x64-windows-static\lib
The additional libraries are linked in the code, using the following four lines of code:
#pragma comment(lib, "mbedcrypto.lib")
#pragma comment(lib, "pthreadVSE3.lib")
#pragma comment(lib, "ssh.lib")
#pragma comment(lib, "Ws2_32.lib")
I have also set the C/C++, Code Generation, Runtime option to Multi-Threaded (/MT)
When I run the program it compiles fine, creating a single executable. However, when I run the program, I get an error stating "ssh_connect: Library not initialized"
This is day three of trying to get this to work, with no previous knowledge of how to compile applications. Any help greatly appreciated :)
3 days spent on guessing instead of reading the manual - it's unbelievable. Almost on the top:
If libssh is statically linked, threading must be initialized by calling
ssh_init() before using any of libssh provided functions. This initialization
must be done outside of any threading context. Don't forget to call
ssh_finalize() to avoid memory leak
By the way, any examples of libbssh usage have calls to ssh_init() and ssh_finalize(). You can look at the unit tests.
Im using Visual Studio 2015 and want to export my project which only includes the .cpp file and a .wav file which supposed to be played. How do i export the exe including the .wav file
#include <Windows.h>
#include <conio.h>
#include <iostream>
using namespace std;
#include <stdio.h>
# include <winuser.h>
#pragma comment(lib, "winmm.lib")
int main()
{
FreeConsole();
while (1)
{
for (char button = 0; button < 256; button++)
{
if (GetAsyncKeyState(button) & 0x8000)
{
PlaySound(TEXT("dab.wav"), NULL, SND_FILENAME);
}
}
Sleep(5);
}
}
You can add your .wav file as resource of your project.
see documentation here: https://msdn.microsoft.com/en-us/library/8fc1e5by.aspx
Then you can play your sound from resource id.
see documentation here: https://msdn.microsoft.com/en-us/library/windows/desktop/dd743679(v=vs.85).aspx
I have a LPCSTR that I want to convert to std::string or char*.
LPCSTR strName;
_tprintf(_T("%s\n"), strName);
I'm trying to get all the audio equipment on the computer and displayed, but to get the LPCSTR type of direct output is garbled, use the above code to output the correct results.
Is there a way to save the correct output?
The following is the complete code:
Add a dependency to the property:
comctl32.lib;winmm.lib;dsound.lib;dxguid.lib;odbc32.lib;odbccp32.lib
ListSoundDev.h
#ifndef _LISTSOUNDDEV_HEAD_
#define _LISTSOUNDDEV_HEAD_
#include<tchar.h>
#include <dshow.h>
#include<iostream>
#include<vector>
#include<mmsystem.h>
#include<mmreg.h>
#include<dsound.h>
#pragma comment(lib, "strmiids.lib")
#pragma comment(lib, "Quartz.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "dsound.lib")
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "strmiids")
using namespace std;
typedef struct _DevItem
{
LPCSTR strName;
GUID guid;
} DevItem;
#endif
main.cpp
#include"ListSoundDev_head.h"
std::vector<DevItem> m_CapDevices;
BOOL CALLBACK DSEnumCallback(LPGUID lpGuid, LPCSTR lpcstrDescription, LPCSTR lpcstrModule, LPVOID lpContext)
{
std::vector<DevItem> *pLst = (std::vector<DevItem> *) lpContext;
if (pLst)
{
DevItem item;
memset(&item, 0, sizeof(item));
item.strName = lpcstrDescription;
if (lpGuid)
item.guid = *lpGuid;
else
item.guid = GUID_NULL;
pLst->push_back(item);
return TRUE;
}
return FALSE;
}
int main()
{
std::vector<DevItem>::iterator it;
HRESULT hr = S_OK;
setlocale(LC_ALL, "chs");
hr = DirectSoundCaptureEnumerate((LPDSENUMCALLBACKW)DSEnumCallback, (LPVOID)&m_CapDevices);
for (it = m_CapDevices.begin(); it != m_CapDevices.end(); it++){
_tprintf(_T("%s\n"), it->strName);//output correct
printf("%s\n",it->strName);//output error
std::cout << it->strName << std::endl;//output error
}
}
Expected output:
麦克风 (Realtek High Definition Au
Actual output:
KQ螛
Expected output:
Realtek Digital Input (Realtek
Actual output:
R
How can I that printf() or std::cout can directly output the correct results?
Thank you very much for your answer, I have solved this problem.
The solution is to use the following code to convert.
The character set is Unicode.
char newStr[100];
wcstombs(newStr, (wchar_t*)it->strName, 100);
printf("newStr=%s\n", newStr);
The character set is Multibyte
printf("%s\n", it->strName);
However, when I write this program in the C + + console to obtain the name of the audio device for the microphone (Realtek High Definition Au, but in MFC using this code to get the device name for the microphone (Realtek High Definition Audio) What is the reason?
I would like to change user password on my Windows 7 PC using C++.
But when I compile it gets error:
undefined reference to 'NetUserChangePassword'
[Error] ld returned 1 exit status.`
How can I fix it?
Here is the MSDN page with the NetUserChangePassword function:
#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "netapi32.lib")
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <lm.h>
bool ChangeUserPassword(LPCWSTR OldPassword, LPCWSTR NewPassword)
{
NET_API_STATUS nStatus;
LPTSTR lp = new TCHAR[256];
DWORD dw = 256;
GetUserName(lp, &dw);
nStatus = NetUserChangePassword(NULL, lp, OldPassword, NewPassword);
delete[] lp;
if (nStatus == NERR_Success)
return true;
return false;
}
int main(int argc, char** argv)
{
LPCWSTR Old_P = L"C";
LPCWSTR New_P = L"D";
ChangeUserPassword(Old_P, New_P);
return 0;
}
I tried to link to the project the winapi32.dll in two ways
i tried to add using the project option
i tried to add following line
HINSTANCE hInst = LoadLibrary( L"C:\\Windows\\System32\\netapi32.dll ");
but i get always the same error
The requirements section of the MSDN topic you linked to states that you must link the Netapi32.lib library. That is the step that you have missed and explains the missing external error.
As to how to resolve the problem it is hard to say for sure. You are not using the MS compiler and so the #pragma approach won't work. Consult the docs for your compiler/linker to work out how to link this library.
It looks like you are using a GCC based compiler and so need to add -lnetapi32 to the options.