现在刚用到C#来调用DLL,我按照http://www.codeproject.com/KB/cs/usecdlllibincs.aspx#里面的例子一步步的做的,
先自己构造一个DLL,代码如下:
#include <stdio.h>extern "C"
{
  __declspec(dllexport) void DisplayHelloFromDLL()
  {
    printf ("Hello from DLL !\n");
  }
}
接着又创建了一个c#应用程序(名称为HelloWorld)来调用DLL,代码如下:
using System;
using System.Runtime.InteropServices;     // DLL supportclass HelloWorld
{
    [DllImport("TestLib.dll")]
    public static extern void DisplayHelloFromDLL ();    static void Main ()
    {
        Console.WriteLine ("This is C# program");
        DisplayHelloFromDLL ();
    }
}
我把上面程序生成的TestLib.dll放在HelloWorld\bin\Debug目录下面,运行HelloWorld程序
报异常:
    未处理BadImageFormatException
  试图加载格式不正确的程序。(异常来自HRESULT:0X8007000B)请各位帮帮忙啊,谢谢了!!

解决方案 »

  1.   

    看得有点晕,LZ最终调用的是哪个方法?原来的方法管用的话,试试将Dll文件注册一下,或者试试能不能直接引入Dll(如果是.Net Framework生成的Dll)
      

  2.   

    调用的是 DisplayHelloFromDLL ();方法,DLL文件是通过C程序创建的。
      

  3.   

    自己写的DLL有错误,希望高手指点
      

  4.   

    要指写dllimport的entrypoint属性和编码吧?你试试
      

  5.   

    引用: using System.Runtime.InteropServices;
            [DllImport("Kernel32")]                    //读取动态库文件
            public static extern int GetProcAddress(int handle, String funcname);
            [DllImport("Kernel32")]
            public static extern int LoadLibrary(String funcname);
            [DllImport("Kernel32")]
            public static extern int FreeLibrary(int handle);
            [DllImport("HmPark.dll")]                  //声明C++中的接口函数,  其中 HmPark 为你的动态库文件名
            public static extern double GetStdCharge(UInt32 time_In, UInt32 time_Out);        private int huser32 = 0;
            private void CountCharge()
            {
        double dCharge = 0;
                try
                {
                    huser32 = LoadLibrary("HmPark.dll");        //载入动态库
                    dCharge = GetStdCharge(time_In, time_Out); //调用 C++ 中的接口函数
                }
                catch (Exception ex)
                {
                    MessageBox.Show("调用出错: " + ex.Message, "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    try
                    {
                        Inspect.FreeLibrary(huser32);                          //释放动态库文件, 否则会弹出异常
                    }
                    catch (Exception ee)
                    {
                        MessageBox.Show("释放出错: " + ee.Message, "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
      

  6.   

    我试了下,正常#include <stdio.h>extern "C"
    {
      __declspec(dllexport) void DisplayHelloFromDLL()
      {
        printf ("Hello from DLL !\n");
      }
    }[DllImport("TestLib2.dll")]
            public static extern void DisplayHelloFromDLL();
            static void Main(string[] args)
            {
                Console.WriteLine("This is C# program");
                DisplayHelloFromDLL();
                
            }你那是什么问题...
      

  7.   

    我这里报异常: 报异常:
        未处理BadImageFormatException
      试图加载格式不正确的程序。(异常来自HRESULT:0X8007000B) 
      

  8.   

    我是说你可能是把C#程序设置成了编译成x64,而你的C dll是x86的,也可能相反。
      

  9.   

    重新生成 DLL试下,是不是文件损坏了
      

  10.   

    我的不行啊,是不是我生成DLL的方式不对啊。
      

  11.   

    看看你的项目属性里和编译配置里,是x64还是anycpu
      

  12.   

    我把生成的DLL文件直接的拷到了HelloWorld\bin\Debug目录下面,现在又报
    DLLNotFoundException的异常。
    无法加载DLL “Test4.dll“:找不到指定的模块。
      

  13.   

    Dll 名称错了吧,好好检察一下 
      

  14.   

    无法加载DLL “Test4.dll“:找不到指定的模块。路径不对、文件名不对、找不到所需的其他dll