如何在Winapi中运行使用InternetOpenFile()下载的.exe文件

时间:2017-12-06 21:23:50

标签: c windows winapi handle

我有一个使用Winapi写的C应用程序和一个我需要下载和执行的.exe文件。到目前为止我的代码是:

if (NULL == (hRequest = HttpOpenRequest(hHTTP, "GET", "/~alexandru.antochi/exe_1.exe", NULL, NULL, rgpszAcceptTypes, NULL, INTERNET_FLAG_NO_COOKIES || INTERNET_FLAG_NO_AUTH)))
        {
            _error("HttpOpenRequest error.");
        }

        if (HttpSendRequest(hRequest, NULL, NULL, NULL, NULL))
        {

            if (InternetReadFile(hRequest, &buffer, 65536, &bytesRead))
            {
                if (bytesRead == 65536)
                {
                    printf("Warning: .exe file too big. Ignoring");
                    continue;
                }
            }
        }
        else
        {
            _error("Could not send HTTP request.");
        }

        closeHandles(2, hRequest, hHTTP);

我读了这个文件,现在怎么样?如果我尝试在本地文件中写入它,它将停在第一个\0分隔符,在我的情况下是2个字母之后。该文件位于http://students.info.uaic.ro/~alexandru.antochi/exe_1.exe

1 个答案:

答案 0 :(得分:2)

您必须先将EXE文件保存到本地文件,然后才能执行它。您无法从内存中执行EXE(无需编写自己的EXE加载程序或使用第三方加载程序)。

<script> var container; var camera, scene, renderer; var mouseX = 0, mouseY = 0; var windowHalfX = window.innerWidth / 2; var windowHalfY = window.innerHeight / 2; init(); animate(); function init() { container = document.createElement( 'div' ); document.body.appendChild( container ); camera = new THREE.PerspectiveCamera( 5, window.innerWidth / window.innerHeight, 1, 5000 ); camera.position.z = 250; scene = new THREE.Scene(); var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.8 ); scene.add( ambientLight ); var pointLight = new THREE.PointLight( 0xFFF1CF, 0.6 , 0 ); camera.add( pointLight ); scene.add( camera ); var manager = new THREE.LoadingManager(); manager.onProgress = function ( item, loaded, total ) { console.log( item, loaded, total ); }; var onProgress = function ( xhr ) { if ( xhr.lengthComputable ) { var percentComplete = xhr.loaded / xhr.total * 100; console.log( Math.round(percentComplete, 2) + '% downloaded' ); } }; var onError = function ( xhr ) {; }; var mtlLoader = new THREE.MTLLoader(); mtlLoader.load('planet.mtl', function(materials) { materials.preload(); var objLoader = new THREE.OBJLoader(); objLoader.setMaterials(materials); objLoader.load('planet.obj', function(object) { object.position.y = 0; scene.add(object); }, onProgress, onError); }); // renderer = new THREE.WebGLRenderer( {alpha: true}); renderer.setClearColor( 0x000000, 0 ); renderer.setPixelRatio( window.devicePixelRatio ); renderer.setSize( window.innerWidth, window.innerHeight ); container.appendChild( renderer.domElement ); document.addEventListener( 'mousemove', onDocumentMouseMove, false ); document.addEventListener( 'mouseclick', onmousedown, false); // window.addEventListener( 'resize', onWindowResize, false ); } function onWindowResize() { windowHalfX = window.innerWidth / 2; windowHalfY = window.innerHeight / 2; camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); renderer.setSize( window.innerWidth, window.innerHeight ); } function onDocumentMouseMove( event ) { mouseX = ( event.clientX ); mouseY = ( event.clientY ); } // function animate() { requestAnimationFrame( animate ); render(); } function render() { camera.position.x += ( mouseX - camera.position.x ) * .05; camera.position.y += ( - mouseY - camera.position.y ) * .05; camera.lookAt( scene.position ); renderer.render( scene, camera ); } </script> 读取任意数量的数据,因此您需要在循环中调用它,直到达到响应结束。将按原样接收的每个数据块写入本地文件。您声称您的写作“将停在第一个InternetReadFile()分隔符”的声明意味着您将接收的数据写为以空字符结尾的字符串而不是原始二进制数据。二进制文件,尤其是可执行文件,其中包含大量0x00字节。因此,这是您需要修复的代码中的逻辑错误。

尝试更像这样的东西:

\0

有关详细信息,请参阅MSDN文档:

HTTP Sessions

Downloading Resources from the WWW