Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DLL Talk / Dll Creation

Author
Message
Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 30th Dec 2008 02:36 Edited at: 30th Dec 2008 05:04
Hi all,
i'm making my first dll in c++ (Dev C++) for DBPRO, and i have a problem and i want to ask the community for help since i'm a noob in c++ and OOP

I created a class in C++, and in a function i created a object of that class.

1) How do i return that object to DarkBasic and then to another dll function in another call.

For example (In Darkbasic):

bigHouse = createHouse()
setHouseHeight(bigHouse ,2)

2) Is there a way to know the object and call its methods in c++, if i pass the adress from DBPRO? (Or there is a way to do it)(IS the same question as 1) )?

If anyone can make an small example please do it since im new in dll making.

thanks
IanM
Retired Moderator
17
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 30th Dec 2008 12:17
Quote: "(Dev C++)"

Dump it.
If you have a problem with using the MS C++ express compiler and want to stick with G++ then at least go with the CodeBlocks or Eclipse IDE's - Dev-C++ hasn't been updated for many years.

1. You can't.

What you can do though is cast a pointer to your class to a DWORD and pass that back instead. Of course, you'll need to cast it back to a pointer to your class when it's used as an argument.

2. There isn't.

Basically, you need to write standard function that accepts an argument to your DWORD/object pointer and convert that into a member function call.



BTW, if you go this route, make sure that you validate all the pointers being passed back to you before you use them.

Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 30th Dec 2008 13:50 Edited at: 30th Dec 2008 13:51
Hi, thanks for the help.
I couldn't put it to work,i'm making a mistake but i'm not able to find it. Can you help me?. I'm returning some kind of address and the second print in DBRPO of variable "valor" should be 4.

my .cpp



my .h



my .rc



my dba



Quote: "Dump it.
If you have a problem with using the MS C++ express compiler and want to stick with G++ then at least go with the CodeBlocks or Eclipse IDE's - Dev-C++ hasn't been updated for many years."


Didn't know. Thanks for the info, i will try eclipse since everybody in Colombia talks about it.

I don't have a problem with MS C++, but we need the Dll (Demon 000 and I) to use in a commercial aplication, so that's the reason (am I wrong?).


Quote: "BTW, if you go this route, make sure that you validate all the pointers being passed back to you before you use them."



I have no idea how to validate a pointer of an object. For my use is safe since i know which parameters i'm using, but I'm thinking to release it to the community and in the end it will be better. I know im becoming a bit burdensome, but, can you explain me please how to do such validations?

Attachments

Login to view attachments
IanM
Retired Moderator
17
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 30th Dec 2008 14:10 Edited at: 30th Dec 2008 14:13
This is an absolute no-no, whether you are interfacing to DBPro or not:


You must not return pointers to stack objects - they are destroyed when the function ends, and using them could cause the objects that replace them later to be corrupted.

Only ever pass pointers back from heap-based objects (ie, those allocated by the 'new' operator).


I also suggest that when you do create new objects on the heap, that you store the addresses in a std::set or some similar structure, and delete from that when the object is deleted. This is so that you can validate incoming addresses against it. If it's not in the set, then it's not a valid object and you should trigger an error.

[EDIT]
I'll put an example together for you later today.

Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 3rd Jan 2009 15:57
Quote: "[EDIT]
I'll put an example together for you later today."


ho well...
we are int the first days of the year, i guess i have to be patient.

PS: I'm sometimes a bit anxious for knowledge.
IanM
Retired Moderator
17
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Jan 2009 17:01
Um ... would you accept a 'whoops' and an apology? I forgot all about this.



Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 3rd Jan 2009 19:42 Edited at: 3rd Jan 2009 19:44
Hi,
talking about speed!

don't worry about it. It happens to me very often

I have read your code but there are some lines that i don't understand



Since i'm a NOOB in C++, I need to study those concepts before. So i will say to you later if they have come to use to me (But, for the looks i should say they will).

Do these operations, declarations and functions have a library name, so I can study straight to the point?

Thanks
IanM
Retired Moderator
17
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Jan 2009 20:44
A C++ set is comparable to the sets that you learned in your maths lessons - a unique list of items with a certain property. It's all that stuff to do with intersections, unions, complements, difference etc.

In this code, I have created a set containing pointers of all objects that currently exist, and named it AllObjects:
std::set<Object*> AllObjects;

The .insert function does exactly what it says - inserts a pointer into the set.
AllObjects.insert(Obj);

The .erase function also does exactly what it says - erases a pointer from the set. As a side effect it also returns the number of items (0 or 1) that it managed to locate and remove.
AllObjects.erase( Obj ) > 0

