Visual C++ DLL that checks if file exists - c++

I made this dll file that tries to check if a file exists. But even if I manually create the file, my dll still can't find it.
My dll retrieves the process id of the running program and looks for a file that is named after the pid.
Can anyone please tell me what I'm missing :(
Code:
#include <Windows.h>
#include <winbase.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int clientpid = GetCurrentProcessId();
ifstream clientfile;
string clientpids, clientfilepath;
VOID LoadDLL() {
AllocConsole();
freopen("CONOUT$", "w", stdout);
std::cout << "Debug Start" << std::endl;
std::ostringstream ostr;
ostr << clientpid;
clientpids = ostr.str();
ostr.str("");
TCHAR tempcvar[MAX_PATH];
GetSystemDirectory(tempcvar, MAX_PATH);
ostr << tempcvar << "\\" << clientpids << ".nfo" << std::endl;
clientfilepath = ostr.str();
//clientfile.c_str()
ostr.str("");
std::cout << "Start search for: " << clientfilepath << std::endl;
FOREVER {
clientfile.open(clientfilepath,ios::in);
if(clientfile.good()) {
std::cout << "Exists!" << std::endl;
}
Sleep(10);
};
}

Supposing you are working with UNICODE
I think the problems goes in the following line:
ostr << tempcvar << "\\" << clientpids << ".nfo" << std::endl; The tempcvar is a tchar, and maybe you are working with unicode, so it means tempcvar is a widechar.
The result that you get inserting tempcvar in ostr is not what you are expecting (You are mixing multibyte with widechar too). A solution to this problem is converting tempcvar into a multi byte string (const char* or char*...)
Look at this example based on your code (Look at the convertion between tchar to multibyte char)
VOID LoadDLL() {
AllocConsole();
freopen("CONOUT$", "w", stdout);
std::cout << "Debug Start" << std::endl;
std::ostringstream ostr;
ostr << clientpid;
clientpids = ostr.str();
ostr.str("");
TCHAR tempcvar[MAX_PATH];
GetSystemDirectory(tempcvar, MAX_PATH);
// Convertion between tchar in unicode (wide char) and multibyte
wchar_t * tempcvar_widechar = (wchar_t*)tempcvar;
char* to_convert;
int bytes_to_store = WideCharToMultiByte(CP_ACP,
0,
tempcvar_widechar,
-1,NULL,0,NULL,NULL);
to_convert = new char[bytes_to_store];
WideCharToMultiByte(CP_ACP,
0,
tempcvar_widechar,
-1,to_convert,bytes_to_store,NULL,NULL);
// Using char* to_convert that is the tempcvar converted to multibyte
ostr << to_convert << "\\" << clientpids << ".nfo" << std::endl;
clientfilepath = ostr.str();
//clientfile.c_str()
ostr.str("");
std::cout << "Start search for: " << clientfilepath << std::endl;
FOREVER {
clientfile.open(clientfilepath,ios::in);
if(clientfile.good()) {
std::cout << "Exists!" << std::endl;
}
Sleep(10);
};
}
You can search more about the wide string to multibyte string convertion if this example does not works to you.
Check if you are working with Unicode, if you are, maybe this is your problem.
If you are not working with unicode, the problem in your code can be opening the file.
Hope it helps!

Related

Get current system locale on Windows 10 in C++?

I'm trying to get the current system locale on Windows 10 in C++.
#include <locale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "");
cout << "LC_ALL: " << setlocale(LC_ALL, NULL) << endl;
cout << "LC_CTYPE: " << setlocale(LC_CTYPE, NULL) << endl;
return 0;
}
It returns
LC_ALL: Chinese (Traditional)_Taiwan.950
LC_CTYPE: Chinese (Traditional)_Taiwan.950
I wonder if it possible to show as BCP47 language tags like zh-TW ?

How to read data from AVRO file using C++ interface?

