catma
asked on
A simple problem involving pointers.
I am trying to get the name of a window, given the handle. I don't know much C, but I learned Perl before, so variables and basic concepts are not foreign to me. The pointers are a problem though. Can you tell we what is wrong with this code? i only get (null) as the output. im pretty sure this is lousy code too, how would u write it?
how would i get the hex representation of the pointer, like 0xFFFFCCCC?
is the handle supposed to be in decimal or hex? both give me (null)
also, is:
printf("%s", (char *) Pointer);
the same as:
printf("%s", Buffer);?
code:
char Buffer;
char * Pointer;
HWND hWnd;
char szWindowHandle;
Pointer = &Buffer;
gets( szWindowHandle );
hWnd = (HWND) szWindowHandle;
GetWindowText(hWnd, Pointer, 25);
printf("Window name is %s\n ", Buffer);
how would i get the hex representation of the pointer, like 0xFFFFCCCC?
is the handle supposed to be in decimal or hex? both give me (null)
also, is:
printf("%s", (char *) Pointer);
the same as:
printf("%s", Buffer);?
code:
char Buffer;
char * Pointer;
HWND hWnd;
char szWindowHandle;
Pointer = &Buffer;
gets( szWindowHandle );
hWnd = (HWND) szWindowHandle;
GetWindowText(hWnd, Pointer, 25);
printf("Window name is %s\n ", Buffer);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you want to input string in hex, then use
sscanf to change it to numeric.
Ex:
Excuding the gets part.
char szWindowHandle[100] = "34AB8;
unsigned long handle;
sscanf(szWindowHandle, "%x", &handle);
hWnd = (HWND) handle;
sscanf to change it to numeric.
Ex:
Excuding the gets part.
char szWindowHandle[100] = "34AB8;
unsigned long handle;
sscanf(szWindowHandle, "%x", &handle);
hWnd = (HWND) handle;
> Is
>printf("%s", (char *) Pointer);
> the same as:
>printf("%s", Buffer);?
No they are not same - the first line is correct - it will print out the string to which Pointer is pointing.
The second line will most probably lead to a crash - since printf will interpret the second parameter i.e. Buffer as a char * , led to this by %s in the first string parameter .
In second line - also note that by writing ( char *) Pointer as a expression you tell an compiler that you want this variable to be treated as a pointer( called typecasting ) . Since Pointer variable is defined as a char * this is redundant.
I guess all other questionshave been covered by rajeev_devin above .
cheers,
vishalrix
>printf("%s", (char *) Pointer);
> the same as:
>printf("%s", Buffer);?
No they are not same - the first line is correct - it will print out the string to which Pointer is pointing.
The second line will most probably lead to a crash - since printf will interpret the second parameter i.e. Buffer as a char * , led to this by %s in the first string parameter .
In second line - also note that by writing ( char *) Pointer as a expression you tell an compiler that you want this variable to be treated as a pointer( called typecasting ) . Since Pointer variable is defined as a char * this is redundant.
I guess all other questionshave been covered by rajeev_devin above .
cheers,
vishalrix
ASKER
thanks for the help!
i tested this code:
#include "windows.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
char mybuffer[1000];
HWND hWnd;
char szWindowHandle[100];
gets( szWindowHandle );
hWnd = (HWND) atoi(szWindowHandle); // Convert string to number.
GetWindowText(hWnd, &mybuffer, 1000);
printf("Window name is %s\n ", mybuffer);
}
and it worked. but i get two warnings:
C:\Hax\c++\getwindow.c(15) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'char (*)[1000]'
C:\Hax\c++\getwindow.c(15) : warning C4024: 'GetWindowTextA' : different types for formal and actual parameter 2
not sure what they mean.
also if i used rajeev_devin's:
hWnd = (HWND) int atoi(szWindowHandle); // Convert string to number.
i get error in addition to warnings:
C:\Hax\c++\getwindow.c(12) : error C2059: syntax error : 'type'
so i removed the 'int' in front of atoi and it worked.
i also want to ask why i have to convert to the input to integer beforehand. thanks
i tested this code:
#include "windows.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
char mybuffer[1000];
HWND hWnd;
char szWindowHandle[100];
gets( szWindowHandle );
hWnd = (HWND) atoi(szWindowHandle); // Convert string to number.
GetWindowText(hWnd, &mybuffer, 1000);
printf("Window name is %s\n ", mybuffer);
}
and it worked. but i get two warnings:
C:\Hax\c++\getwindow.c(15)
C:\Hax\c++\getwindow.c(15)
not sure what they mean.
also if i used rajeev_devin's:
hWnd = (HWND) int atoi(szWindowHandle); // Convert string to number.
i get error in addition to warnings:
C:\Hax\c++\getwindow.c(12)
so i removed the 'int' in front of atoi and it worked.
i also want to ask why i have to convert to the input to integer beforehand. thanks
ASKER
i find that if i use :
GetWindowText(hWnd, mybuffer, 1000);
instead of:
GetWindowText(hWnd, &mybuffer, 1000);
i don't get any of those errors or warnings... but isn't the reason you put &buffer is because that parameter is for a pointer... ?
GetWindowText(hWnd, mybuffer, 1000);
instead of:
GetWindowText(hWnd, &mybuffer, 1000);
i don't get any of those errors or warnings... but isn't the reason you put &buffer is because that parameter is for a pointer... ?
One of the properties of array(s) is that their name stands ( always ?) for their address - so they can be used as char * by themselves like you do for
GetWindowText(hWnd, mybuffer, 1000); /* myBuffer behaves as a char * */
whereas in a statement like
GetWindowText(hWnd, &mybuffer, 1000);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> i also want to ask why i have to convert to the input to integer beforehand
That was a typo :)
That was a typo :)
>> i find that if i use :
>> GetWindowText(hWnd, mybuffer, 1000);
That was again a typo :)
You are correct.
>> GetWindowText(hWnd, mybuffer, 1000);
That was again a typo :)
You are correct.
>One of the properties of array(s) is that their name stands ( always ?) for their address - so they can be used as >char * by themselves
The above reply ( by me ) is wrong and misleading. What I mean is that array name variables stand for pointers to the type they contain ... so if we have
int arr[100];
then arr by itself is an int * .
The above reply ( by me ) is wrong and misleading. What I mean is that array name variables stand for pointers to the type they contain ... so if we have
int arr[100];
then arr by itself is an int * .
ASKER
thanks guys. everything is clear now :)
>> char szWindowHandle;
Sizes are not correct here.
>> gets( szWindowHandle );
Your this statement should have raised compilation error here. Since your szWindowHandle is a char.
Byt gets takes char* as input.