The .find function will search for the pointer you provide, and if it doesn't find it, will return an item (.end()) that signals that (not precisely true, but a reasonable simple description of what is happening here).
AllObjects.find(Obj) != AllObjects.end()

Take a look at the following URL for a full description of the C++ set class: http://www.cplusplus.com/reference/stl/set

Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 3rd Jan 2009 22:20
Thanks a lot.

I'm going to continue right now with the Dll and tell you the results.
Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 5th Jan 2009 00:37 Edited at: 5th Jan 2009 00:44
Big thanks IanM, I have managed to implement the first version of the Aggregated Arrays functions.

There are a lot of things to be done like error handling (), but for now Aggregated Arrays work for integers! . (floats and strings to be implemented)

I have uploaded the dll as an attachment.

[EDIT] I had problems with the returning for void returning functions since the compiler didn't recognize them. I have put a double xx for the variable name, for functions that shouldn't return a value [/ EDIT]

A code example for AA functions:

Attachments

Login to view attachments
IanM
Retired Moderator
17
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 5th Jan 2009 13:59
Quote: "I had problems with the returning for void returning functions since the compiler didn't recognize them"

Assuming you are talking about the resource strings, you should put a 0 in the string:


Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 5th Jan 2009 15:06
Strange enough,
im doing something like this, without a valid result:



When I call the function in Darkbasic it doesn't recognize it. If I put to return something then it works. Maybe it's DevC++, since i have not started to use Eclipse (Or maybe im doing something terribly wrong).

IanM
Retired Moderator
17
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 5th Jan 2009 18:13
That's where you are going wrong.

You only include a return type for functions, those 'commands' with the opening square bracket at the end of the name.
Then you fill the rest with your arguments in order.

Only if you have no return type and no arguments do you put the '0' in.

For example...

function returning an integer:
MYFUNC[%L%FunctionName%

function returning an integer and accepting a string:
MYFUNC[%LS%FunctionName%short description of string parameter

command accepting an integer:
MYCMD%L%FunctionName%short description of integer parameter

command with no parameters:
MYCMD%0%FunctionName%

When using them in DBPro, commands do not need brackets. Functions do, and they always require you to use the result (as a parameter to another command/function or storing in a variable).

Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 6th Jan 2009 01:18 Edited at: 6th Jan 2009 01:18
Hi,
excuse me for confusing you.

the "AA_Return" above is suposed to be a "AA_SetValue", so, it's not returning a value of any kind.

although i didn't kknow this:

Quote: "command with no parameters:
MYCMD%0%FunctionName%"


I didn't know also the difference between commands and functions.

---------------------------------

My problem was calling the command in Darkbasic like you said, because I didn't need the brackets, so, when i use:



and,



It works fine in Darkbasic. Thank you so much.
Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 8th Jan 2009 00:26 Edited at: 8th Jan 2009 00:46
hi,
I'm advancing more on this Dll and i have a couple of questions more about them (thanks for all your help).

1) I'm doing message boxes for error handling, but i need to "end" the DarkBasic Application when errors are triggered. Do you know a way to do it?

2) I'm trying to pass floats and integers through the same function (to set the value of aggregated arrays), but the floats get rounded when i pass them. Is there a way to do it?, or i have to do a function to receive floats and a function to receive integers independently?.

I'm doing it in this in this way:

DBPRO



in C++

Attachments

Login to view attachments
Master Xilo
13
Years of Service
User Offline
Joined: 8th Sep 2006
Location: Bern, Switzerland
Posted: 11th Jan 2009 19:01
Quote: "I don't have a problem with MS C++, but we need the Dll (Demon 000 and I) to use in a commercial aplication, so that's the reason (am I wrong?)."


You can use everything created with msvc (express) comercially...

IanM
Retired Moderator
17
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th Jan 2009 19:20
Quote: "but the floats get rounded when i pass them"

You have a single function for setting that accepts an integer value - DBPro has no choice but to convert your float to an integer when you call that function.

You will need 1 function for each type you want to store, and 1 function for each type you want to retrieve (take a look at my memory bank or raw memory plug-ins for example command sets). You'll most probably also need some method for recording the original type when you store it so that you can detect errors (store as a string but fetch as an integer) or automatically convert.

I suggest that you write an interface base class for general fetch/store value and derive specific types from that for at least integer, floats and strings.
Either that, or use the boost 'any' class, which does the same thing in a more generic way.

If you don't know how that's done, then I'd study up a little more on C++ if I were you.

