Search code examples
c++visual-c++ifstream

Visual C++ DLL that checks if file exists


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);
    };
}

Solution

  • 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!