|
Because I need to use the MD5 algorithm, and I am really lazy, I decided to use the MD5Init, MD5Update, MD5Final API provided by Windows, but Microsoft is also lazy. These three APIs do not even provide header files, so I had to use LoadLibrary. GetProcAddress. But the problem appeared, after the compilation and running prompt:
Run-Time Check Failure # 0-The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
The problem is where MD5Init is called. Disassembly, it was found that when the API function returned, the ret 4 instruction was used (MD5Init has only one parameter: a pointer to the MD5_CTX structure). After returning, it was found that the code generated by VS added an instruction such as Add esp, 4, and ESP was added by mistake. 4 bytes were loaded, so the above problem occurred. But the same code I compile with GCC3.4.4 without problems.
How to solve this problem. The source code is as follows:
//////////////md5.h//////////////////////
#pragma once
#include <windows.h>
typedef struct _MD5_CTX
{
ULONG i [2];
ULONG buf [4];
unsigned char in [64];
unsigned char digest [16];
} MD5_CTX;
typedef void (* PMD5Init) (
MD5_CTX * context
);
typedef void (* PMD5Update) (
MD5_CTX * context,
unsigned char * input,
unsigned int inlen
);
typedef void (* PMD5Final) (
MD5_CTX * context
);
////////////////////////md5.cpp///////////////////
#include "stdafx.h"
#include "md5.h"
void GetMD5Hash (MD5_CTX * md5)
{
HMODULE hmodule = LoadLibrary (TEXT ("Cryptdll.dll"));
PMD5Init MD5Init = (PMD5Init) GetProcAddress (hmodule, "MD5Init");
Ranch
MD5Init (md5); // This is it! !! !!
FreeLibrary (hmodule);
}
int _tmain (int argc, _TCHAR * argv [])
{
MD5_CTX md5;
GetMD5Hash (&md5);
return 0;
} |
|