| View previous topic :: View next topic |
| Author |
Message |
FerrisBuellerYourMyHero Master Cheater
Reputation: 0
Joined: 14 Feb 2007 Posts: 401 Location: Inside your <kernel>
|
Posted: Wed Dec 31, 2008 4:13 pm Post subject: DbgPrint() for usermode! [SOLVED] |
|
|
Ok so lately I've gotten used to how nice DbgPrint() is in kernel mode. In user mode I've always used sprintf to format my output to a char string, then OutputDebugString to debug print it. I use DebugView to view the output for both DbgPrint, and OutputDebugString.
I'm getting kind of tired of writing two function calls sprintf + OutputDebugString. So I want to make a function like DbgPrint for usermode so I can do it all in one call.
DbgPrint is exported by ntdll.dll so I thought you could use it from user mode. Well you can but the output doesn't show in Debug View for some reason. So I think probably the best way would be to make one function which calls sprintf and OutputDebugString.
Problem is I'm not sure how to go about doing that. sprintf has a variable number of parameters. specified with the "..."
I thought of doing something like this:
| Code: |
void DbgPrint(char* FormatStr, ...)
{
char dbgout[1000];
sprintf(dbgout, FormatStr, ...);
OutputDebugStringA(dbgout);
}
|
but it doesn't compile. How can I pass the unknown number of parameters to sprintf?
Thanks for your help.
_________________
You know, life moves pretty fast. If you don't stop and look around once in a while, You could miss it!

Last edited by FerrisBuellerYourMyHero on Wed Dec 31, 2008 10:18 pm; edited 1 time in total |
|
| Back to top |
|
 |
sponge I'm a spammer
Reputation: 1
Joined: 07 Nov 2006 Posts: 6009
|
Posted: Wed Dec 31, 2008 5:14 pm Post subject: |
|
|
Buffer to structure of parameters.
len of parameter structure. then you push it out in the function.
_________________
|
|
| Back to top |
|
 |
FerrisBuellerYourMyHero Master Cheater
Reputation: 0
Joined: 14 Feb 2007 Posts: 401 Location: Inside your <kernel>
|
Posted: Wed Dec 31, 2008 5:56 pm Post subject: |
|
|
hmm... I kind of see what your saying. But then I would have to fill out the structure and set the length before calling the new function. I like it be just be one line to write, like DbgPrint for kernel mode is!
anyways I did a little researching on var arg functions. (variable argument functions) How they are made and such.
you define a va_list then va_start sets up the parameters for you to use.
so...
| Code: |
va_start(vaList, FormatString);
|
that means after the "FormatString" parameter thats where the variable number of parameters starts.
now normally you'd get the parameters one by one with va_arg. However I found this one formatting function called wvsprintf!
and guess what it takes a va_list! perfect!
here's the result:
| Code: |
void DbgPrint(char* FormatString, ...)
{
char* dbgout = new char[1024];
va_list vaList;
va_start(vaList, FormatString);
wvsprintfA(dbgout, FormatString, vaList);
OutputDebugStringA(dbgout);
va_end(vaList);
delete[] dbgout;
}
|
but I think its best to just allocate the dbgout buffer at the start of the program and deallocate it when it quits(or just use the stack). because the constant allocating and deallocating every time you call it seems to slow it down a little bit.
Its simple, no messing around with structs and parameter lengths and pushing params. And best of all it works nicely!
Thanks for posting though.
_________________
You know, life moves pretty fast. If you don't stop and look around once in a while, You could miss it!
 |
|
| Back to top |
|
 |
rapion124 Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Mar 2007 Posts: 1095
|
Posted: Wed Dec 31, 2008 7:14 pm Post subject: |
|
|
Do not define the buffer as a static variable. If you make a multi-threaded app, then there's a very likely chance of memory corruption when 2 threads are calling the function.
You can use the stack. It's safe because there's no recursive calls, making a stack overflow very unlikely. If you want to be safe, just allocate the buffer dynamically.
|
|
| Back to top |
|
 |
FerrisBuellerYourMyHero Master Cheater
Reputation: 0
Joined: 14 Feb 2007 Posts: 401 Location: Inside your <kernel>
|
Posted: Wed Dec 31, 2008 10:22 pm Post subject: |
|
|
| rapion124 wrote: | Do not define the buffer as a static variable. If you make a multi-threaded app, then there's a very likely chance of memory corruption when 2 threads are calling the function.
You can use the stack. It's safe because there's no recursive calls, making a stack overflow very unlikely. If you want to be safe, just allocate the buffer dynamically. |
Yeah I'm just dynamically allocating the buffer upon loading of the app. and freeing the memory when its terminated. It's probably best since stack memory is limited. Save it for when you need it. It's also quicker to do it that way, no sense in allocating and deallocating the memory every call to it.
Anyway I've marked this thread as solved since I got it working like I need it.
Thanks again.
_________________
You know, life moves pretty fast. If you don't stop and look around once in a while, You could miss it!
 |
|
| Back to top |
|
 |
|