I'm attempting to write a simple program to extract some data from a bunch of AVRO files. The schema for each file may be different so I would like to read the files generically (i.e. without having to pregenerate and then compile in the schema for each) using the C++ interface.
I have been attempting to follow the generic.cc example but it assumes a separate schema where I would like to read the schema from each AVRO file.
Here is my code:
#include <fstream>
#include <iostream>
#include "Compiler.hh"
#include "DataFile.hh"
#include "Decoder.hh"
#include "Generic.hh"
#include "Stream.hh"
const std::string BOLD("\033[1m");
const std::string ENDC("\033[0m");
const std::string RED("\033[31m");
const std::string YELLOW("\033[33m");
int main(int argc, char**argv)
{
std::cout << "AVRO Test\n" << std::endl;
if (argc < 2)
{
std::cerr << BOLD << RED << "ERROR: " << ENDC << "please provide an "
<< "input file\n" << std::endl;
return -1;
}
avro::DataFileReaderBase dataFile(argv[1]);
auto dataSchema = dataFile.dataSchema();
// Write out data schema in JSON for grins
std::ofstream output("data_schema.json");
dataSchema.toJson(output);
output.close();
avro::DecoderPtr decoder = avro::binaryDecoder();
auto inStream = avro::fileInputStream(argv[1]);
decoder->init(*inStream);
avro::GenericDatum datum(dataSchema);
avro::decode(*decoder, datum);
std::cout << "Type: " << datum.type() << std::endl;
return 0;
}
Everytime I run the code, no matter what file I use, I get this:
$ ./avrotest twitter.avro
AVRO Test
terminate called after throwing an instance of 'avro::Exception'
what(): Cannot have negative length: -40 Aborted
In addition to my own data files, I have tried using the data files located here: https://github.com/miguno/avro-cli-examples, with the same result.
I tried using the avrocat utility on all of the same files and it works fine. What am I doing wrong?
(NOTE: outputting the data schema for each file in JSON works correctly as expected)
After a bunch more fooling around, I figured it out. You're supposed to use DataFileReader templated with GenericDatum. With the end result being something like this:
#include <fstream>
#include <iostream>
#include "Compiler.hh"
#include "DataFile.hh"
#include "Decoder.hh"
#include "Generic.hh"
#include "Stream.hh"
const std::string BOLD("\033[1m");
const std::string ENDC("\033[0m");
const std::string RED("\033[31m");
const std::string YELLOW("\033[33m");
int main(int argc, char**argv)
{
std::cout << "AVRO Test\n" << std::endl;
if (argc < 2)
{
std::cerr << BOLD << RED << "ERROR: " << ENDC << "please provide an "
<< "input file\n" << std::endl;
return -1;
}
avro::DataFileReader<avro::GenericDatum> reader(argv[1]);
auto dataSchema = reader.dataSchema();
// Write out data schema in JSON for grins
std::ofstream output("data_schema.json");
dataSchema.toJson(output);
output.close();
avro::GenericDatum datum(dataSchema);
while (reader.read(datum))
{
std::cout << "Type: " << datum.type() << std::endl;
if (datum.type() == avro::AVRO_RECORD)
{
const avro::GenericRecord& r = datum.value<avro::GenericRecord>();
std::cout << "Field-count: " << r.fieldCount() << std::endl;
// TODO: pull out each field
}
}
return 0;
}
Perhaps an example like this should be included with libavro...

How to fix 'file keeps the original location and after being copied doesnt copy extension'?

I am trying to create a program which copy itself while it is running to new location without keeping the orignal file location . once it is copied I got the file without extension , but how can I overcome this ?
int _tmain(int argc, _TCHAR* argv[])
{
TCHAR szFilepath[MAX_PATH];
TCHAR szFilename[MAX_PATH];
TCHAR szDestpath[MAX_PATH];
/* Get the current executable's full path */
GetModuleFileName(NULL, szFilepath, MAX_PATH);
std::wcout << "filepath: " << szFilepath << std::endl;
/* Extract just the name */
GetFileTitle(szFilepath, szFilename, MAX_PATH);
std::wcout << "filename: " << szFilename << std::endl;
//Set the destination folder path
_tcscpy(szDestpath, L"D:\\");
//Set the destination file path
_tcscat(szDestpath, szFilename);
std::wcout << "dest path: " << szDestpath << std::endl;
// copys the file of your '.exe'
if (!CopyFile(szFilepath, szDestpath, FALSE)) {
std::cout << "couldnt copy the file";
}
else {
std::cout << "copied";
}
return 0;
}
Per the documentation for GetFileTitle():
GetFileTitle returns the string that the system would use to display the file name to the user. The display name includes an extension only if that is the user's preference for displaying file names. This means that the returned string may not accurately identify the file if it is used in calls to file system functions.
You should be using a more suitable function to get the actual filename, such as PathFindFileName():
#include <windows.h>
#include <shlwapi.h>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
WCHAR szFilepath[MAX_PATH];
LPWSTR lpszFilename;
WCHAR szDestpath[MAX_PATH];
/* Get the current executable's full path */
GetModuleFileNameW(NULL, szFilepath, MAX_PATH);
std::wcout << L"filepath: " << szFilepath << std::endl;
/* Extract just the name */
lpszFilename = PathFindFileNameW(szFilepath);
std::wcout << L"filename: " << lpszFilename << std::endl;
/* Set the destination folder path and file name */
PathCombineW(szDestpath, L"D:\\", lpszFilename);
std::wcout << L"dest path: " << szDestpath << std::endl;
// copys the file of your '.exe'
if (!CopyFileW(szFilepath, szDestpath, FALSE)) {
std::wcout << L"couldnt copy the file";
}
else {
std::wcout << L"copied";
}
return 0;
}
Or, you could simply parse the filename yourself using normal C++ string operations, eg:
#include <windows.h>
#include <iostream>
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
WCHAR szFilepath[MAX_PATH];
std::wstring wFilepath;
std::wstring wFilename;
std::wstring wDestpath;
/* Get the current executable's full path */
wFilepath = std::wstring(szFilepath, GetModuleFileNameW(NULL, szFilepath, MAX_PATH));
std::wcout << L"filepath: " << wFilepath << std::endl;
/* Extract just the name */
wFilename = wFilepath.substr(wFilepath.find_last_of(L"\\/")+1);
std::wcout << L"filename: " << wFilename << std::endl;
/* Set the destination folder path and file name */
wDestpath = L"D:\\" + wFilename;
std::wcout << L"dest path: " << wDestpath << std::endl;
// copys the file of your '.exe'
if (!CopyFileW(wFilepath.c_str(), wDestpath.c_str(), FALSE)) {
std::wcout << L"couldnt copy the file";
}
else {
std::wcout << L"copied";
}
return 0;
}

