现在刚用到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)请各位帮帮忙啊,谢谢了!!
先自己构造一个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)请各位帮帮忙啊,谢谢了!!
解决方案 »
- checkbox为什么没有 readonly属性
- php代码转 c#
- 读取XML
- VS2005里面通过xsd设计时,连接不上MySQL
- C#GDI+画png图片
- 如何是好(把服务器A上一个目录下的一个文件 移动到 服务器B上的某个目录下(此目录都有权限读写的))
- 高分求代码A文件夹和B文件夹比对
- 数据库中为每个用户给不同的权限后,我想问:用户登陆成功后如何验证权限?难道是形成一个用户名ID的SESSION ?
- 用C#做了一个仿Messenger弹出信息提示的float window,问题是Msn的弹出窗体并不抢占焦点,而用c#写的一弹出,就抢占了焦点?
- 如何用C#编个应用程式能够象IE一样浏览指定的网址
- 怎么输入这些数字
- C#版 斗地主源代码
[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);
}
}
}
{
__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();
}你那是什么问题...
未处理BadImageFormatException
试图加载格式不正确的程序。(异常来自HRESULT:0X8007000B)
DLLNotFoundException的异常。
无法加载DLL “Test4.dll“:找不到指定的模块。