creating dll

okay, I need a bit of help creating a dll. I need to know where to place my code, because the created files look a little weird.

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "main.h"

// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
    MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            // attach to process
            // return FALSE to fail DLL load
            break;

        case DLL_PROCESS_DETACH:
            // detach from process
            break;

        case DLL_THREAD_ATTACH:
            // attach to thread
            break;

        case DLL_THREAD_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
}


main.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

/*  To use this exported function of dll, include this header
 *  in your project.
 */

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif


#ifdef __cplusplus
extern "C"
{
#endif

void DLL_EXPORT SomeFunction(const LPCSTR sometext);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__ 


I know one file is for the function names, and one is for the classes, but which is which, and where does the code go that I write? lol
Sounds to me that you should spend some time on the basics of C or C++ (the latter would be the best). Try out this site's tutorial under Documentation.

The quick answer to your question is: The implementation file (.cpp file).
okay, scratch the stuff above. I started a new project in visual studio for making the dll. (I learn this way, creating, experimenting, and playing)

cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// OgreTitaniumDll.cpp
// compile with: /EHsc /LD
#include <string>
#include <stdexcept>
#include "OgreTitanium.h"

using namespace std;

namespace OgreTexts 
{

	string OgreTexts::SwordBlows()
	{

	int AttackID;
		string AttackDisplay;
	
	}

}


header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// this is the Ogre namspace for texts that can be displayed from class within the the files.
namespace OgreTextDisplay
{

	class OgreTexts
	{

	public:
		// returns random sword strikes
		static __declspec(dllexport) string RandomSwordStikes();
			
	};
	
}


I'm already getting errors. I'm just wondering if you can see any right off, and help me sort them out, thanks.
To be honest, I don't think you know what half the words in that code mean. You should really get a better grip of basic C++ first.
Agreed with Gaminic. You seem to be way too green in this. For example, you use one namespace in the header file, and a different one in the implementation file. You also declare a method in the header file that cannot be found in the implementation file, while in the implementation file you implement a method that isn't declared in the header file.

Seriously, read one or two basic C++ tutorials first. Otherwise you'll just run around chasing your own tail.

And for future reference, always include the verbatim or the error messages you get so you can receive speedier and more accurate assistance.
To be honest, I don't think you know what half the words in that code mean. To be honest I don't, but I have an idea. I'm not out to memorize words, and meanings, I'm trying to accomplish something, and learn as I go. That's the best way to put it. Simply I'm trying to accomplish something...and learn as I go...simple as that. Either help, or stay off the topic. I'm asking for help, and asking questions, please answer them.

for farther information I'm following this

http://en.wikipedia.org/wiki/Namespace

I thought I had everything the way they were doing it...

some errors I'm getting are.

Error 6 error C2039: 'SwordBlows' : is not a member of 'OgreTexts' c:\users\jeremy\documents\visual studio 2010\projects\ogre titanium\ogre titanium\ogre titanium\ogretitanium.cpp 12 1 Ogre Titanium
Error 1 error C2146: syntax error : missing ';' before identifier 'RandomSwordStikes' c:\users\jeremy\documents\visual studio 2010\projects\ogre titanium\ogre titanium\ogre titanium\ogretitanium.h 10 1 Ogre Titanium
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\jeremy\documents\visual studio 2010\projects\ogre titanium\ogre titanium\ogre titanium\ogretitanium.h 10 1 Ogre Titanium
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\jeremy\documents\visual studio 2010\projects\ogre titanium\ogre titanium\ogre titanium\ogretitanium.h 10 1 Ogre Titanium
Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\jeremy\documents\visual studio 2010\projects\ogre titanium\ogre titanium\ogre titanium\ogretitanium.h 10 1 Ogre Titanium

warning

Warning 5 warning C4183: 'RandomSwordStikes': missing return type; assumed to be a member function returning 'int' c:\users\jeremy\documents\visual studio 2010\projects\ogre titanium\ogre titanium\ogre titanium\ogretitanium.h 10 1 Ogre Titanium

sorry for the way the above sounded, but I'm a hobbyist, and I'm simply playing around an learning. I am learning the basics, as well as playing with advanced, and intermediate things at the same time... I could sit around an puddle with the basics, but that's simply not what I want to do.

maybe you could point out errors, tell me why they're caused, and contribute to my learning.

thanks :)
Last edited on
All I really need is help getting this one thing up, and I'll follow suit with the rest. Once I run into another problem, I'll ask for help. That's the way I've been learning here. Try something, try to fix it, if I can't post for help.

