What's the equivalent of fopen(..) in API's CreateFile

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? ...

  1. #1
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    307

    Smile What's the equivalent of fopen(..) in API's CreateFile

    I have this code in C:
    Code:
    file = fopen (filename, "rb");
    What would be the euivalent in Win32 API?
    Thanks?
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    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

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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

  5. #5
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    307
    Yeah, I noticed that is default binary...thanks to all

    I have another question, this is the C-style code:
    Code:
    for (i = 0; i < 16; i++) {
         printf("&#37;02x", digest[i]); 
         // this returns something like 0a44a522a8349e6ed4fd1b71e8745759
    }
    Now, when I ported to API
    Code:
    for (i = 0; i < 16; i++, szBuff++) {
         wsprintf(szBuff, "%02x", digest[i]);
         // this returns something like 04a2a396df17e7559
    }
    szBuff = '\0';
    Any ideas
    Last edited by Joelito; 11-27-2007 at 07:21 PM. Reason: New question
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    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.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Joelito View Post
    Yeah, I noticed that is default binary...thanks to all

    I have another question, this is the C-style code:
    Code:
    for (i = 0; i < 16; i++) {
         printf("%02x", digest[i]); 
         // this returns something like 0a44a522a8349e6ed4fd1b71e8745759
    }
    Now, when I ported to API
    Code:
    for (i = 0; i < 16; i++, szBuff++) {
         wsprintf(szBuff, "%02x", digest[i]);
         // this returns something like 04a2a396df17e7559
    }
    szBuff = '\0';
    Any ideas
    Maybe you should increment szBuff by two, since each hex number is two digits long?

    --
    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.

  8. #8
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    307
    Ok, I think I'm understanding:
    Code:
    void MDPrint (unsigned char digest[16], char *szBuff) {
    	unsigned int i;
    	for (i = 0; i < 16; i++, szBuff++) {
    		wsprintf(szBuff++, "&#37;02x", digest[i]);
    	}
    	//szBuff = '\0';
    }
    
    //
    char dump[17];
    MD5File("dummy.txt", dump);
    MessageBox(hWnd, dump, 0, 0);
    Now, displays correctly the hashed string, but after that it crashes... ???
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Is this clearer?
    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';
    }
    What are you passing to this function, as szBuff ?
    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.

  12. #12
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    307
    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], "&#37;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;
    }
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,159
    Quote Originally Posted by matsp View Post
    Maybe you should increment szBuff by two, since each hex number is two digits long?
    Even easier, don't think about the length at all:

    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 */
    Now the loop is immune to any changes in the actual format string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  2. Pointer equivalent to array notation
    By bekkilyn in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 07:22 PM
  3. Replies: 10
    Last Post: 08-17-2005, 11:17 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  5. fopen and CreateFile
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 04-03-2003, 06:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21