Search code examples
c++cprintfmsvcrt

Converting a DWORD without CRT, implementing custom sprintf() function


Background:

My application ships without a CRT as it MUST be portable and small.

Problem:

When I run my application on a Virtual Machine for Windows XP, my CreateProcess() function fails, I need to display the GetLastError() DWORD in a MessageBox() without using any of the CRT,

How can I manually implement sprintf() function in my program to convert the DWORD type to a char buffer to display in the MessageBox() function to get the error type, so I can therefore fix the problem.

I have implemented my own memset() function like so

extern "C" void * __cdecl memset(void *, int, size_t);


#pragma function(memset)
void * __cdecl memset(void *pTarget, int value, size_t cbTarget) {
unsigned char *p = static_cast<unsigned char *>(pTarget);
while (cbTarget-- > 0) {
    *p++ = static_cast<unsigned char>(value);
}
return pTarget;
}

I am unsure of how to implement a custom sprintf() function

Thank you


Solution

  • If speed is not what you'd concern, you can use RtlMoveMemory and wsprintf supplied by kernel32.dll for memcpy(memmove) and sprintf (Not actually THAT slow, just cannot do link time optimization).