Besides I know a lot of the simple things...functions, passing variables into the parameters, returning values. I'm getting sick of writing if statements, and what not. I'm off to something bigger. I just need some help learning it is all.
Last edited on
cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// OgreTitaniumDll.cpp
// compile with: /EHsc /LD
#include <string>
#include <stdexcept>
#include "OgreTitanium.h"

using namespace std;

namespace OgreTextDisplay 
{

	string OgreTextDisplay::LightSwordBlows()
	{

	int AttackID;
		string AttackDisplay;

	}

}


Header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// this is the Ogre namspace for texts that can be displayed from class within the the files.
#include <string>

namespace OgreTextDisplay
{

	class LightSwordBlowsTexts
	{

	public:
		// returns random sword strikes
		static __declspec(dllexport) string LightSwordBlows();
			
	};
	


}


new errors with this code.

Error 1 error C2146: syntax error : missing ';' before identifier 'SwordBlows' c:\users\jeremy\documents\visual studio 2010\projects\ogre titanium\ogre titanium\ogre titanium\ogretitanium.h 10 1 Ogre Titanium
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\jeremy\documents\visual studio 2010\projects\ogre titanium\ogre titanium\ogre titanium\ogretitanium.h 10 1 Ogre Titanium
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\jeremy\documents\visual studio 2010\projects\ogre titanium\ogre titanium\ogre titanium\ogretitanium.h 10 1 Ogre Titanium
Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\jeremy\documents\visual studio 2010\projects\ogre titanium\ogre titanium\ogre titanium\ogretitanium.h 10 1 Ogre Titanium

warning

Warning 5 warning C4183: 'SwordBlows': missing return type; assumed to be a member function returning 'int' c:\users\jeremy\documents\visual studio 2010\projects\ogre titanium\ogre titanium\ogre titanium\ogretitanium.h 10 1 Ogre Titanium


Missing return type makes me think that it's not accepting string as return type, but I could be wrong, lol.
Last edited on
I understand the whole "learn by doing" thing and even the "I do this as a hobbie" thing. But truth be told, C++ is NOT a simple language by any standard and it is definitely not for the hobbyist programmer. Want to be a hobbyist programmer? Learn C# (or .Net in general) or Java. Those languages shield the programmer from the headaches of memory and resource management, and have simpler syntax. C++ is just not a good choice for you.

Your latest piece of code is just a tad bit better, but still has most issues, plus a new one. Trying to explain the why's of things here would be just too long for a simple forum post reply, so I say this with my hand on my heart: Read a C++ tutorial (or 2) or back off from C++ and switch to an easier language. Really. A tutorial will explain far better than I probably can in a forum reply.

I assure you I'm not trying to be mean or anything. I am just being realistic. C++ as a hobby is a bad idea.
"C++ as a hobby is a bad idea. " That's your opinion. I'm having fun doing it. Having fun researching, and doing the tutorials. Who are to tell me what hobbies to take up, and which are bad ideas...how about you list yours, and I'll start picking them apart for you. I'm asking for specific help, and your bringing this into my topic. I don't need someone in here telling me I shouldn't have the hobbies I do, or what I'll enjoy doing today, or tomorrow.

If I decide tomorrow that I want to study rocket science, and take it up so be it...If I do, and join a site forum for 'beginners' and start asking questions as a beginner, I'm not confronted by people like you to tell that my hobbies are a bad choice. Hopefully someone will actually come along, and start teaching me, so that I can have the fun that I intended.
LOL. Ok. I'll try my best not to answer any of your questions from now on, as I don't have the patience or time required to answer them in an appropriate fashion.

Please accept my most sincere apologies for wasting your time, and good luck finding someone with enough patience to explain your problems down to the missing semi-colon.
I assure you I'm not trying to be mean or anything. I am just being realistic. C++ as a hobby is a bad idea.


*areYouKiddingMe.jpg

LOL. Ok. I'll try my best not to answer any of your questions from now on, as I don't have the patience or time required to answer them in an appropriate fashion.


sad day.

Who are to tell me what hobbies to take up, and which are bad ideas...how about you list yours, and I'll start picking them apart for you. I'm asking for specific help, and your bringing this into my topic. I don't need someone in here telling me I shouldn't have the hobbies I do, or what I'll enjoy doing today, or tomorrow.