As for errors, here's a piece of code I used to use:


The two 'DBPro' functions are available within the DBProCoreDebug.DLL, so you'll need to grab them using LoadLibrary/GetProcAddress

Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 12th Jan 2009 07:03 Edited at: 12th Jan 2009 07:04
Quote: "You can use everything created with msvc (express) comercially..."


I didn't know, thanks .


Quote: "Quote: "but the floats get rounded when i pass them"
You have a single function for setting that accepts an integer value - DBPro has no choice but to convert your float to an integer when you call that function.

You will need 1 function for each type you want to store, and 1 function for each type you want to retrieve (take a look at my memory bank or raw memory plug-ins for example command sets). You'll most probably also need some method for recording the original type when you store it so that you can detect errors (store as a string but fetch as an integer) or automatically convert.

I suggest that you write an interface base class for general fetch/store value and derive specific types from that for at least integer, floats and strings.
Either that, or use the boost 'any' class, which does the same thing in a more generic way.

If you don't know how that's done, then I'd study up a little more on C++ if I were you.

As for errors, here's a piece of code I used to use:
+ Code Snippet

DBPro::CloseDisplay();
DBPro::Quit();
MessageBox( NULL, "messagebox error", "messagebox title", MB_OK );
ExitProcess(0);



The two 'DBPro' functions are available within the DBProCoreDebug.DLL, so you'll need to grab them using LoadLibrary/GetProcAddress"


- I will directly compare the values. I have a type variable in the paren Array class, so when the parameter is sended it could compare if the type is the same, if not then trigger an error. This will make it more slow, if it refeers to thousands of operations (what it's intended for), and i don't know to include in the final version. From your knowledege do you thing that this is slow is much if a I make many operations?, or it just doesn't matter? (it's an if for each value filling)

- I think you are reffering that i use polymorphism for the fetch and store value for the interface base class. I'm just learning that and I'm kinda noob making classes, altough i will do what you say and study a little bit more to do it that way.

For error triggers and exit, i have now tree possibilities (two from you), I have to study more to to have this done and choose one.

I will see if I can have this Dll complete, for tomorrow.

Thank you so much.
IanM
Retired Moderator
17
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 12th Jan 2009 14:03
Quote: "From your knowledege do you thing that this is slow is much if a I make many operations?"

Better slow+correct than fast+wrong. Write it correctly first.

If you find it's too slow, then compile a second version with the checking taken out - use the first version while developing in DBPro, use the second for speed checks and your final release.

In my experience though, adding a single check to a function call adds so little overhead that I don't worry too much about it.

Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 19th Jan 2009 12:50 Edited at: 20th Jan 2009 16:12
Hi!!,
i think I have finished the dll (with floats and int's, I should extend it futher to strings, but i'm seeing myself short of time right now), i know i said an ending date for last week, but I had to study veeerryyy much to advance in the dll.

- i have not implemented polymorphism, because i have a generic function
with void pointers (the one from the post above), that can receive anything if I cast properly.

- I'm using "PostQuitMessage(0);" to end the DBPRO application when an error is triggered.

- I have implemented error handling for out of bounds and set existance. I have not implemented for correct sending of the type from Darbasic, since Darkbasic take care of all the details.

- I have to implement the ini and web help files, and test a bit more the error handling mechanism.

- I have a quiestion.

* IF an error is triggered i delete all the objects in the set with an iterator. How i do to remove this objects from the set when the applications of DBPRO is finished?,

* should i create and object when that destroys erases the objects in the set?,

*if i don't do this a memory leak is created because the objects in the set are never erased when the DBPRO application ends?


- The functions in this dll so far (GetValue and SetValue are dummy functions):



[edit] I have been testing the Dll and is almost ready for the community. I have finished the .ini file and the error handling for all type of errors, although, I don't know how to get the line where the error was triggered, do you know? (again thanks for your help and patience) [edit]
Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 1st Apr 2009 19:25
Hi,
progress in the Dll has been suspended because I was preparing my marriage. .

I'm Married now, but I'm a little busy ATM, I expect to reopen development next week.

Cheers.

AlfaX
skully
13
Years of Service
User Offline
Joined: 21st Jul 2006
Location: y+width*x
Posted: 2nd Apr 2009 08:33
congratulations!!!... or rather I should say ... welcome to the hell, hahaha, no no is joke


Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 15th May 2009 05:20
Wait for me
Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 18th Jun 2009 14:48
hmm

Login to post a reply

Server time is: 2019-10-19 08:14:49
Your offset time is: 2019-10-19 08:14:49