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)
{
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;
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I had tried before with the full path location but didn't realize you needed to double the "\" in the string. Adding the full path solved the problem.
Thanks,
Ed
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
'\' 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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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)
{
}
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;
}
//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;
}
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
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.
Thank you Clifford,
I had tried before with the full path location but didn't realize you needed to double the "\" in the string. Adding the full path solved the problem.
Thanks,
Ed
'\' 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