B_old 07-29-2002, 08:41 AM Hello.
I'm absolutely new to C++ and having some problems to create even the most somple program.
As I still write a kindoff big project in VB and there is no way for me to do this in C++ I thought it would be cool indeed to have a .dll written in C++ and cal it in VB.
(By the way I use Visual C++ 6)
So what I actually ask for, is as detailed help as possible whith that.
I thought about to start with a .dll (just so I can concentrate on the basics) that gets a number from VB, adds, lets say, the number 2 to this number and returns the new number.
Also I would be happy I someone would show how to call that dll from VB.
I know that is pretty much demanded (in fact it must seem that I ask you to write my programs) but I having a hard time with C++, and the tutorials I read so far left me wondering where actually to type the code... (well, it's not that bad but you get the point)
Your help is very much appreciated.
Rezner 07-29-2002, 09:59 PM This is actually not that hard to do. There are many, many ways to acheive it though. Here (http://www.developerfusion.com/show/1973/)'s an example of one of the easiest.
B_old 07-30-2002, 04:01 AM Actually I found that tutorial too!
And it even works for me.
Just one problem. I think the code is dealing with to much stuff, I mean as I know nothing about C++ i found it hard to even edit the code. You know what i mean?
Anyway I wrote this in a *.cpp
#include "AddNumber.h"
long __stdcall Calculate (int &Number, int &Result)
{
Result = Number * Number;
return 0;
}
And I compiled it and there is no error.
When I call it whith VB though, the program just ends.
If I leave out the "Result = Number * Number;" part
there is no error.
Could you show what I have to do to actually return the result value to VB.
I think that I know how to give the number to the dll, at least i hope so.
(I ask for c++ code here)
Thank you!
Thinker 07-30-2002, 07:30 AM What is the VB declaration you used for this function, and the VB
code you used to call it?
B_old 07-30-2002, 08:18 AM Private Declare Function Calculate Lib "c:\temp\addnumber.dll" (ByVal PutV As Integer, ByVal GetV As Integer) As Long
Private Sub Form_Load()
Dim RetVal As Long
Dim Number As Integer
Dim Result As Integer
Number = 20
RetVal = Calculate(Number, Result)
MsgBox (Result)
End Sub
I think that this is not the problem. I suspect that the error is in the C++ code. As I said, if I do not calculate anything in the dll. everything works (accept that nothing happens and the mssgbox shows 0)...
Thanks
Thinker 07-30-2002, 08:49 AM The problem is the ByVal passing of parameters. There is no
way to pass the value back.
B_old 07-30-2002, 10:05 AM Oh, so the problem is the ByVal-thing. I understand.
...Or rather not.
Come on, I really appreciate a helping hand here.
Well...
Thinker 07-30-2002, 11:03 AM When a variable is passed byval, the value is passed. When the
normal byref (default), the address of the variable is passed. Your
C function parameters are int &Number, int &Result, which would
be references to the variables, so it seems like you should be
passing byref. I am not at all sure about this though, but I am
sure if you want to modify the passed parameters, they have to
be passed byref.
B_old 07-30-2002, 02:48 PM Ha!
I wrote Byref in the VB-code and changed a small detail in the C++-code and now it works. thanks.
Pookie 08-01-2002, 08:12 AM You also have to remember that an Integer in C++ is not the same as one in VB.
And Int in C++ is a Long in VB.
A Short in C++ is an Integer in VB.
So your also calling it with the wrong data types.
Why not just call it like this:
int _stdcall Calculate (int Number)
{
Return Number * Number;
}
This way, when you call Result= Calculate (Number) the result from the dll function will be returned in the variable Result.
B_old 08-01-2002, 09:25 AM #include "AddNumber.h"
long __stdcall Calculate (int &Number)
{
Number = Number * Number;
return 0;
}
My code is like this, and it works.
I don't know what the "&" in front of Number is good for though.
Also I don't know what return means...
I thought return 0 would just close the programm or so.
.... anyway. thanks
Thinker 08-01-2002, 09:31 AM Pookie is correct, your VB declaration needs to be As Long.
Pookie 08-01-2002, 07:44 PM Originally posted by B_old
I don't know what the "&" in front of Number is good for though.
Also I don't know what return means...
I thought return 0 would just close the programm or so.
.... anyway. thanks
The & in front of a Variable means the memory address of the variable which is being referenced to it. Which basically means the same as byref in VB, so whatever number the &number becomes, it will change the original VB variable which it was referenced with too as well.
The return command is a way to exit from the function, and putting a value allows you to return some data from the function.
So:
int _stdcall (int Number) {
if ( Number < 0) {
Number = -Number;
}
Return Number; // Absolute value of number
} // int means the variable type being returned from the function
If you don't wish to return anything from a function
void _stdcall (int Number) {
Return 0;
} // function returns nothing, or void.
btw, A faster method of making a negative int to positive is:
Number=( Number and 0x7FFFFFFF ); :)
Doing a search on the internet, I found this site for you which has some tutorials and articles aimed at beginners.
http://www.codeproject.com/cpp/#Beginners
B_old 08-02-2002, 10:46 AM Well, OK.
Thanks for the link to the beginners tutorial!.
Can you also return more then one value from a funtion?
Thanks for the help!
OnErr0r 08-02-2002, 10:50 AM If you pass params ByRef, sure.
|