Thread: Trying to make C++ DLL, got a problem with hWnd

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    Trying to make C++ DLL, got a problem with hWnd

    Hi,

    First, I have to tell you, I'm new at the forums. I signed up because I started programming in C++ and often get problems, most of them are solved easily, but I have to wait for a friend to reply on the e-mail I send before I can continue with my project. Well, anyway, as you've propably read from the topic title, my problem has something to do with hWnd. I want to know which window is on the top in my DLL, I thought I could do that with this function:
    Code:
    export double window_getfocus(double a)
    	{
    	if (GetForegroundWindow() == a)
    		{
    		return 1;
    		}
    	else
    		{
    		return 0;
    		}
    	}
    When I build it, Visual C++ tells me that there is no conversion from the double a to the hWnd from the function. With this information I've searched for an hour on the internet, but found nothing understandable [that's probably because of me :P]. Now my question is, how can I change the double a to a hWnd type?

    Ragoune

    Btw, I'm making this DLL for my program in Game Maker cause I like the easyness of it ^^ [no offense]. But Game Maker uses only strings [char*s I believe] and reals [doubles I believe] so it might be possible to change double a to char* a but I prefer the double.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    The obvious thing to do here is to typecast a to a HWND, but you would still be doing the wrong thing.

    Tell me - why are you using a double? Why does your logic tell you that a double should contain an information about a window? Of course, everything is really just a sequence of bytes, but types are still a somewhat well-defined concept, especially if you compare C++ to C.

    You should be using the HWND type for your variables, and again, I would be interested in knowing why double seemed logical for you.

    EDIT: Sorry, neglected to read the last few lines.

    In C++, you cannot convert a double to a window handle(HWND). HWND is a struct, and a double is a scalar.
    Last edited by IceDane; 03-11-2009 at 08:44 AM.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    K, but there isn't another way of getting to know which window is on top and equals a window id as a double type? Maybe by changing the type several times?

    Ragoune

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Where do you get the id as double from in the first place?

    In <winnt.h> the following macro is defined:
    Code:
    #ifdef STRICT
    typedef void *HANDLE;
    #define DECLARE_HANDLE(n) typedef struct n##__{int i;}*n
    #else
    typedef PVOID HANDLE;
    #define DECLARE_HANDLE(n) typedef HANDLE n
    #endif
    Then in <windef.h> the name HWND is created as follows:

    Code:
    DECLARE_HANDLE(HWND);
    As you can see, HWND can be a different thing based on whether STRICT is defined: a pointer to a struct containing an int (I suppose the integer is unused and the whole idea is to create a unique type of pointer and the pointer itself identifies the window), or a void pointer.

    The whole idea of things like HWND is that you don't need to know what's behind them and also to prevent stupid things like comparing HWND to something completely unrelated (like double) from compiling.

    You should be using HWND all the way.
    Last edited by anon; 03-11-2009 at 09:13 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    Well, then I'll have to look for another way to do [something like] this .
    Thanks all for helping!

    Ragoune

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    export double window_getfocus(double a)
    Why do you use double data type to return 1 or 0 instead of bool or perhaps int?

    The name of the function seems a little misleading. It doesn't get focus (whatever that means), it checks to see if the window referenced by a is the top window.

    Where do you think you'll get a window handle as double from in the first place. You can either use HWND or perhaps void* (with a cast in the function body).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    hi , i am a fresh student to learn C programing and i like to know very much but it s a new subject for me i dont know how to do how to study to know better ,pleas tell me how can i do ?

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    Quote Originally Posted by anon View Post
    Code:
    export double window_getfocus(double a)
    Why do you use double data type to return 1 or 0 instead of bool or perhaps int?

    The name of the function seems a little misleading. It doesn't get focus (whatever that means), it checks to see if the window referenced by a is the top window.

    Where do you think you'll get a window handle as double from in the first place. You can either use HWND or perhaps void* (with a cast in the function body).
    As I said in my topic post, I'm making this DLL for Game Maker. And Game Maker only uses 2 types of variables: strings [char*s] and reals [doubles]. When I execute my game from Game Maker I use a function to get the window id [window_handle() is the function] and it returns a double as the ID of the game window.
    This also means that Game Maker doesn't understand a bool or void* so you can't use it as a return value in the DLL.
    So maybe you know another way to check whether a window is the focussed window?

    Ragoune

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, you can do it by reinterpreting bit patterns, but that's nothing a beginner should do.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 05:36 AM
  2. problem in calling DLL
    By bhagwat_maimt in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 10:43 PM
  3. fullscreen toggling problem
    By hannibar in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2005, 08:06 PM
  4. Bitmap Displaying Problem
    By MadCow257 in forum Game Programming
    Replies: 0
    Last Post: 01-25-2005, 05:21 PM
  5. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM