I have this code in C:
What would be the euivalent in Win32 API?Code:file = fopen (filename, "rb");
Thanks?
This is a discussion on What's the equivalent of fopen(..) in API's CreateFile within the Windows Programming forums, part of the Platform Specific Boards category; I have this code in C: Code: file = fopen (filename, "rb"); What would be the euivalent in Win32 API? ...
I have this code in C:
What would be the euivalent in Win32 API?Code:file = fopen (filename, "rb");
Thanks?
* Debian 6.0.1 on Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM.
* lighttpd, php5, perl, eruby, python.
* geany, XHTML & CSS & JavaScript, C, C++.
* GTK+ C & perl-gtk2
The binary is default, so you need to open in read mode. Pass GENERIC_READ for dwDesiredAccess, FILE_SHARE_READ for dwShareMode, OPEN_EXISTING for dwCreationDisposition and 0 for dwFlagsAndAttributes.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
FYI - The Platform SDK contains the MSCRT 6.0 source code (with bug fixes and 64bit support).
So you could look at the source for fopen() and see how it uses CreateFile() etc.
gg
You could - but the CRT source is rather horrible.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Yeah, I noticed that is default binary...thanks to all
I have another question, this is the C-style code:
Now, when I ported to APICode:for (i = 0; i < 16; i++) { printf("%02x", digest[i]); // this returns something like 0a44a522a8349e6ed4fd1b71e8745759 }
Any ideasCode:for (i = 0; i < 16; i++, szBuff++) { wsprintf(szBuff, "%02x", digest[i]); // this returns something like 04a2a396df17e7559 } szBuff = '\0';![]()
Last edited by Joelito; 11-27-2007 at 07:21 PM. Reason: New question
* Debian 6.0.1 on Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM.
* lighttpd, php5, perl, eruby, python.
* geany, XHTML & CSS & JavaScript, C, C++.
* GTK+ C & perl-gtk2
Since you seem to have only gotten the MSB of each hex digit pair, I'd say you've probably messed up the types somewhere.
Try posting more code next time, at least enough for us to establish some context for the question.
The last line of your 'new' code makes no sense at all.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
Ok, I think I'm understanding:
Now, displays correctly the hashed string, but after that it crashes... ???Code:void MDPrint (unsigned char digest[16], char *szBuff) { unsigned int i; for (i = 0; i < 16; i++, szBuff++) { wsprintf(szBuff++, "%02x", digest[i]); } //szBuff = '\0'; } // char dump[17]; MD5File("dummy.txt", dump); MessageBox(hWnd, dump, 0, 0);
* Debian 6.0.1 on Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM.
* lighttpd, php5, perl, eruby, python.
* geany, XHTML & CSS & JavaScript, C, C++.
* GTK+ C & perl-gtk2
You need at least 33 bytes to cover a 16 byte string as hex - 16 * 2 bytes for the actual hex characters, and one for the NUL indicating end of string.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
God, that loop is awful! Why use such stupid tricks? Just increment the stupid thing by two, once.
It crashes after displaying the string? Then why don't you show us the code that comes after?
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Is this clearer?
What are you passing to this function, as szBuff ?Code:void MDPrint (unsigned char digest[16], char *szBuff) { unsigned int i; for (i = 0; i < 16; i++) { wsprintf( &szBuff[i*2], "%02x", digest[i]); } szBuff[i*2] = '\0'; }
Is it 33 wchar_t's ?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
First, the code ain't mine
2nd, I don't know that much of C to API
@Salem: Thanks
Final code:
Code:void MDPrint (unsigned char digest[16], char *szBuff) { unsigned int i; for (i = 0; i < 16; i++) { wsprintf(&szBuff[i*2], "%02x", digest[i]); } szBuff[i*2] = '\0'; } int MD5File(const char *pszFilename, char *szBuff) { MD5_CTX context = {0}; HANDLE hFile; int len; unsigned char buffer[1024], digest[16]; hFile = CreateFile(pszFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) return 0; MD5Init(&context); while(len) { ReadFile(hFile, buffer, 1, &len, NULL); MD5Update (&context, buffer, len); } MD5Final(digest, &context); CloseHandle(hFile); MDPrint(digest, szBuff); return 1; } int main(int argc, char *argv[]) { char dump[128]; MD5File(argv[1], dump); MessageBox(0, dump, 0, 0); return 0; }
* Debian 6.0.1 on Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM.
* lighttpd, php5, perl, eruby, python.
* geany, XHTML & CSS & JavaScript, C, C++.
* GTK+ C & perl-gtk2
Even easier, don't think about the length at all:
Now the loop is immune to any changes in the actual format string.Code:for (i = 0; i < 16; i++) { szBuff += wsprintf(szBuff, "%02x", digest[i]); } szBuff = '\0'; /* This isn't necessary, because wsprintf() already makes sure it's there */