Stop that. Arguing won't get your question answered.

Read a C++ tutorial (or 2) or back off from C++ and switch to an easier language. Really. A tutorial will explain far better than I probably can in a forum reply.


this. I agree with the veritable webJose that it would be a good idea to read up on the basics. It would serve you very well indeed.


Last edited on
"LOL. Ok. I'll try my best not to answer any of your questions from now on, as I don't have the patience or time required to answer them in an appropriate fashion."


You didn't answer a question.


Please accept my most sincere apologies for wasting your time, and good luck finding someone with enough patience to explain your problems down to the missing semi-colon.


This is a forum for beginners to learn, if someones not going to teach them than how are they? If you're not interested in helping someone learn the advancing topics, and teach them, and guide them go find someone wondering how to declare a variable as that may suite your 'time and patience'


Stop that. Arguing won't get your question answered.


I was only defending myself. I get kinda defensive I guess when someones belittling my 'choice of hobby'...lol. I've kinda given up on this topic for a moment, until someone IS willing to come along, and point out some errors, why they're there, and how to fix them..what to do, what not to do...after all this is a place of learning, being taught, and getting help isn't it?

it would be a good idea to read up on the basics. It would serve you very well indeed.


I agree with this too though. I've been reading tuts, and watching videos. I just wanted to take on a bigger project, and get some help doing it, while I'm learning the basics.
after all this is a place of learning, being taught, and getting help isn't it?


This is a community mate, it's made by good feelings and working together. Keep it cool and life will be good. I'll take the time to answer your question when I get a minute, unfortunately I don't have more than 2 minutes to spare.

webJose is quite nice, try to treat him well ;)
Anger is the path to the dark side. Confusion manifest frustration which in turn creates angry words. A solution will always come to the one who is patient and doesn't five in to such frustration.

DLLs were a hassle for me to learn when I first got into it. There are different types you need to be aware of. Right now the DLL I am working on is resource DLLs which have no real code, just holds bitmaps for the main program. I wanted a card program with interchangeable bitmap images for the cards. This is different from what you are working with because you want code that runs based on using the DLL, in fact you seem to want a class inside the DLL.

Are you doing this so that you can access the DLL from other program languages or is this code for your own use just for C++? These are the important questions that must be asked. Libraries are best used for reusable code that is only for C++ and isn't needing updated later. I ask because you are putting a class into a DLL and it is the first logical assumption.

Libraries are great because you can use pragma comment to add the library file in the header file so that you don't need to worry about including libraries later. This means to include something to your new programs, just include the header and everything will be apart of it. Also I suggest compiling a debugger version (Add a D to the file for debugging) and a release version so that when you create a release version, you don't need to go through and recompile each part each time.

1
2
3
4
5
#ifdef DEBUG
#pragma comment(llib, "SomelibraryD.lib")
#else
#pragma comment(lib, "SomeLibrary.lib")
#endif 


This way you don't need to change code, it is already set up for both release and debugging. You can change things later for upgrading without too much problems. Just in properties make sure you save the file to a folder with the header and add that same folder to your directoryof includes.

As for DLLs, it means you only need the DLL and can be used with other programming languages, but requires a lot of effort and knowledge in using DLLs. That would require more information then I can sit here and type.

Placing a namespace inside a DLL seems pointless since you are adding the code specifically to use with your programs. You have to access the memory location of the function specific to the DLL so you don't need to worry about about much in regards to other functions with the same name so you can get rid of that. In fact that may be a problem for your DLL since you can not directly access the function from outside the DLL.

Hope my info helps and if you want more specific information, please feel free to ask. Every one gets frustrated with code, I have been programming for over 10 years and haven't found a problem I couldn't solve, and even I get frustrated with code at times. So no matter how good you become, you will always get frustrated, much like a brilliant novelist like say Stephen King getting writer's block. The secret is to not let it get you down and be as patient as possible. I sometimes walk away for a little while so I can look at it with new eyes, a new perspective. I may go play a video game for an hour or watch TV for a little while so I can relax and then when I go back to see what is wrong with my code, I tend to see it better since I didn't have my head stuck too far in the code. Sometimes when you get too involved in something you miss simple things that normally you be able to see.

Hope my information help.
Topic archived. No new replies allowed.