C#调用c++写的dll的问题

我看你有戏 2010-11-21 01:33:55
我根据网上搜索到的文章的写法

写了个demo老是弹出如下东东,我用的是VS2008 知道问题的朋友告诉我下,谢谢

Additional information: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)
...全文
437 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyerwing 2010-11-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 henry3695 的回复:]
对了我的系统是64位的
[/Quote]
那那个DLL估计也得是64的,不染偶了
voodoo82 2010-11-22
  • 打赏
  • 举报
回复
你看下32bit系统里面报的异常到底是什么。按理说你修改后在32和64位系统都可以运行的。

如果你把项目的平台设置为Any CPU的话,那么你就必须编译32bit和64bit的DLL。然后在程序里面判断当前是32bit还是64bit运行,再载入相应的DLL。
我看你有戏 2010-11-22
  • 打赏
  • 举报
回复
谢谢大家问题找到了
昨天折腾了一天
结贴了
---引蜘入网,准备结贴
赛特维维
面对面
迪迪微123
免费信息
日日日123
发发发123
牛牛牛123
啪啪123
耶耶耶123
有事互联
同城123
沙特123
PDA123
VVV123
同区123
我看你有戏 2010-11-21
  • 打赏
  • 举报
回复
或者可以这样问

就是64位windows7系统在.net cpu是any的情况下,如何调用c++写的dll可以成功调用
我看你有戏 2010-11-21
  • 打赏
  • 举报
回复

需求
就是我编写的代码在32位的机器上面一定要可以运行的

之前.net 的cpu是选 any的话,在32位机器上是可以跑的
我看你有戏 2010-11-21
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 voodoo82 的回复:]

我刚说漏了几个字,是把你的C#项目设置为x86模式执行一次看看是不是能够正常执行,如果可以的话,那么就是因为64bit进程载入32bit DLL失败。我看你前面的描述应该是知道怎么改的。

在菜单里面“项目-》项目属性-》Build-》Platform Target”的值从Any CPU改为x86,然后编译执行试试呢。
[/Quote]

改成x86 现在是可以了,但是我把exe 和相关dll移到32位的机器上去,一运行就报错了 ,该怎么弄呢
voodoo82 2010-11-21
  • 打赏
  • 举报
回复
我刚说漏了几个字,是把你的C#项目设置为x86模式执行一次看看是不是能够正常执行,如果可以的话,那么就是因为64bit进程载入32bit DLL失败。我看你前面的描述应该是知道怎么改的。

在菜单里面“项目-》项目属性-》Build-》Platform Target”的值从Any CPU改为x86,然后编译执行试试呢。
我看你有戏 2010-11-21
  • 打赏
  • 举报
回复
现在碰到的是系统兼容性的问题
足球中国 2010-11-21
  • 打赏
  • 举报
回复
一个获取图片缩略图的源码 原 #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/2612525

vc写的一个dll
编译下放在debug目录下就可以了。
对于大图片效果非常不错。


按这个例子你也可以用vc写一些dll给c#用。



下载地址:http://files.cnblogs.com/yufb/ImagePlusDll.rar


很久之前写的。
我看你有戏 2010-11-21
  • 打赏
  • 举报
回复
c#设置32位怎么弄的啊
voodoo82 2010-11-21
  • 打赏
  • 举报
回复
我觉得原因是,你的C#项目选择的Any CPU,所以在64bit系统上运行是64bit的。但是你的Dll是32bit的所以无法载入。

你可以试一下,把你的C#设置为32bit运行,看看是否能正常执行。如果可以的话,那么就是这个原因了。

如果你的C#必须选择Any CPU的话,那么你就必须同时提供32bit和64bit的Dll,在不同的系统中,载入相应的Dll来解决。
我看你有戏 2010-11-21
  • 打赏
  • 举报
回复
在吗

高手教教我呢
我看你有戏 2010-11-21
  • 打赏
  • 举报
回复
我这个程序编译好以后需要放到32位的机器上去用的,C#是需要选any cpu的

c++ dll我是选X86的
机器人 2010-11-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 henry3695 的回复:]
对了我的系统是64位的
[/Quote]

C++不是很熟悉,编译的时候可以选择CPU吗?
我看你有戏 2010-11-21
  • 打赏
  • 举报
回复

#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 @ 2



using 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的
whycom 2010-11-21
  • 打赏
  • 举报
回复
你是怎么调用的?
那个dll是什么类型的?
我看你有戏 2010-11-21
  • 打赏
  • 举报
回复
对了我的系统是64位的
我看你有戏 2010-11-21
  • 打赏
  • 举报
回复
dll 我用def的方式导出的
fox000002 2010-11-21
  • 打赏
  • 举报
回复
DLL 是否加入了 CLR 支持?

DLL 导出的函数是否有问题?
neohope 2010-11-21
  • 打赏
  • 举报
回复
你的调用格式对吗,分清stdcall和cdll

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