473,388 Members | 1,298 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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

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
8 1593
Silent1Mezzo
208 100+
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
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
but the same code works fine if i run the function from simple program..instead of calling the dll
Jun 4 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
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
yes i have done all that..
Jun 4 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
yes i have done all that..
May I see what you have done?
Jun 4 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
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
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

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

Similar topics

2
by: Count Dracula | last post by:
Is there a way to write a c++ program that will print out the last few lines of a file without reading the whole file? The implementations of 'tail' I have seen all appear to be system dependent....
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
40
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
5
by: Martin Svensson | last post by:
Hello! I need some help/recommendations on how to do the following. I have a program that writes an IP address two control numbers and a date to file, on one line. It's a basic text file and it...
1
by: Jose Reckoner | last post by:
I'm running python 2.3 on Windows XP. Anyone have a quick small script to convert .DT1 and .DEM data to ASCII or some other format? I don't need a viewer. Thanks!
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
4
by: amatoo04 | last post by:
hi guys, i am trying to finish up a program i started this morning and basically i got the whole input and output down... i just need help with the function which will scan the file and return number...
5
by: shermeenz | last post by:
the file which i have to read is: From sms@localhost.localdomain Wed May 16 10:46:03 2007 Return-Path: <sms@localhost.localdomain> Received: from localhost.localdomain (localhost.localdomain ) by...
9
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying...
63
by: Bill Cunningham | last post by:
I don't think I can do this without some help or hints. Here is the code I have. #include <stdio.h> #include <stdlib.h> double input(double input) { int count=0,div=0; double...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.