There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
.dbx file audio automatic updates avg avg 8 blue screen brand new codec control panel conversion crash dbx file desktop display dos driver duplicate dvd error error message excel explorer external file game hardware hijackthis log install installation internet itunes javascript laptop macro malware msn music network outlook outlook 2003 outlook express php problem random rundll32 security seo sound sp3 spyware switch tag cloud trojan usb video virtumonde virus vista visual basic vundo wallpaper windows windows vista windows xp winxp wireless word xp service pack xp sp3 youtube
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Solved: C++ help needed


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
fabian.rap.more's Avatar
Computer Specs
Junior Member with 20 posts.
 
Join Date: Jan 2008
Experience: Beginner
04-May-2008, 03:20 PM #1
Solved: C++ help needed
I am using this IDE to write some c++ programs: http://www.bloodshed.net/devcpp.html

I have a program that I need to put into a dll file under a function so that I can call it from any application. How do I do this? Is there any tutorial for this? I would also like to be able to add more functions to this dll file.

Thanks in advance.
Shadow2531's Avatar
Distinguished Member with 2,621 posts.
 
Join Date: Apr 2001
04-May-2008, 09:26 PM #2
Here's a c++ example of how to create a dll with Mingw and use it. (Bloodshed uses Mingw)

mytestlibrary.hh:
Code:
#ifndef MYTESTLIBRARY_HH
#define MYTESTLIBRARY_HH

#include <iostream>

void test();

#endif
mytestlibrary.cc:
Code:
#include "mytestlibrary.hh"

void test() {
    std::cout << "My test library!\n";
}
Code:
g++ -Wall -Wextra -shared mytestlibrary.cc -o mytestlibrary.dll -O3 -s -Wl,--out-implib,libmytestlibrary.a,--output-def,mytestlibrary.def
Now that you've built the library, install it.

Put mytestlibrary.hh in a mytestlibrary folder in the compiler's include folder.

Put libmytestlibrary.a and mytestlibrary.def in the compiler's lib folder. (.def file is useful for generating a .lib file that MSVC can link to. Not needed for Mingw.)

Now, create a program that uses the library.

mytestprogram.cc:
Code:
#include <mytestlibrary/mytestlibrary.hh>

int main() {
    test();
}
Code:
g++ -Wall -Wextra mytestprogram.cc -o mytestprogram -O3 -s -lmytestlibrary
Make sure mytestlibrary.dll is in the same directory as mytestprogram.exe and run mytestprogram.exe

To add more functions to the dll, just declare more in the hh file, implement them in the cc file, rebuild the dll, install it and rebuild the test program.

If you want to add a function to a .dll that you don't have the source to and can't build, I'm not sure.
__________________
10 ? "a line as the unending horizon"
20 ? "a curve as the rolling hillside"
30 ? "a point as a distant bird"
40 ? "a ray as the rising sun"
run

Last edited by Shadow2531 : 04-May-2008 09:33 PM.
fabian.rap.more's Avatar
Computer Specs
Junior Member with 20 posts.
 
Join Date: Jan 2008
Experience: Beginner
05-May-2008, 07:06 AM #3
Ok. Thank you very much. It worked.
fabian.rap.more's Avatar
Computer Specs
Junior Member with 20 posts.
 
Join Date: Jan 2008
Experience: Beginner
05-May-2008, 07:41 AM #4
One more thing. I need to pass a few parameters. I tried putting test(int no); but no luck.

And from some applications it appears like this: Mylibrary::_Z6testv

How do I fix this?
Shadow2531's Avatar
Distinguished Member with 2,621 posts.
 
Join Date: Apr 2001
05-May-2008, 04:27 PM #5
Quote:
One more thing. I need to pass a few parameters. I tried putting test(int no); but no luck
Did you make the change in *both* the hh and cc files?

And from some applications it appears like this: Mylibrary::_Z6testv

I think you have to use extern C if you want the names to come out unmangled. You might have to do some other things depending on what compiler built the program that's loading your mingw dll.

This will explain:
http://en.wikipedia.org/wiki/Name_ma...ing_in_C.2B.2B
__________________
10 ? "a line as the unending horizon"
20 ? "a curve as the rolling hillside"
30 ? "a point as a distant bird"
40 ? "a ray as the rising sun"
run
fabian.rap.more's Avatar
Computer Specs
Junior Member with 20 posts.
 
Join Date: Jan 2008
Experience: Beginner
06-May-2008, 11:25 AM #6
I get the following errors:

Quote:
D:\Mydlls\/mytestlibrary.hh:6: error: previous declaration of `void* Create()' with C++ linkage
D:\Mydlls\mytestlibrary.cc:9: error: conflicts with new declaration with C linkage
fabian.rap.more's Avatar
Computer Specs
Junior Member with 20 posts.
 
Join Date: Jan 2008
Experience: Beginner
06-May-2008, 01:59 PM #7
And I tried adding each parameter to both the files but it gives me ALOT of errors.
Shadow2531's Avatar
Distinguished Member with 2,621 posts.
 
Join Date: Apr 2001
06-May-2008, 11:59 PM #8
Try the attached.
Attached Files
File Type: zip test.zip (1.2 KB, 1 views)
fabian.rap.more's Avatar
Computer Specs
Junior Member with 20 posts.
 
Join Date: Jan 2008
Experience: Beginner
07-May-2008, 05:34 AM #9
OK. This is my last requirment:

I need to pass a string name to the dll and this string will be a file name but I can't pass a string though I tried const string file and const char file

Also I can't open a file whose name is stored in a variable. I open files using: std:fstream myf ("name.txt");
Shadow2531's Avatar
Distinguished Member with 2,621 posts.
 
Join Date: Apr 2001
07-May-2008, 06:18 AM #10
Try the attached to see if that gives you an idea.

Also, note that in testloadlibrary.cc,

Code:
MYFUNC writeFile = (MYFUNC)GetProcAddress(mydll, "writeFile");
should probably be:

Code:
MYFUNC writeFile = reinterpret_cast<MYFUNC>(GetProcAddress(mydll, "writeFile"));
You can catch that by building with -Wold-style-cast
Attached Files
File Type: zip test2.zip (1.4 KB, 1 views)

Last edited by Shadow2531 : 07-May-2008 06:29 AM.
fabian.rap.more's Avatar
Computer Specs
Junior Member with 20 posts.
 
Join Date: Jan 2008
Experience: Beginner
07-May-2008, 07:37 AM #11
Thanks. It works now
fabian.rap.more's Avatar
Computer Specs
Junior Member with 20 posts.
 
Join Date: Jan 2008
Experience: Beginner
07-May-2008, 07:39 AM #12
And also thank you so much for helping me
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:32 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.