Menu

fstream dll in windows

eddy
2008-01-14
2012-09-26
  • eddy

    eddy - 2008-01-14

    Hello,

    I'm creating a simple dll to interface with excel (a function to convert units), I know the code works because I have changed the source so that it is an executable and it works fine
    When I try to use the below code in for the dll application for some reason the fstream (code line ifstream T(fpath.c_str(), ifstream::in);) does not work, the object is not created. I don't understand why the ifstream object can not be created, any help would be greatly appreciated.

    Thanks in advance,
    Ed

    Below is part of the source:

    include "dll.h"

    include <windows.h>

    include <iostream>

    include <fstream>

    include <sstream>

    include <string>

    include <vector>

    include <wtypes.h>

    include "units.h"

    static std::vector<Unit_item> dunits;
    int Load_flag=0;

    using namespace std;
    // Function to read file
    int file_read(string fpath)
    {

    ifstream T(fpath.c_str(), ifstream::in);
    Unit_item tempholder;
    string ch=&quot;&quot;;
    string version= &quot;&quot;;
    string header = &quot;&quot;;
    string d_temp = &quot;&quot;;
    int index = 1;
    
    if (!T)
    {
        //
        MessageBox (NULL,  &quot;Could not Open File.&quot; , &quot;Msgbox&quot;, 0);
        return 0;
    }
    int flen =0;
    getline(T,version);
    getline(T,header);
    while(!T.eof())
    {
        getline(T,ch);
        istringstream stream(ch);
        stream &gt;&gt; tempholder.units;
        stream &gt;&gt; tempholder.category;
        stream &gt;&gt; tempholder.factor;
        stream &gt;&gt; tempholder.offset;
        while (stream &gt;&gt; d_temp) {
            if (index &gt;= 2)
            {
                 tempholder.description += &quot; &quot; + d_temp;
            }
            else
            {
                 tempholder.description = d_temp;
            }
            index ++;
        }
        dunits.push_back(tempholder);
        index=1;
        flen ++;
    }
    T.close();
    T.clear();
    Load_flag=1;
    return 1;
    

    }

    DllClass::DllClass()
    {

    }

    DllClass::~DllClass ()
    {

    }

    BOOL APIENTRY DllMain (HINSTANCE hInst / Library instance handle. / ,
    DWORD reason / Reason this function is being called. / ,
    LPVOID reserved / Not used. / )
    {
    switch (reason)
    {
    case DLL_PROCESS_ATTACH:
    break;

      case DLL_PROCESS_DETACH:
        break;
    
      case DLL_THREAD_ATTACH:
        break;
    
      case DLL_THREAD_DETACH:
        break;
    }
    
    /* Returns TRUE on success, FALSE on failure */
    return TRUE;
    

    }

    //Function to Convert units
    declspec(dllexport) double stdcall Unit_Convert(double init_value, BSTR ptr_initval, BSTR ptr_finalval)
    {
    string intemp="";
    string finaltemp="";
    if(!ptr_initval || !ptr_finalval) //ShouldneverbeNULL,but...
    return 0; //ReturnVBFalse
    if(!ptr_initval || !ptr_finalval) //Isstringinitialised?
    return 0; //ReturnVBFalseifnot
    for(int i=0;;i++)
    {
    if(!((char
    )(ptr_initval))[i]){
    break;
    }
    intemp += ((char
    )(ptr_initval))[i];
    }
    for(int i=0;;i++)
    {
    if(!((char
    )(ptr_finalval))[i]){
    break;
    }
    finaltemp += ((char
    )(*ptr_finalval))[i];
    }
    if (Load_flag == 0){
    string fname = "t2.fac";
    int file_flag;
    file_flag = file_read(fname);
    }
    int j=unit_find(intemp);
    int z=unit_find(finaltemp);
    TCHAR numstr [ 32 ];
    sprintf(numstr,"%.4f",z);
    MessageBox (NULL, numstr ,"test" , 0);
    double new_factor= dunits[j].factor * dunits[z].factor;
    return new_factor;
    }

     
    • cpns

      cpns - 2008-01-14

      The usual reason for failure is simply that the file does not exist. Have you tried calling it with a fully specified path (such as "c:\mydatafolder\t2.fac" for example)? If that works (with the file in the appropriate folder), then it is a simple case of your current working folder not being the same as your data folder at the time the code is executed.

      Clifford

       
    • cpns

      cpns - 2008-01-14

      As an aside, note that DLL interfaces with C++ linkage are not portable amongst compilers, so other development tools may not be able to use your DLL.

       
    • eddy

      eddy - 2008-01-14

      Thank you Clifford,

      I had tried before with the full path location but didn't realize you needed to double the "\&quot; in the string. Adding the full path solved the problem.

      Thanks,
      Ed

       
      • cpns

        cpns - 2008-01-14

        '\' is teh escape character in C/C++ used for placing 'invisible' characters such as tab ('\t') and newline ('\n') into string and character constants. How a single '\' is interpreted depends on what follows it. My example without the '\' escape sequences:

        "c:\mydatafolder\t2.fac"

        would be interpreted as:

        "c:ydatafolder<TAB>2.fac"

        Most modern Windows compilers will accept Unix style forward-slash ('/') folder delimiters, which need not (and should not) be doubled up.

        Clifford

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.