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.
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;
  8. BOOL APIENTRY DllMain( HANDLE hModule, 
  9.                        DWORD  ul_reason_for_call, 
  10.                        LPVOID lpReserved
  11.                      )
  12. {
  13.     return TRUE;
  14. }
  16. _declspec(dllexport) int filereader(LPCSTR fpath)
  17. {   int val=0,i=0;
  18.     fstream f;
  19.     string w;
  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;
  34.     return val;
  35. }
LIBRARY filedll

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

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

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

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

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 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;
  7. _declspec(dllexport) int Add(int a,int b)
  8. {
  10.     return (a+b);
  11. }
LIBRARY deftry
Jun 5 '07 #9

Post your reply

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