-
Question about pointers to DOUBLEs (VC++6.0)
Hi.
I'm having a problem with parameter passing when it comes to DOUBLE or double and a pointer to this parameter. I have a double variable holding a value and I make a pointer to this value. The problem is: when I pass this pointer to the function, it won't recognize it as a valid double value and crash the extension at this point.
If I call this function this way, it'll work:
Code:
myFunction(1, "Hello!", 20.95);
But this will NOT:
Code:
void* pointer = NULL;
double argument = 20.95;
pointer = reinterpret_cast<double*>(&argument);
myFunction(1, "Hello!", pointer);
I MADE A MISTAKE. IT'S THE POINTER WHICH IS PASSED AS ARGUMENT. SORRY
The third parameter expects a DOUBLE and reinterpret_cast works for almost all variable types I tested(int, long, ulong, BOOL, BSTR, LPSTR) but it seems it doesn't work the same way with DOUBLEs.
Am I passing the pointer in a wrong way into the function? I wanna pass the value of 20.95 directly as in the first example above but I need this pointer. Please help.
Thanks for your attention. :)
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Does it crash as soon as it calls the function or when you in the function casts it back?
Shouldn't it be pointer = reinterpret_cast<void*>(&argument); instead?? Does it crash if you do a static cast or a C "(void*)argument"-cast?
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Hi, z33z.
I tried static_cast as well. I create this void* pointer because I don't know which variable type it will be passed as argument and the function accepts any types. It works if the argument is an int or LPSTR and I covnert the pointer like this:
Code:
//A pointer to an int
pointer = reinterpret_cast<int*>(argument);
//A pointer to a C string
pointer = reinterpret_cast<LPSTR*>(argument);
//A pointer to a bool value
pointer = reinterpret_cast<BOOL*>(argument);
All above work successfully when passing the pointer as a parameter:
Code:
myFunction(1, "Hello!", pointer);
I'm stuck here because I need to pass a simple double value to a function like this like 20.95 but it's accepted as a wrong parameter, I'm sure and the function fails only in this case. I know it's something to do with the way doubles and floats are stored in memory and retrieved by the pointer but I can't understand how.
Why wouldn't it work like the var types I mentioned above? :(
Thanks for your help so far.
-
Re: Question about pointers to DOUBLEs (VC++6.0)
But still it should be
pointer = reinterpret_cast<void*>(&argument);
instead of
pointer = reinterpret_cast<double*>(&argument);
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Really? :D
Thank you!
I'll try that later.
*offers beer*
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Really! And don't like beer.. ;)
What does the debugger say is the reason for the crash? If you're debugging it in VS.
-
Re: Question about pointers to DOUBLEs (VC++6.0)
:D
Is there a way to stop the execution of a program from a given line and show the values of the variables while executing the extension? I mean, a breakpoint inside the program.
I'm 98% sure that it's how I'm reinterpret_casting the pointer to the double value. I replaced the type with <void*> like you advised but still the same error. Only when passing doubles (although it comes from a DOUBLE variable and not a double). Everything is fine with other parameters, even strings, integers and BSTR strings. I do the same thing with a DOUBLE and it doesn't accept the parameter.
Since I'm using a COM interface to interact with other objects, the error message doesn't come from VC++ but from the object itself but it tells me that it's a wrong parameter. I can pass 10 integers, 10 strings and it'll work just fine.
If I could make a breakpoint inside the program that would show me the value inside the pointer after I typecasted it, I think this would make things clearer. Is there a way?
Thank you, z33z. Your help is much appreciated.
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Make a breakpoint as normal by hitting F9 on the correct line, then search for CopyRun.bat in the official SDK docs for what to do next in order to debug :)
-
Re: Question about pointers to DOUBLEs (VC++6.0)
If I understood your problem correctly you are passing a pointer instead of a value, and thats causing the problem.
Try using:
Code:
myFunction(1, "Hello!", *((double*)pointer));
And see if it solves your problem.
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Jamie: Thank you. I'll try that. :)
Werbad: The problem is I can't modify the way the function is called because at the time it's called, I don't know which variable it is which is passed as argument. It could be a double or it could be anything else. So I just call it like:
myFunction(1, "Hello!", pointer);
where pointer can be anything depending on what parameters are passed. If I could make the pointer act for doubles just the same for all other variables before the pointer is passed. I tried just about anything, even unsafe methods(although some say that reinterpret_cast may cause some troubles). Thanks for your help though. :(
-
Re: Question about pointers to DOUBLEs (VC++6.0)
This should work for pointing "pointer" to "argument"
pointer = &argument;
then if you need to read from the pointer
double arg = *(double*)pointer;
I'm not very good when it comes to pointers, but this should work, aight?
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Hi, DanielRehn.
Thanks for the advice but I can't pass a variable like "arg" in the function, only a pointer instead. The problem is, I can't change the way the function is called. What puzzles me is that it works with every other variable type.
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Will the pointer always point to a double?
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Nono.. that's not what I meant...
use this and pass pointer
pointer = &argument;
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Werbad: the pointer is originally a void pointer and then I change into the type using reinterpret_cast.
Daniel: I tried that before. :(
Maybe it's because of the size of the pointer from void* to DOUBLE* which is not correctly resized when I use reinterpret_cast?
-
Re: Question about pointers to DOUBLEs (VC++6.0)
A pointer is always the same size, 4 bytes, since it's just a memory address (sizeof(void*) == sizeof(double*)). And you can debug you extension as any application/dll, no difference, set breakpoints, step over/into code etc. From the help file:
Quote:
Originally Posted by helpfile
Open the Extensions \ Help \ Misc Documentation folder of the SDK and copy the file named "CopyRun.Bat" just beside MMF2.EXE in your MMF2 directory.
If it does not already exist, create a "Temp" directory at the root of your C: drive. In this directory, create a "Rt" folder. Note: you can also edit CopyRun.bat and change the name of the directory. See below.
Load your extension project file in Visual C++, open the Project Settings, display the "Debug" tab. In the line "Executable for debug session", enter the pathname to the file named "EdRt.exe" that you will find in the Data\Runtime folder of your MMF2 folder.
In the "Program argument" line, enter "/fC:\Temp\Rt\App0.ccn".
How to debug?
Load MMF2, and create the application with the object that you want to debug.
Hold down the SHIFT key while you click on the Run Frame or Run Application icon : this will instruct MMF2 to run the file "CopyRun.bat" so that the temporary files produced for the runtime are copied to the C:\Temp\Rt folder.
Go into Visual C++, and press F5 to start the debugging : it will launch the runtime executable EdRt.exe with a command line indicating where to find the files to play. In this case, it will play App0.ccn found in the temporary folder on your C: drive, the file we just saved. Of course you can put breakpoints in your code.
And I don't think the problem has anything to do with casting of pointers, it's something else that causes the problem.
-
Re: Question about pointers to DOUBLEs (VC++6.0)
I think you have misunderstood what reinterpret_cast actually does. There is no way of changing the type of any variable; Your variable pointer is initialized as a void* and will always be a void*.
In your example you use this pointer and point it at the address of a double value, but it will still remain a void*, aka a typeless pointer. You get an error in your function because the compiler doesn't know how to handle pointer, since it has no type.
I'm still curious why you use a void* and not simply a double*, since it will only be used for doubles.
-
Re: Question about pointers to DOUBLEs (VC++6.0)
What he's trying to do is a function that can take any type of object, and inside the function he somehow knows what object it is and casts it back to correct object pointer again. It's very common to use void pointers as parameters, since you then can pass anything.
At least I hope that he in the function knows what he has sent in and dereferences it correctly, otherwise there will be problems. :)
The compiler has no problems with void pointers, since all pointers are of the same size, the problem is if you cast it back to wrong type:
void* pV = &iMyInt;
double d = *(double*)pV; // Will access wrong memory due to different sizes of types.
But int iMyNewInt = *(int*)pV; would work just fine.
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Guys, your help is invaluable. I'll try that later when I'm at home. But I sense that debugging the code while the double is being passed to the function is something that will clear things up a lot. Thank you.
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Quote:
What he's trying to do is a function that can take any type of object, and inside the function he somehow knows what object it is and casts it back to correct object pointer again
Perhaps he can use a template function in this situation? It depends on what he wants to do with the object I guess.
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Confirmed now. It's the pointer to the double. The function somehow accepts pointers to any basic types, even strings but not to doubles or floats.
Is there any other way to pass arguments of unknown type to a function without using pointers? I tried with variants but with no success.
Thanks.
-
Re: Question about pointers to DOUBLEs (VC++6.0)
You could use unions, function overloading or templates, when writing a function which should take different parameters. What are you trying to do? What is the function used for?
(If you want, you could send me the extension code, and I can tell you how to do it if you don't get it to work.)
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Hi, z33z. The function I use is this:
Code:
PutValue(comobj o, LPCOLESTR command, ...);
The "..." represents infinite parameters with a *marker pointer.
Now I call the function like this:
Code:
Put Value(rdPtr->obj, L"Columns(%s).Size = %e", param[0], param[1], param[2], param[3], param[4], param[5],
param[6], param[7], param[8], param[9], param[10]);
"%s" tells me the parameter is an LPSTR argument and "%e" is a double.
The param[i] array is a pointer array which I change the types of each pointer individually depending on what is passed by the user. So I must have in param[0], by the example above, an LPSTR string and in param[1] a double. Since there are only two arguments in the command(%s and %e), the rest of the parameters passed will be NULL and that is acceptable by the function.
The user passes as arguments a string like "A:E,250.95" which I extract the parameters individually and convert the pointer accordingly.
Code:
_variant_t parameter1;
_variant_t parameter2;
parameter1.ChangeType(VT_LPSTR);
parameter1 = "A:E";
parameter2.ChangeType(VT_R8);
parameter2 = 250.95;
param[0] = reinterpret_cast<void*>(ConvertBstrToAnsiStr(parameter1.bstrVal));
param[1] = reinterpret_cast<void*>(¶meter2.dblVal);
The rest of the parameters, I pass NULL. If in param[1], there is an int, the function will work correctly. I've tried with many parameters at the same time and if they are not of DOUBLE type, it'll work flawlessly.
Can you see what's wrong here, please? If not, I can send you the source code. Thank you once again. :)
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Hmm, don't see it at first glance. You could send it to the_znail at hotmail.com and I can see if I can help you.
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Sure thing. Once I'm home, I'll do that.
Thanks. :)
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Hi, z33z. The e-mail was sent.
It's the source code with an example which shows the problem with the PUT VALUE function. Thanks. :)
-
Re: Question about pointers to DOUBLEs (VC++6.0)
Hi, guys. Some news here. :D
Now that I found a way to do it, I'm facing a horrible C1001 - INTERNAL COMPILER ERROR and, from what I've read so far, it's a compiler bug since other compilers like gcc can manage the same source code. It's when using a class as a template parameter and returning that class. Like:
//V is a class
template <typename TYPE>
TYPE variant_cast<TYPE>(V &var)
{
return var;
}
So I'm on a quest to upgrade to Visual C++ 2005 Express. Does anyone use it? Sorry to be offtopic but I didn't want to create a new topic just for this post.
Thank you. And happy kliking... ;)
Sincerely,
Andre Guerreiro (byo)