Jump to content
  • Advertisement
Sign in to follow this  
sir_wojciech

C# + DirectX Problem

This topic is 4493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When I try to compile any application in c# with directx I get an BadImageFormatException (is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)). I tested even on the tutorials they gave in dx june 2006 sdk for c#. Do you know what's wrong? Here's 1 tutorial from the sdk:
//-----------------------------------------------------------------------------
// File: CreateDevice.cs
//
// Desc: This is the first tutorial for using Direct3D. In this tutorial, all
//       we are doing is creating a Direct3D device and using it to clear the
//       window.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

public class CreateDevice : Form
{
    // Our global variables for this project
    Device device = null; // Our rendering device

    public CreateDevice()
    {
        // Set the initial size of our form
        this.ClientSize = new System.Drawing.Size(400, 300);
        // And it's caption
        this.Text = "D3D Tutorial 01: CreateDevice";
    }

    public bool InitializeGraphics()
    {
        try
        {
            // Now let's setup our D3D stuff
            PresentParameters presentParams = new PresentParameters();
            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;
            device = new Device(0, DeviceType.Hardware, this,
CreateFlags.SoftwareVertexProcessing, presentParams);
            return true;
        }
        catch (DirectXException)
        {
            return false;
        }
    }
    private void Render()
    {
        if (device == null)
            return;

        //Clear the backbuffer to a blue color 
        device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
        //Begin the scene
        device.BeginScene();

        // Rendering of scene objects can happen here

        //End the scene
        device.EndScene();
        device.Present();
    }
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        this.Render(); // Render on painting
    }
    protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
    {
        if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
            this.Close(); // Esc was pressed
    }

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {

        CreateDevice frm = new CreateDevice(); //<------ Exception Comes Here
            if (!frm.InitializeGraphics()) // Initialize Direct3D
            {
                MessageBox.Show("Could not initialize Direct3D.  This tutorial will exit.");
                return;
            }
            frm.Show();

            // While the form is still valid, render and process messages
            while (frm.Created)
            {
                frm.Render();
                Application.DoEvents();
            }
       
    }
}

Thanks in advance! [Edited by - sir_wojciech on June 27, 2006 12:51:08 PM]

Share this post


Link to post
Share on other sites
Advertisement
I think I solved this. Since I was using Visual C# 2005 EE I couldn't change the "Target CPU" so it compiled as "Any CPU" and that's why it wasn't working. When I changed the IDE to SharpDevelop and set the Target CPU to 32-bit everything compiled and ran well. Thanks for your tip JM.

Share this post


Link to post
Share on other sites
Yeah I ran into the same problem with C#/VS2005 and win64.
If you don't change your target from anycpu you won't get a running executable.
Never figured out why?

Share this post


Link to post
Share on other sites
I checked the comparison table between the different Visual Studio 2005 editions and it looks like the Express Editions do not contain 64bit compilers.
Have a look here

So unless you're willing to buy another edition of VS you'll have to stay with SharpDevelop. (Which probably isn't that bad. Don't know. Haven't tried it out yet)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!