C#调用c++写的dll的问题 我根据网上搜索到的文章的写法写了个demo老是弹出如下东东,我用的是VS2008 知道问题的朋友告诉我下,谢谢Additional information: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你的调用格式对吗,分清stdcall和cdll DLL 是否加入了 CLR 支持?DLL 导出的函数是否有问题? 你是怎么调用的?那个dll是什么类型的? #include "stdafx.h"int __stdcall Add(int numa, int numb){ return (numa + numb);}int __stdcall Sub(int numa, int numb){ return (numa - numb);}---defLIBRARY "MyDll"EXPORTSAdd @ 1Sub @ 2using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;namespace DotNetTest{ class CMyDll { [DllImport("MyDll", EntryPoint = "Add", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)] public static extern int Add(int a,int b); }}dll是Unicode的 C++不是很熟悉,编译的时候可以选择CPU吗? 我这个程序编译好以后需要放到32位的机器上去用的,C#是需要选any cpu的c++ dll我是选X86的 我觉得原因是,你的C#项目选择的Any CPU,所以在64bit系统上运行是64bit的。但是你的Dll是32bit的所以无法载入。你可以试一下,把你的C#设置为32bit运行,看看是否能正常执行。如果可以的话,那么就是这个原因了。如果你的C#必须选择Any CPU的话,那么你就必须同时提供32bit和64bit的Dll,在不同的系统中,载入相应的Dll来解决。 一个获取图片缩略图的源码 原 #include "stdafx.h"#include <GdiPlus.h>using namespace Gdiplus;#pragma comment(lib,"gdiplus.lib ")extern "C" _declspec (dllexport) HBITMAP fnGetBitMap(UINT zoom,LPCSTR strPath){ USES_CONVERSION; HBITMAP hbmp=NULL; Bitmap bmp(A2W(strPath)); UINT width=0; UINT height=0; UINT w= bmp.GetWidth(); UINT h=bmp.GetHeight(); width =(UINT)( w /zoom); height =(UINT)(h/zoom); Bitmap * img=static_cast<Bitmap *>(bmp.GetThumbnailImage(width,height,NULL,NULL)); img->GetHBITMAP(NULL,&hbmp); return hbmp;}extern "C" _declspec (dllexport) HBITMAP fnGetBitMap2(UINT width,UINT height,LPCSTR strPath){ USES_CONVERSION; HBITMAP hbmp=NULL; Bitmap bmp(A2W(strPath)); Bitmap * img=static_cast<Bitmap *>(bmp.GetThumbnailImage(width,height,NULL,NULL)); img->GetHBITMAP(NULL,&hbmp); return hbmp;}extern "C" _declspec (dllexport) HBITMAP fnGetBitMap3(UINT zoom,LPCSTR strPath, int *wg, int *he){ USES_CONVERSION; HBITMAP hbmp=NULL; Bitmap bmp(A2W(strPath)); UINT width=0; UINT height=0; UINT w= bmp.GetWidth(); UINT h=bmp.GetHeight(); //CString cs; //CString cs1; //CString cs2; //cs.Format(_T("cs w=%d"),w); //AfxMessageBox(cs); int ww=(int) w; int hh=(int) h; //cs1.Format(_T("cs1 w=%d"),ww); //AfxMessageBox(cs1); *he=hh; *wg=ww; //cs2.Format(_T(" cs2 w=%d"),*wg); //AfxMessageBox(cs2); width =(UINT)( w /zoom); height =(UINT)(h/zoom); Bitmap * img=static_cast<Bitmap *>(bmp.GetThumbnailImage(width,height,NULL,NULL)); img->GetHBITMAP(NULL,&hbmp); return hbmp;}extern "C" _declspec (dllexport) HBITMAP fnGetBitMap4(UINT width,UINT height,LPCSTR strPath,int * w,OUT int * h){ USES_CONVERSION; HBITMAP hbmp=NULL; Bitmap bmp(A2W(strPath)); *w=bmp.GetWidth(); *h=bmp.GetHeight(); Bitmap * img=static_cast<Bitmap *>(bmp.GetThumbnailImage(width,height,NULL,NULL)); img->GetHBITMAP(NULL,&hbmp); return hbmp;} using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;namespace Diaw{ public class clsApi { [DllImport("ImagePlusDll.dll")] public static extern IntPtr fnGetBitMap(int zoom, System.Text.StringBuilder strPath); [DllImport("ImagePlusDll.dll")] public static extern IntPtr fnGetBitMap2(int width,int height, System.Text.StringBuilder strPath); [DllImport("ImagePlusDll.dll")] public static extern IntPtr fnGetBitMap3(int zoom, System.Text.StringBuilder strPath, ref int he, ref int wg); [DllImport("ImagePlusDll.dll")] public static extern IntPtr fnGetBitMap4(int width, int height, System.Text.StringBuilder strPath, ref int w, ref int h); }}下载地址:http://download.csdn.net/source/2612525vc写的一个dll编译下放在debug目录下就可以了。对于大图片效果非常不错。按这个例子你也可以用vc写一些dll给c#用。 下载地址:http://files.cnblogs.com/yufb/ImagePlusDll.rar很久之前写的。 我刚说漏了几个字,是把你的C#项目设置为x86模式执行一次看看是不是能够正常执行,如果可以的话,那么就是因为64bit进程载入32bit DLL失败。我看你前面的描述应该是知道怎么改的。在菜单里面“项目-》项目属性-》Build-》Platform Target”的值从Any CPU改为x86,然后编译执行试试呢。 改成x86 现在是可以了,但是我把exe 和相关dll移到32位的机器上去,一运行就报错了 ,该怎么弄呢 需求就是我编写的代码在32位的机器上面一定要可以运行的之前.net 的cpu是选 any的话,在32位机器上是可以跑的 或者可以这样问就是64位windows7系统在.net cpu是any的情况下,如何调用c++写的dll可以成功调用 你看下32bit系统里面报的异常到底是什么。按理说你修改后在32和64位系统都可以运行的。如果你把项目的平台设置为Any CPU的话,那么你就必须编译32bit和64bit的DLL。然后在程序里面判断当前是32bit还是64bit运行,再载入相应的DLL。 那那个DLL估计也得是64的,不染偶了 谢谢大家问题找到了昨天折腾了一天结贴了---引蜘入网,准备结贴赛特维维面对面迪迪微123免费信息日日日123发发发123牛牛牛123啪啪123耶耶耶123有事互联同城123沙特123PDA123VVV123同区123 求助大神,字符串删除的问题 随机时间如何获取? Xml文件被使用 string 和 Hex之间的转换 textbox如何控制禁止输入半角,只允许输入全角~! C#滚动条 我要启多个Windows service为什么每次总是只能启动第一个Service? 关于 RichTextBox 控件 急用 为什么得不到当前值呢 求救 急!!我的VS.net帮助不见了 vs2010中C#能开发ActiveX吗? tabpage控件怎么让其他的都禁用
那个dll是什么类型的?
#include "stdafx.h"int __stdcall Add(int numa, int numb)
{
return (numa + numb);
}int __stdcall Sub(int numa, int numb)
{
return (numa - numb);
}
---def
LIBRARY "MyDll"
EXPORTS
Add @ 1
Sub @ 2using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace DotNetTest
{
class CMyDll
{
[DllImport("MyDll", EntryPoint = "Add", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
public static extern int Add(int a,int b);
}
}dll是Unicode的
C++不是很熟悉,编译的时候可以选择CPU吗?
#include <GdiPlus.h>
using namespace Gdiplus;
#pragma comment(lib,"gdiplus.lib ")
extern "C" _declspec (dllexport) HBITMAP fnGetBitMap(UINT zoom,LPCSTR strPath)
{
USES_CONVERSION;
HBITMAP hbmp=NULL;
Bitmap bmp(A2W(strPath));
UINT width=0;
UINT height=0;
UINT w= bmp.GetWidth();
UINT h=bmp.GetHeight();
width =(UINT)( w /zoom);
height =(UINT)(h/zoom);
Bitmap * img=static_cast<Bitmap *>(bmp.GetThumbnailImage(width,height,NULL,NULL));
img->GetHBITMAP(NULL,&hbmp);
return hbmp;
}extern "C" _declspec (dllexport) HBITMAP fnGetBitMap2(UINT width,UINT height,LPCSTR strPath)
{
USES_CONVERSION;
HBITMAP hbmp=NULL;
Bitmap bmp(A2W(strPath)); Bitmap * img=static_cast<Bitmap *>(bmp.GetThumbnailImage(width,height,NULL,NULL));
img->GetHBITMAP(NULL,&hbmp);
return hbmp;
}
extern "C" _declspec (dllexport) HBITMAP fnGetBitMap3(UINT zoom,LPCSTR strPath, int *wg, int *he)
{
USES_CONVERSION;
HBITMAP hbmp=NULL;
Bitmap bmp(A2W(strPath));
UINT width=0;
UINT height=0; UINT w= bmp.GetWidth();
UINT h=bmp.GetHeight();
//CString cs;
//CString cs1;
//CString cs2;
//cs.Format(_T("cs w=%d"),w);
//AfxMessageBox(cs);
int ww=(int) w;
int hh=(int) h;
//cs1.Format(_T("cs1 w=%d"),ww);
//AfxMessageBox(cs1);
*he=hh;
*wg=ww;
//cs2.Format(_T(" cs2 w=%d"),*wg);
//AfxMessageBox(cs2); width =(UINT)( w /zoom);
height =(UINT)(h/zoom);
Bitmap * img=static_cast<Bitmap *>(bmp.GetThumbnailImage(width,height,NULL,NULL));
img->GetHBITMAP(NULL,&hbmp);
return hbmp;
}
extern "C" _declspec (dllexport) HBITMAP fnGetBitMap4(UINT width,UINT height,LPCSTR strPath,int * w,OUT int * h)
{
USES_CONVERSION;
HBITMAP hbmp=NULL;
Bitmap bmp(A2W(strPath));
*w=bmp.GetWidth();
*h=bmp.GetHeight();
Bitmap * img=static_cast<Bitmap *>(bmp.GetThumbnailImage(width,height,NULL,NULL));
img->GetHBITMAP(NULL,&hbmp);
return hbmp;
} using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;namespace Diaw
{
public class clsApi
{
[DllImport("ImagePlusDll.dll")]
public static extern IntPtr fnGetBitMap(int zoom, System.Text.StringBuilder strPath); [DllImport("ImagePlusDll.dll")]
public static extern IntPtr fnGetBitMap2(int width,int height, System.Text.StringBuilder strPath); [DllImport("ImagePlusDll.dll")]
public static extern IntPtr fnGetBitMap3(int zoom, System.Text.StringBuilder strPath, ref int he, ref int wg); [DllImport("ImagePlusDll.dll")]
public static extern IntPtr fnGetBitMap4(int width, int height, System.Text.StringBuilder strPath, ref int w, ref int h); }
}
下载地址:http://download.csdn.net/source/2612525vc写的一个dll
编译下放在debug目录下就可以了。
对于大图片效果非常不错。
按这个例子你也可以用vc写一些dll给c#用。 下载地址:http://files.cnblogs.com/yufb/ImagePlusDll.rar
很久之前写的。
改成x86 现在是可以了,但是我把exe 和相关dll移到32位的机器上去,一运行就报错了 ,该怎么弄呢
需求
就是我编写的代码在32位的机器上面一定要可以运行的之前.net 的cpu是选 any的话,在32位机器上是可以跑的
昨天折腾了一天
结贴了
---引蜘入网,准备结贴
赛特维维
面对面
迪迪微123
免费信息
日日日123
发发发123
牛牛牛123
啪啪123
耶耶耶123
有事互联
同城123
沙特123
PDA123
VVV123
同区123