MFC做的dll能不产生lib吗?

Alany 2001-05-21 05:40:00
怎样在vc下做的dll可以摆脱编译产生的lib文件,使其它工具也能调用dll中输出的函数?
要求函数能用export和WINAPI/_stdcall声明
...全文
185 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinaprogrammer 2001-06-08
  • 打赏
  • 举报
回复
我就是依照上述方法用VC和DELPHI做图象和网络通讯项目的,很好啊!
Alany 2001-06-04
  • 打赏
  • 举报
回复
怎么又没人理我了
Alany 2001-06-02
  • 打赏
  • 举报
回复
依照上述方法写的dll,用mfc做的程序中调用它时,::LoadLibrary返回的instance总是0x10000,GetProcAddress就找不到dll输出的函数
chinaprogrammer 2001-05-26
  • 打赏
  • 举报
回复
对这个问题的补充解释如下:

VC的编译器默认情况下采用__cdecl调用约定

extern "C" __declspec(dllexport) int MyDllCall(int n)

extern "C" __declspec(dllexport) int __cdecl MyDllCall(int n)
等效


Delphi 对于参数的调用约定参照如下描述:
The table below summarizes calling conventions.

Directive Parameter order Clean-up Passes parameters in registers?
register Left-to-right Routine Yes
pascal Left-to-right Routine No
cdecl Right-to-left Caller No
stdcall Right-to-left Routine No
safecall Right-to-left Routine No

The default register convention is the most efficient,
since it usually avoids creation of a stack frame.
(Access methods for published properties must use register.)
The cdecl convention is useful when you call functions from DLLs written in C or C++, //注意此行文字!!!
while stdcall and safecall are used for Windows API calls. //是对API
The safecall convention must be used for declaring dual-interface methods.
The pascal convention is maintained for backward compatibility.


所以我在上面例子中写的Delphi调用方式,并不准确,但它可以运行,在Delphi的IDE环境中,
运行结束时,会报错, 原因在于参数的Clean-up不正确

function MyDllCall(n: Integer): Integer; stdcall; external 'DllTest.dll';
改为:
function MyDllCall(n: Integer): Integer; cdecl; external 'DllTest.dll';
才是最准确的!


另外,以下来自MSDN
__stdcall
Home | Overview | How Do I

Microsoft Specific —>

The __stdcall calling convention is used to call Win32 API functions. //是对API
The callee cleans the stack, so the compiler makes vararg functions __cdecl.
Functions that use this calling convention require a function prototype.

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
至少到目前VC6.0 WINAPI pascal __stdcall是一个意思, 我觉得<<VC++技术内幕>>Dll那章中,
描述的可能不准确或不清楚.








Alany 2001-05-26
  • 打赏
  • 举报
回复
上面的MyDllCall加上_stdcall/WINAPI可就输出不出来了
chinaprogrammer 2001-05-25
  • 打赏
  • 举报
回复
补充以下:
当然DELPHI也可以用动态装载方式.
chinaprogrammer 2001-05-25
  • 打赏
  • 举报
回复
以下是用VC6.0写的DLL
调用者分别是VC6.O写的EXE和DELPHI5.0写的EXE
不管VC写的EXE还是DELPHI写的EXE,都不需任何.h或.lib文件
说明:
1.调用约定可改为_stdcall,自己试一下吧.
2.DLL是正规MFC DLL


-----------------------------------------------------------------
DLL源码:


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

#include "stdafx.h"
#include "DllTest.h"

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

//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//

/////////////////////////////////////////////////////////////////////////////
// CDllTestApp

BEGIN_MESSAGE_MAP(CDllTestApp, CWinApp)
//{{AFX_MSG_MAP(CDllTestApp)
// 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()

/////////////////////////////////////////////////////////////////////////////
// CDllTestApp construction

CDllTestApp::CDllTestApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CDllTestApp object

CDllTestApp theApp;

/////////////////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport) int MyDllCall( int n)
{
return (n*n);

}

------------------------------------------------------------------
VC写的EXE调用源码:

void CCallDllTestDlg::OnButton1()
{

typedef int (Proc)(int);
HINSTANCE h;
Proc* pProc;
h = ::LoadLibrary("DllTest.dll");
pProc = (Proc*)::GetProcAddress(h, "MyDllCall");
int d = (*pProc)(10);
CString sz;
sz.Format("%d", d);
AfxMessageBox(sz);
}

-------------------------------------------------------------------

DELPHI写的调用源码:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

function MyDllCall(n: Integer): Integer; stdcall; external 'DllTest.dll';

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(IntToStr(MyDllCall(100)));
end;

end.
gpmn 2001-05-24
  • 打赏
  • 举报
回复
头文件还是要要的,lib可以不要,用LoadLibrary();
至于用到lib的方法,实际上就是静态连接,和用mfc的没两样
Alany 2001-05-24
  • 打赏
  • 举报
回复
真不知各位是不懂还是不屑一顾,这问题竟然没人再答了
eggplant 2001-05-22
  • 打赏
  • 举报
回复
那就在你要输出的函数前面加上_stdcall吧。
Alany 2001-05-22
  • 打赏
  • 举报
回复
加extern "C"后,在外部可以找到函数,但不能以_stdcall方式调用
sequoia_96 2001-05-22
  • 打赏
  • 举报
回复
同意楼上。。
Alany 2001-05-22
  • 打赏
  • 举报
回复
就是在输出的函数前加上_stdcall,extern "C"的函数才不能在调用时找到
ookaiii 2001-05-21
  • 打赏
  • 举报
回复
extern "C" 你家了么?他去处修饰副,详细的看看书把
Alany 2001-05-21
  • 打赏
  • 举报
回复
上面我说的就是用LoadLibrary,访问_stdcall的输出函数,VC做的DLL就是不行
Alany 2001-05-21
  • 打赏
  • 举报
回复
为什么C++Builder生成的DLL就可以完全摆脱Lib和头文件,而VC就不能
seesi 2001-05-21
  • 打赏
  • 举报
回复
调用有两种方法一种需要Lib和头文件,一种两都不需要,用LoadLibrary方法

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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