Can std::wofstream << WCHAR and << CHAR at the same time?

How can we write WCHAR and CHAR to a wofstream at the same time?like this:
wofstream << L"汉字" << "汉字"
Here is something for testing. I can only "wofstream<<WCHAR" or only "wofstream<<CHAR", but can't "wofstream<<WCHAR<<CHAR" both at the same time.
#include <iostream>
#include <fstream>
#include <locale>
int main() {
std::wofstream wof2;
wof2.open(L"2.txt", std::wofstream::app);
wof2 << "\nCHAR 汉";
wof2.flush();
wof2 << L"\nWCHAR 汉";
wof2.close();
const char * tmp = setlocale(LC_ALL, "");
std::cout << tmp << std::endl;
std::locale::global(std::locale(""));
wof2.open(L"2.txt", std::wofstream::app);
wof2.imbue(std::locale());
wof2 << L"\nWCHAR after imbue";
wof2.flush();
wof2 << L"\nWCHAR 汉";
wof2.flush();
wof2 << "\nCHAR汉";
wof2.flush();
return 0;
}
We can see that at the console
Chinese (Simplified)_China.936
But In the 2.txt, I see that
CHAR 汉
WCHAR
WCHAR after imbue
WCHAR 汉
CHAR

Creating file with CreateFile function results in write protected file

I have been trying to write a file in path returned by SHGetFolderPath . But as File Is created, it has been given Write Protected. Following is my Code:
#include <Windows.h>
#include <Shlobj.h>
#include <iostream>
#include <Shlwapi.h>
int main()
{
HANDLE hfile;
TCHAR szPath[MAX_PATH];
char dataBuffer[] = "Some data to write here";
DWORD dwBytesWritten = 0;
if(SUCCEEDED(SHGetFolderPath(NULL,CSIDL_COMMON_APPDATA,NULL,0, szPath)))
{
std::cout << szPath << std::endl;
PathAppend(szPath,TEXT("lpa"));
std::cout << szPath << std::endl;
PathAppend(szPath,TEXT("config"));
std::cout << szPath << std::endl;
CreateDirectory(szPath, NULL);
PathAppend(szPath, TEXT("lpa.config"));
std::cout << szPath << std::endl;
hfile = CreateFile(szPath,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
/*WriteFile(hfile,dataBuffer,(DWORD)strlen(dataBuffer),&dwBytesWritten,NULL);
std::cout << szPath <<std::endl;*/
}
}
The code creates a file in C:\ProgramData\lpa\config\lpa.config but editing the file pops up dialog saying WriteProtection. What am I doing wrong here?
Yes, When you run VS instance with Admin privileges, special permissions are assigned to the created file. Either run as the program as Non-Admin or use SECURITY_DESCRIPTOR so that other user can access the file.
Creating a Security Descriptor for a New Object in C++
the example in the link shows how a registry key is created, you can put CreateFile() instead.
I solved the issue by using CSIDL_LOCAL_APPDATA instead of CSIDL_COMMON_APPDATA . I have the modified code given as below:
#include <Windows.h>
#include <Shlobj.h>
#include <iostream>
#include <Shlwapi.h>
int main()
{
HANDLE hfile;
TCHAR szPath[MAX_PATH];
char dataBuffer[] = "Some data to write here";
DWORD dwBytesWritten = 0;
if(SUCCEEDED(SHGetFolderPath(NULL,CSIDL_LOCAL_APPDATA,NULL,0, szPath)))
{
std::cout << szPath << std::endl;
PathAppend(szPath,TEXT("lpa"));
if(!CreateDirectory(szPath, NULL))
{
std::cout << "Create directory failed" <<std::endl;
}
std::cout << szPath << std::endl;
PathAppend(szPath,TEXT("config"));
if(!CreateDirectory(szPath, NULL))
{
std::cout << "Create directory failed" <<std::endl;
}
std::cout << szPath << std::endl;
PathAppend(szPath, TEXT("lpa.config"));
std::cout << szPath << std::endl;
hfile = CreateFile(szPath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
WriteFile(hfile,dataBuffer,(DWORD)strlen(dataBuffer),&dwBytesWritten,NULL);
std::cout << szPath <<std::endl;
}
}