saucer(思归) 关于C++生成Dll的问题

bijiniye1988 2009-06-30 02:32:30
Hi saucer:
您好,我现在遇到这么一个问题,不知道如何解决。
我搞好了一个C++的应用程序,类似于WinForm那种的窗口应用程序,现在要把它转为DLL在C#中调用,可是却不知道如何将其生成为一个DLL出来。我在网上也查看了一些资料,但是还是不明白,毕竟自己对C++不是很清楚,所以就来CSDN上问问,希望能够帮助一下我,我快被逼疯了…………



P.S:各位Csdner们,同样拜托你们哦~!~!~!



不好意思,我不知道如何发帖,才算是向专家提问....
...全文
192 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
bijiniye1988 2009-10-28
  • 打赏
  • 举报
回复
不好意思,好长时间没来,我先把帖子结了吧。。
bijiniye1988 2009-07-01
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 jintianfree 的回复:]
把具体的C++项目说说吧

还有你在转为DLL  在C#中不能正确执行时遇到的具体问题 说说吧
[/Quote]

。。。我目前没有碰到在C#如法正常调用的情况,因为dll还没有生成出来。。。
bijiniye1988 2009-07-01
  • 打赏
  • 举报
回复
up
jintianfree 2009-06-30
  • 打赏
  • 举报
回复
把具体的C++项目说说吧

还有你在转为DLL 在C#中不能正确执行时遇到的具体问题 说说吧
破碎的脸 2009-06-30
  • 打赏
  • 举报
回复
平台调用
破碎的脸 2009-06-30
  • 打赏
  • 举报
回复
这种事何必麻烦大虾。。。。
很简单,四种方法都可以,但首先你必须生成这个dll。
第一种,前期绑定
第二种,后期绑定
第三种,Com呃。。是COM还是Active?反正会做但不知道叫什么
第四种,这也是最强大的一种。。。。用C#再写一个- -!!!
cnzdgs 2009-06-30
  • 打赏
  • 举报
回复
这要看你的C++程序是用什么开发的,用的哪种项目模板。例如用VS.NET开发的基于对话框的MFC程序,可以在项目属性中将配置类型改为动态库,将App类InitInstance函数中创建对话框的相关代码删除,例如增加一个导出函数来创建和显示对话框。C#中用DllImport声明extern函数。
bijiniye1988 2009-06-30
  • 打赏
  • 举报
回复
up
bijiniye1988 2009-06-30
  • 打赏
  • 举报
回复
up
LQknife 2009-06-30
  • 打赏
  • 举报
回复
就是来看思归的
wangan2008 2009-06-30
  • 打赏
  • 举报
回复
up
bijiniye1988 2009-06-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hdt 的回复:]
如果c++是托管的,和c#没有太大区别,
如果是非托管的 ,建立一个win32 dll 工程
不过如果你不懂c++,会有不小的麻烦

[/Quote]


是非托管的,麻烦点儿无所谓,主要是能够实现就行,能够细说一下吗?

还有,C++已经写好的程序也可以改为托管的吗?
qqiuzaihui 2009-06-30
  • 打赏
  • 举报
回复
在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);

private static Delegate GetAddress(int dllModule, string functionname, Type t)
{
int addr = GetProcAddress(dllModule, functionname);
if (addr == 0)
return null;
else
return Marshal.GetDelegateForFunctionPointer(new IntPtr(addr), t);
}
public delegate void ShowDlg(); //委托

//调用Dll的代码
int huser32 = 0;
huser32 = LoadLibrary("你的动态库.dll");
ShowDlg show = (ShowDlg)GetAddress(huser32, "ShowDlg", typeof(ShowDlg));
show(); //调用C++中动态库函数
FreeLibrary( huser32 );

以上代码在VS2005中测试通过。

其它DLL相关问题, 参考:
http://topic.csdn.net/u/20090604/11/7c066445-732e-45f5-a947-b13ce31f1390.html 第1楼的转贴。
qqiuzaihui 2009-06-30
  • 打赏
  • 举报
回复
VC6.0 下:

1. 新建 MFC AppWizard 工程, 取名如: SS

2. Insert -> New Form 取名如: QQ

3. 在 ss.cpp 中引用 #include "qq.h" 并编写 ShowDlg() 函数.
ss.cpp 中所有代码如下:

// ss.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "ss.h"
#include "qq.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BEGIN_MESSAGE_MAP(CSsApp, CWinApp)
//{{AFX_MSG_MAP(CSsApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


CSsApp::CSsApp()
{
}

CSsApp theApp;

void ShowDlg( )
{
qq qqdlg;

AFX_MANAGE_STATE(AfxGetStaticModuleState()); //自动切换当前模块状态
qqdlg.DoModal();
return;
}


4. 在 ss.def 的 EXPORTS 下导出 ShowDlg 函数, 如:
EXPORTS
; Explicit exports can go here
ShowDlg

5. 完成.
ximi82878 2009-06-30
  • 打赏
  • 举报
回复
貌似 saucer(思归) N久都不来了~~~帮你顶吧~~~
whyabc 2009-06-30
  • 打赏
  • 举报
回复
up
真相重于对错 2009-06-30
  • 打赏
  • 举报
回复
如果c++是托管的,和c#没有太大区别,
如果是非托管的 ,建立一个win32 dll 工程
不过如果你不懂c++,会有不小的麻烦
bijiniye1988 2009-06-30
  • 打赏
  • 举报
回复
up
bijiniye1988 2009-06-30
  • 打赏
  • 举报
回复
up

110,533

社区成员

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

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

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