News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Problem with wsprintf

Started by Robert Collins, February 04, 2005, 07:52:32 PM

Previous topic - Next topic

Robert Collins

I'm trying to write a DLL in 'C' and I am having a bit of a problem using the wsprintf function. In the below code snippit (which is just a testing for what I want to do later) the array 'digest' is initialized with an already converted MD5 text string. In the for loop I put both a printf and a wsprintf to test them. The output of printf is correct but the out put of wsprintf is totally incorrect and beyond my reason of what is going on. I though that wsprintf behaves exactly the same as printf except instead of printing the results it places the results in a string buffer which I have declared as an array the same size as the 'digest' array. What am I doing wrong? Any help will be greatly appricated. Thank you.


#include <windows.h>
#include <stdio.h>

int MD5String();

void main()
{
   MD5String();
}
int MD5String()
{
  unsigned char digest[] = "ÅÈûMÞžõ\nM%Œe–Bƒ.";
  unsigned char md5hex[16];
  unsigned int i;

  for (i = 0; i < 16; i++)
  {
    printf("%02x", digest[i]);
    wsprintf((LPTSTR)md5hex[i], "%02x", digest[i]);
  }
   
  printf("\nMD5 = ");
  printf(md5hex);
  printf("\n");

  return 0;
}

Relvinian

Quote from: Robert Collins on February 04, 2005, 07:52:32 PM
I'm trying to write a DLL in 'C' and I am having a bit of a problem using the wsprintf function. In the below code snippit (which is just a testing for what I want to do later) the array 'digest' is initialized with an already converted MD5 text string. In the for loop I put both a printf and a wsprintf to test them. The output of printf is correct but the out put of wsprintf is totally incorrect and beyond my reason of what is going on. I though that wsprintf behaves exactly the same as printf except instead of printing the results it places the results in a string buffer which I have declared as an array the same size as the 'digest' array. What am I doing wrong? Any help will be greatly appricated. Thank you.


#include <windows.h>
#include <stdio.h>

int MD5String();

void main()
{
   MD5String();
}
int MD5String()
{
  unsigned char digest[] = "ÅÈûMÞžõ\nM%Œe–Bƒ.";
  unsigned char md5hex[16];
  unsigned int i;

  for (i = 0; i < 16; i++)
  {
    printf("%02x", digest[i]);
    wsprintf((LPTSTR)md5hex[i], "%02x", digest[i]);
  }
   
  printf("\nMD5 = ");
  printf(md5hex);
  printf("\n");

  return 0;
}


Robert,

The syntax you are using for WSPRINTF is corrupting your stack.  You are trying to store 32 bytes into a 16-byte allocation.  ;-)

Think of WSPRINTF as a formatting string (like printf but uses a "buffer" to hold its output).

For what you are trying to do here is an example:

int MD5String()
{
  unsigned char digest[] = "ÅÈûMÞžõ\nM%Œe–Bƒ.";
  unsigned char md5hex[(sizeof(digest) * 2 )+ 1];    // 2-bytes per character plus a NULL for HEX output
  unsigned int i;

  for (i = 0; i < 16; i++)
  {
    wsprintf((char)md5hex[i*2], "%02x", digest[i]);
  }
   
  printf("\nMD5 = ");
  printf(md5hex);
  printf("\n");

  return 0;


That should give you the correct output you are looking for. What you need to make sure you handle correctly is the correct size of the buffer you are trying to put "formatted" text into.  Since you are displaying the string as a HEX value, you must ensure your formatting buffer is allocated big enough.

Hope this helps. You may need to dynamically allocate a format buffer instead of using the sizeof() to correctly compile this example but it should be enough to get you an idea.

Relvinian

Robert Collins

Ummmmm............well if I use what you posted I get the following compilier warnings:

c:\....\md5.c(20) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'char '
c:\....\md5.c(20) : warning C4024: 'wsprintfA' : different types for formal and actual parameter 1

If I use wsprintf((LPTSTR)md5hex[i*2]..... instead I do not get any compilier warnings or errors.

However, wheather I use (char) or (LPTSRT) doesn't matter the results are the same and both are still incorrect. The md5hex array either is not being filled in from the for loop or it is being filled in with ascii characters that have a decimal value of 204. 

Gustav


I would suggest:

Quote
    wsprintf((LPTSTR)&md5hex, "%02x", digest);

why? because expression md5hex is type char, but md5hex is type "char *". So your cast doesnt do what it is supposed to do.

Robert Collins

Thanks to the both of you :U   Relvinian for the [i*2] and Gustav for the &md5hex.

The one that works is:


wsprintf((LPTSTR)&md5hex[i*2], "%02x", digest[i]);

Robert Collins

Actually it turns out that the (LPTSTR) isn't required. So, just wsprintf(&md5hex[i*2], "%02x", digest); is all that is needed.