NO

Author Topic: I miss printf() and other beginner Window programming frustrations  (Read 6234 times)

colepc

  • Guest
I knew I couldn't use printf() in a Windows program but I thought at least I could use sprintf(). But no, I was told that it could not be used and I would have to use StringCbPrintf() or StringCchPrintf() instead. They are harder to use and I have yet to make ether one of them work correctly. Guess what value for xx is displayed by popup() in the following code.
int xx;
char pop[301];
xx=2;
StringCbPrintf(pop, 300, "xx=%d",  " ", xx);
popup(pop);

You guessed wrong, xx=1073776114. This is a typical example of how frustrating it is for a beginner to work with Window programming.

The point is I need help with Window programming. Are there any good beginner and reference books available?   

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: I miss printf() and other beginner Window programming frustrations
« Reply #1 on: January 28, 2015, 06:45:41 PM »
I knew I couldn't use printf() in a Windows program but I thought at least I could use sprintf(). But no, I was told that it could not be used and I would have to use StringCbPrintf() or StringCchPrintf() instead.
????? Why? Who say that?


They are harder to use and I have yet to make ether one of them work correctly. Guess what value for xx is displayed by popup() in the following code.
int xx;
char pop[301];
xx=2;
StringCbPrintf(pop, 300, "xx=%d",  " ", xx);
popup(pop);

You guessed wrong, xx=1073776114. This is a typical example of how frustrating it is for a beginner to work with Window programming.

Try:
Code: [Select]
StringCbPrintf(pop, 300, "xx=%d", xx);

The point is I need help with Window programming. Are there any good beginner and reference books available?
Sure  :)
Windows usage is quite different, and frustration for those who come from consolle printout is normal  ;)
The starting point remain the old good "Programming Windows" from Charles Petzold...
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

colepc

  • Guest
Re: I miss printf() and other beginner Window programming frustrations
« Reply #2 on: January 29, 2015, 02:45:50 AM »
Thanks Frankie. Frustration 256 has been solved. I got a little carried away with the example I found and kept a string parameter in StringCBPrintf() that I didn't support.

I use printf() often with command line. Interestingly, it shows no error using Pelles C for Windows however I don't have a clue as to where its output goes (where is stdout for Windows?).
 
Pelles C for Windows says I can't use sprint(). When I use sprintf() like this:
#include <stdio.h>
int xx;
char pop[301];
xx=2;
sprintf(pop, "xx - %d", xx);

I get this error:   
error #2048: Undeclared identifier 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA'.
*** Error code: 1 ***

czerny

  • Guest
Re: I miss printf() and other beginner Window programming frustrations
« Reply #3 on: January 29, 2015, 08:02:14 AM »
colepc: Please use code tags for your examples.
Code: [Select]
#include <stdio.h>

int main(int argc, char *argv[])
{
int xx;
char pop[301];
xx = 2;
sprintf(pop, "xx - %d", xx);

return 0;
}
This one works ok for me! Try to explain your problem better.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: I miss printf() and other beginner Window programming frustrations
« Reply #4 on: January 29, 2015, 12:18:01 PM »
He is compiling in GUI.
Try the sample project Colepc.

P.S. I understoood Colepc you have used strsafe.h that remove the usage of 'unsafe' functions.
If you define STRSAFE_NO_DEPRECATE before the include you have again the functions.
I.e.
Code: [Select]
#define STRSAFE_NO_DEPRECATE 1
#include <strsafe.h>
You can still use sprintf, etc.
« Last Edit: January 29, 2015, 04:42:02 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

colepc

  • Guest
Re: I miss printf() and other beginner Window programming frustrations
« Reply #5 on: January 29, 2015, 08:29:04 PM »
Thanks Frankie. I can now use sprint(). I'm a happy camper :)

Now that I know about strsafe.h and STRSAFE_NO_DEPRECATE it was easy to find that these are Microsoft creations and not Pelles C creations. However, doing a search for sprintf does not seem to help me find strsafe.h and STRSAFE_NO_DEPRECATE. I even tried searching for the error 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' and my results were confusing with some results discussing strsafe.h and one result discussing STRSAFE_NO_DEPRECATE but I didn't find a discussion of the two together. Thanks again Frankie for connecting the two.