473,695 Members | 2,907 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting Function pointers : Can anyone correct this MS Knowledge base Example pls ?

I have only small knowledge of c++, but I would like to compile the
example in
http://support.microsoft.com/kb/q246772/
HOWTO: Retrieve and Set the Default Printer in Windows
I included "#include <Windows.h>" at the start, and the following goes
wrong :

BOOL DPGetDefaultPri nter(LPTSTR pPrinterName, LPDWORD pdwBufferSize)
{
....
PROC fnGetDefaultPri nter = NULL;
....
// This line works :
fnSetDefaultPri nter = GetProcAddress( hWinSpool, SETDEFAULTPRINT ER);
....
bFlag = fnGetDefaultPri nter(pPrinterNa me, pdwBufferSize);
// but this one gives
// GetDfltPrt.cpp( 100) : error C2197: 'int (__stdcall *)(void)' : too
many actual parameters
....
bFlag = fnSetDefaultPri nter(pPrinterNa me);
// and this gives the same.

This should probably be casted, because the number of parameters is
different.
Comments on this code are also welcome. Are there better ways to get
the default printer on all windows platforms ?
Many thanks in advance.
Dirk.
Jul 22 '05 #1
10 2334
Dirk Vanhaute posted:
I have only small knowledge of c++, but I would like to compile the
example in
http://support.microsoft.com/kb/q246772/
HOWTO: Retrieve and Set the Default Printer in Windows
I included "#include <Windows.h>" at the start, and the following goes
wrong :

BOOL DPGetDefaultPri nter(LPTSTR pPrinterName, LPDWORD pdwBufferSize)
{
...
PROC fnGetDefaultPri nter = NULL;
...
// This line works :
fnSetDefaultPri nter = GetProcAddress( hWinSpool, SETDEFAULTPRINT ER);
...
bFlag = fnGetDefaultPri nter(pPrinterNa me, pdwBufferSize);
// but this one gives
// GetDfltPrt.cpp( 100) : error C2197: 'int (__stdcall *)(void)' : too
many actual parameters
...
bFlag = fnSetDefaultPri nter(pPrinterNa me);

If you have a function that looks like so:

int Blah(char, int, bool);
then to define a pointer to such a *type* of function, you do:

int (*p_Blah)(char, int, bool) = Blah;
What's happening in your code is that the types don't match. It's akin to
doing the following:

void Blah(int);

char (*p_Blah)(char, bool,float) = Blah;
What you need to do is change the type of your pointer!
-JKop

Jul 22 '05 #2
>
This should probably be casted, because the number of parameters is
different.
Seems reasonable.
Comments on this code are also welcome.
It's the usual pile of shit.
Are there better ways to get
the default printer on all windows platforms ?
No idea, ask on a Windows programming group. Your question is off topic for
two different reasons, firstly the code is C not C++, secondly Windows
programming is not on topic in a C++ language group. Try
news:comp.os.ms-windows.program mer.win32.
Many thanks in advance.
Dirk.


john
Jul 22 '05 #3

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2t******** *****@uni-berlin.de...

This should probably be casted, because the number of parameters is
different.


Seems reasonable.
Comments on this code are also welcome.


It's the usual pile of shit.


Personal aversion to Windows programming expressed here? Or C-style API
calls in general?

You may not like Windows, but a lot of us have to write C++ code that runs
on that platform. And since the API is in C, we're stuck with C-style
interfaces in our C++ code.
Are there better ways to get
the default printer on all windows platforms ?


No idea, ask on a Windows programming group. Your question is off topic
for
two different reasons, firstly the code is C not C++, secondly Windows
programming is not on topic in a C++ language group. Try
news:comp.os.ms-windows.program mer.win32.


It's valid C++ code. Sure, it's calling a C API function, but if the
program is a C++ program, it's reasonable that the question could be
considered a C++ question.

The problem is apparently the definition of "PROC", which isn't shown. The
OP needs to define the function pointer to match the function declaration,
which it obviously doesn't. Simply using "PROC" is not the answer. The OP
needs to get the actual declaration of the function, and define a pointer
that matches that declaration.

However, you're right that THAT information is something the he probably
needs to go to a Windows newsgroup for, since we don't have (and don't
particularly care about) that info here.

-Howard

Jul 22 '05 #4

"Howard" <al*****@hotmai l.com> wrote in message
news:aX******** **********@bgtn sc04-news.ops.worldn et.att.net...

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2t******** *****@uni-berlin.de...
>
This should probably be casted, because the number of parameters is
different.


Seems reasonable.
Comments on this code are also welcome.


It's the usual pile of shit.


Personal aversion to Windows programming expressed here? Or C-style API
calls in general?


Neither, the example is typical of the poor standard of coding that exists
on MSDN. I particular I don't like the needless use of Windows specific
types (BOOL, DWORD etc) and functions (lstrlen, lstrcat etc) when perfectly
good standard alternatives exist. Also the completely gratuitous casting all
over the place.

john

Jul 22 '05 #5
John Harrison posted:

"Howard" <al*****@hotmai l.com> wrote in message
news:aX******** **********@bgtn sc04-news.ops.worldn et.att.net...

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2t******** *****@uni-berlin.de...
>
This should probably be casted, because the number of parameters is
different.

Seems reasonable.

Comments on this code are also welcome.

It's the usual pile of shit.


Personal aversion to Windows programming expressed here? Or C-style
API calls in general?


Neither, the example is typical of the poor standard of coding that
exists on MSDN. I particular I don't like the needless use of Windows
specific types (BOOL, DWORD etc) and functions (lstrlen, lstrcat etc)
when perfectly good standard alternatives exist. Also the completely
gratuitous casting all over the place.

john

Don't forget the use of ZeroMemory, plus...

typedef struct _Blah
{
int k;
} Blah;
(I think that's the syntax they use!
-JKop
Jul 22 '05 #6

"JKop" <NU**@NULL.NULL > wrote in message
news:e2******** ***********@new s.indigo.ie...

typedef struct _Blah
{
int k;
} Blah;
(I think that's the syntax they use!

That's normal C code, isn't it? Unless C has changed is standards (which I
have no idea), then that's just a complaint about using C. (Which to my
mind is nothing to complain about...in fact, it just makes it easier to use
from a variety of languages, doesn't it?)

-JKop


-Howard
Jul 22 '05 #7
"Howard" <al*****@hotmai l.com> wrote in message :
The problem is apparently the definition of "PROC", which isn't shown. The
OP needs to define the function pointer to match the function declaration,
which it obviously doesn't. Simply using "PROC" is not the answer. The OP
needs to get the actual declaration of the function, and define a pointer
that matches that declaration.


PROC is defined via windef.h as
typedef int (CALLBACK *PROC)();

and CALLBACK as
#define CALLBACK __stdcall

I thought those BOOL, DWORD ... types were something of C++. I write
a little c and use the classical types int, char, long, ...
I don't think Kernighan and Ritchie approve this kind of misuse.

Thanks to JKop, Howard and John for you help.
Dirk.
Jul 22 '05 #8
"Howard" <al*****@hotmai l.com> wrote in message :
The problem is apparently the definition of "PROC", which isn't shown. The
OP needs to define the function pointer to match the function declaration,
which it obviously doesn't. Simply using "PROC" is not the answer. The OP
needs to get the actual declaration of the function, and define a pointer
that matches that declaration.


PROC is defined via windef.h as
typedef int (CALLBACK *PROC)();

and CALLBACK as
#define CALLBACK __stdcall

I thought those BOOL, DWORD ... types were something of C++. I write
a little c and use the classical types int, char, long, ...
I don't think Kernighan and Ritchie approve this kind of misuse.

Thanks to JKop, Howard and John for you help.
Dirk.
Jul 22 '05 #9
Dirk Vanhaute wrote:

"Howard" <al*****@hotmai l.com> wrote in message :
The problem is apparently the definition of "PROC", which isn't shown. The
OP needs to define the function pointer to match the function declaration,
which it obviously doesn't. Simply using "PROC" is not the answer. The OP
needs to get the actual declaration of the function, and define a pointer
that matches that declaration.
PROC is defined via windef.h as
typedef int (CALLBACK *PROC)();

and CALLBACK as
#define CALLBACK __stdcall

I thought those BOOL, DWORD ... types were something of C++.


All of them are simple #define's which eventually lead to int, unsigned
int etc. There is no difference to C.

But Howard is right. What you need to do is to define the function
pointer correctly.

eg. You want the pointer fnSetDefaultPri nter to point to the function
that does 'Set Default Printer'. From the docu you got: it is a function
which returns an int and takes a const char* as its first parameter.
Thus it should be:

int (*fnSetDefaultP rinter)( const char*);

and not

PROC fnSetDefaultPri nter;

PROC is a standard function pointer which doesn't know about
the specifics of the function in question. It is your job to add
this information by writing a correct definition.

Note: GetProcAddress( ) is a function which returns a 'standard function
pointer'. If you think of it, this is very logical. GetProcAddress needs
to return any function pointer regardless of what the real function has as
return type or what arguments it takes. So you have no other choice as to
heavily cast that pointer to the desired type. You an do eg.

(*(PROC*)&fnSet DefaultPrinter = GetProcAddress( hWinSpool, SETDEFAULTPRINT ER );

That is casting the left hand side of the assignment to match what GetProcAddress
returns, instead of the other way round. This way all usages of GetProcAddress look
basically the same, even if there are different types of function pointers are involved.

As for: Are there better ways to get
the default printer on all windows platforms ?


You should ask this in a Windows newsgroup as it is definitily way off topic here.
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3465
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A good way to do this is by example. So I will give an example and please tell me what you think: I have a base class A with a virtual destructor, and a class B that is it inherits publicly from A and defines som extra stuff.
35
2685
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
3
2360
by: Michael B Allen | last post by:
I need a macro to compute an offset of an object relative to a base address. The problem is the object can sometimes be a function pointer with a base address of 0 but I don't know in advance that the object is a function. Can it be done? #define BREF(b,p) ((size_t)((p) ? (char *)(p) - (char *)(b) : 0)) The following code demonstrates that strict c89 doesn't accept casting a pointer to a function to a pointer to char which makes my...
3
1679
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
0
1307
by: Kurt Lange | last post by:
no... the array is created dynamically. and no... that defeats the purpose of what im trying todo.. encapsulate all initializing of variables in base class... derive from it... by deriving from base class, and casting, derived classes would already have their variables initialiezed(cause they have already been initialized in the base class) ....
23
3513
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than the Inherit statement).
22
13110
by: Adam Clauss | last post by:
OK, I have class A defined as follows: class A { A(Queue<B> queue) { ... } } Now, I then have a subclass of both classes A and B. The subclass of A (SubA), more specifically is passed a Queue<SubB>.
9
2455
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I have a derived class object and then upcast it to its base class, which cast operator should I use? Is it static_cast, or, can I simply cast it implicitly without any operator? Does this upcasting remove the derived class portion of the object?...
9
3462
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
0
8640
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8582
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9122
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9001
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7672
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5841
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3018
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2281
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.