Eugeny89
Eugeny89

Reputation: 3741

std::ofstream behaves differently in DLL

I'm developing a plugin. Take a look at the following code.

string request(char post_params[]) {
    CURL *curl;
    CURLcode res;
    std::string buffer; //here we'll write response

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_params);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) strlen(post_params));
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);

        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }

    return buffer;
}
....
bool perform(..) {
    std::ofstream file ("d:/t/t.txt");
    file << "opened";
    file.close();       
    string resp = request(....); 
    ...
}

If the code is launched inside an app, file d:/t/t.txt is created, but if the code is compiled to a DLL, and launched from an app running my plugin, the file is not created. But if I comment out line string resp = request(....); and what follows, the file will be created. Can somebody explain me what's up here?

Upvotes: 0

Views: 1332

Answers (2)

Grasshopper
Grasshopper

Reputation: 49

Make sure you add msvcprtd.lib ( Debug ) and msvcprt.lib ( Release ) to your dependencies if using Visual Studio.

Upvotes: 1

karlphillip
karlphillip

Reputation: 93468

std::ofstream file ("d:/t/t.txt");

// Make sure the file is opened before trying to write in it
if (!file.is_open())
{
   // print error message
}
else
{
   file << "opened";
   file.close();
}

Upvotes: 1

Related Questions