cwindowsgcclibjpegpyd

Compiling pyd file on windows but "This program cannot be run in DOS mode."


I was compiling a C file with gcc on Windows and got pyd file successfully. To my surprise, it shows "This program cannot be run in DOS mode" in hex. Although I still can call the function from it, the program crashed soon, caused by jpeg_read_header() from libjpeg library.

My question is what on earth made my program crashed.

Here are my guesses:

  1. jpeg_read_header() : I tried both jpeg_mem_src() and jpeg_stdio_src() but it still crashed.

    int _read_dct_coefficients(FILE* input_file, int** all_dcts)
    {   
    JDIMENSION i, compnum, rownum, blocknum;
    JBLOCKARRAY row_ptrs[MAX_COMPONENTS];
    size_t block_row_size;
    int num_blocks = 0, cnt = 0;
    
    #ifdef LOG_DEBUG
    log_debug(__LINE__, "enter _read_dct_coefficients");
    #endif
    
    /* init decompression */
    srcinfo.err = jpeg_std_error(&jsrcerr);
    jpeg_create_decompress(&srcinfo);
    
    /* init compression */
    dstinfo.err = jpeg_std_error(&jdsterr);
    jpeg_create_compress(&dstinfo);
    
    jsrcerr.trace_level = jdsterr.trace_level;
    srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use;
    
    #ifdef LOG_DEBUG
    log_debug(__LINE__, "%%%%%%%MY TEST # 1%%%%%%%%");
    #endif  
    
    //***************************************************************
    
    unsigned int get_file_size(FILE *fp)  
    {  
        unsigned long filesize = -1;  
        if(fp == NULL)
            return filesize;
        fseek(fp, 0L, SEEK_END);
        filesize = ftell(fp);
        fclose(fp);
        return filesize;
    }
    
    int size = get_file_size(input_file);
    #ifdef LOG_DEBUG
    log_debug(__LINE__, "file size = %d", size);
    #endif
    char *tmp_buf = (unsigned char *)malloc(sizeof(char) * size);
    if (size != fread(tmp_buf, 1, size, input_file))
        log_debug(__LINE__, "cannot open.");
    
    jpeg_mem_src(&srcinfo, tmp_buf, size);
    /*
    jpeg_stdio_src(&srcinfo, input_file);
    */
    #ifdef LOG_DEBUG
    log_debug(__LINE__, "%%%%%%%MY TEST # 2%%%%%%%%");
    #endif  
    jpeg_read_header(&srcinfo, TRUE);
    
    ......
    
    }
    
  2. Pyd file : It cannot be run in DOS mode?


Solution

  • A Python .pyd file is just a DLL, which is just a Windows PE file. Windows PE files by convention start with a stub that prints that message if you run them in DOS:

    https://en.wikipedia.org/wiki/Portable_Executable#History

    Pretty much every Windows EXE and DLL file contains this header; it doesn't imply anything special.