Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C++ application giving a wrong result (Visual C++)

c++ application dll visual studio


This topic has been archived. This means that you cannot reply to this topic.
16 replies to this topic

#1 zohratl

zohratl

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 01 October 2014 - 05:39 AM

Well, I've buil a DLL and I am trying to use it within an application. The function I am calling in my application gives me the version of the DLL (The version is set manually in a file called Version.h). Following are pieces of my code:

 

Version.h:

unsigned const MAJOR_VERSION = 1;
unsigned const MINOR_VERSION = 0;

Application.cpp

 

#include <iostream> //for cin, cout and endl
using namespace std;


#include "Class2.h" 
#include "Class1.h" 
using namespace MyDLL;

int main()
{
    Class1 Inst;
    int Choice;
    int Info;

cout<<"Welcome to MyDLL"<<endl;
cout<<"Enter 1 to display MINOR Version and 0 to display MAJOR Version of MyDLL"<<endl;
cin >> Choice;
Inst.GetInfos (Choice, &Info);
    cout<<"The Version asked for is "<<Info<<endl;
cin.get();//pause console to see the message

return 0;


}

The function Code

 

short Class1::GetInfos
(
        int    ID,
        int    *Infos
    )
{
    while (Infos!= NULL) {
            switch (ID)
            {
               case 0:
               {int  MAJOR_VERSION = reinterpret_cast<int>( &Infos );}
               break;
               case 1:
               {int  MINOR_VERSION = reinterpret_cast<int>( &Infos );}


               return '0';
           }
          }
    };

When I execute the executable I get this:

 

When 0: it goes back to line

 

When 1: -858993460

 

 

Why I am not getting the right value?

 

 



#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 01 October 2014 - 06:55 AM

I don't see any assignment made to Infos anywhere in your GetInfos function.

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 zohratl

zohratl

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 01 October 2014 - 06:56 AM

int  MINOR_VERSION = reinterpret_cast<int>( &Infos )

 is equivalent to Infos= MINOR_VERSION.



#4 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 01 October 2014 - 06:57 AM

Nope. The variable that gets assigned a value always goes on the left hand side.

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5 zohratl

zohratl

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 01 October 2014 - 07:06 AM

You're saying that

int  &Infos = reinterpret_cast<int>(MINOR_VERSION )

is the right way to go?



#6 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 01 October 2014 - 07:09 AM

Remove the int type specifier from &Infos. You do not want to declare a new local variable here as the destination.

Edit:

Additionally, &Infos is not the way you dereference int *Infos. It should be:
*Infos = (some value you want to assign to Infos);

Edited by gregwarner, 01 October 2014 - 07:13 AM.

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#7 zohratl

zohratl

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 01 October 2014 - 08:14 AM

It didn't work,I've got this error: C2440: The compiler cannot cast from 'const unsigned int' to 'unsigned int'.



#8 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 01 October 2014 - 08:15 AM

Post your updated code please.

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#9 zohratl

zohratl

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 01 October 2014 - 08:24 AM

I just remplaced  the line we're talking about by 

*Infos = reinterpret_cast<int>(MINOR_VERSION);

Same for MAJOR_VERSION.


Edited by zohratl, 01 October 2014 - 08:25 AM.


#10 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 01 October 2014 - 08:32 AM

You declared MINOR_VERSION to be of type unsigned const, so therefore it follows that it must be cast to type int const.

Edit: Incidentally, is there some specific reason you are using reinterpret_cast as opposed to something safer, such as static_cast? Casting an unsigned to a signed could cause problems if your unsigned > INT_MAX when using reinterpret_cast. (It's not going to cause any problems with your example, when using only 0 and 1, however it's best to support good coding habits.)

Edited by gregwarner, 01 October 2014 - 08:37 AM.

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#11 zohratl

zohratl

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 01 October 2014 - 08:36 AM

Isn't that the whole point of the reinterpret_cast operator?

 

Otherwise, how could I assign to my pointer the value of MAJOR_VERSION for example, to be able to retrieve it in my application later on.



#12 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts

Posted 01 October 2014 - 08:41 AM

Think about it this way: You are trying to cast a constant variable into a non-constant variable. The compiler doesn't have any way of knowing at the time of cast what you're going to end up doing with that variable. You might try and modify it, which violates its constant rule. If you want to cast a constant variable to a different type, then that other type needs to be constant also, so the compiler can be assured that you're not going to try and change the value of that constant variable.

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download