By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,412 Members | 929 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,412 IT Pros & Developers. It's quick & easy.

dll to read a file and return the number of lines in the file.

P: 52
following is the code for dll to read a file and return the number of lines in the file.
filedll.cpp
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include<windows.h>
  4. #include<fstream>
  5. #include<string>
  6. using namespace std;
  7.  
  8. BOOL APIENTRY DllMain( HANDLE hModule, 
  9.                        DWORD  ul_reason_for_call, 
  10.                        LPVOID lpReserved
  11.                      )
  12. {
  13.     return TRUE;
  14. }
  15.  
  16. _declspec(dllexport) int filereader(LPCSTR fpath)
  17. {   int val=0,i=0;
  18.     fstream f;
  19.     string w;
  20.     f.open(fpath,ios::in);
  21.     if(f.is_open())
  22.     {
  23.        while(!f.eof())
  24.        {
  25.            getline(f,w);
  26.            i++;
  27.        }
  28.        val=i;
  29.        f.close();
  30.     }
  31.     else
  32.         val=-1;
  33.  
  34.     return val;
  35. }
  36.  
filedll.def
LIBRARY filedll
EXPORTS
filereader

BUT its not working..it just prints 1..even if the filename passed doesnot exist.
Jun 4 '07 #1
Share this Question
Share on Google+
8 Replies


100+
P: 208
So it looks like its getting into if(f.is_open()) whether the file is open or not. I don't know C++ but is it possible this is returning something other than 1?
Jun 4 '07 #2

P: 52
no its not like dat...
its giving 1 in all da cases..if the file exists and there are many lines in it..
Jun 4 '07 #3

P: 52
but the same code works fine if i run the function from simple program..instead of calling the dll
Jun 4 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
Have you tried to follow the path of execution using your debugger??

Other questions:
1) have you loaded the dll??
2) have you used GetProcAddress??
Jun 4 '07 #5

P: 52
yes i have done all that..
Jun 4 '07 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
yes i have done all that..
May I see what you have done?
Jun 4 '07 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
It just occurred to me:

You are aware that C++ mangles function names???

If you have .cpp file you will nedd to
a) use a DEF file to export the mangled name as a readable one
b) use extern "C" on your C++ function.

With a static lib, the mangled name is used throughout.

Or, you can forget all this and put your code in a .c file. Function names are not mangled in C and a DLL is an old C thingy.
Jun 4 '07 #8

P: 52
I am confused about this mangled name..to be put in def file..

If the following dll works fine without mangled names and extern "C" ..why cant others..
Expand|Select|Wrap|Line Numbers
  1. deftry.cpp
  2. #include<iostream>
  3. #include<string>
  4. #include<windows.h>
  5. using namespace std;
  6.  
  7. _declspec(dllexport) int Add(int a,int b)
  8. {
  9.  
  10.     return (a+b);
  11. }
  12.  
deftry.def
LIBRARY deftry
EXPORTS
Add
Jun 5 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.