📄 subject_54792.htm
字号:
<p>
序号:54792 发表者:鹏程 发表日期:2003-10-01 12:19:41
<br>主题:简单问题,按书的例题输入的,却不能运行???
<br>内容:动态连接库制作:<BR>动态连接库头文件:<BR>//mymaths.h<BR>// The following ifdef block is the standard way of creating macros which make exporting <BR>// from a DLL simpler. All files within this DLL are compiled with the MYMATHS_EXPORTS<BR>// symbol defined on the command line. this symbol should not be defined on any project<BR>// that uses this DLL. This way any other project whose source files include this file see <BR>// MYMATHS_API functions as being imported from a DLL, wheras this DLL sees symbols<BR>// defined with this macro as being exported.<BR>#ifdef MYMATHS_EXPORTS<BR>#define MYMATHS_API __declspec(dllexport)<BR>#else<BR>#define MYMATHS_API __declspec(dllimport)<BR>#endif<BR><BR>// This class is exported from the mymaths.dll<BR>class MYMATHS_API CMymaths <BR>{<BR>public:<BR> CMymaths(void);<BR> // TODO: add your methods here.<BR>};<BR><BR>extern MYMATHS_API int nMymaths;<BR><BR>MYMATHS_API int fnMymaths(void);<BR><BR>/////////////////////////////////////////////////////////////////////<BR>//加入导出函数的导出原形定义<BR>MYMATHS_API int Summary(int);<BR><BR>MYMATHS_API int Factorial(int);<BR><BR><BR><BR>动态连接库实现文件:<BR>// mymaths.cpp : Defines the entry point for the DLL application.<BR>//<BR><BR>#include "stdafx.h"<BR>#include "mymaths.h"<BR><BR>BOOL APIENTRY DllMain( HANDLE hModule, <BR> DWORD ul_reason_for_call, <BR> LPVOID lpReserved<BR> )<BR>{<BR> switch (ul_reason_for_call)<BR> {<BR> case DLL_PROCESS_ATTACH:<BR> case DLL_THREAD_ATTACH:<BR> case DLL_THREAD_DETACH:<BR> case DLL_PROCESS_DETACH:<BR> break;<BR> }<BR> return TRUE;<BR>}<BR><BR><BR>// This is an example of an exported variable<BR>MYMATHS_API int nMymaths=0;<BR><BR>// This is an example of an exported function.<BR>MYMATHS_API int fnMymaths(void)<BR>{<BR> return 42;<BR>}<BR><BR>// This is the constructor of a class that has been exported.<BR>// see mymaths.h for the class definition<BR>CMymaths::CMymaths()<BR>{ <BR> return; <BR>}<BR><BR>/////////////////////////////////////////////////////////////////////<BR>//加入导出函数的定义<BR>MYMATHS_API int Summary(int n)<BR>{<BR> int sum=0;<BR> int i;<BR> for(i=1;i<=n;i++)<BR> {<BR> sum+=i;<BR> }<BR> return sum;<BR>}<BR><BR>MYMATHS_API int Factorial(int n)<BR>{<BR> int fact=1;<BR> int i;<BR> for(i=1;i<=n;i++)<BR> {<BR> fact*=i;<BR> }<BR> return fact;<BR>}<BR><BR><BR><BR><BR><BR><BR>**************说明:已经将“mymath.h”和“mymath.lib”拷贝到了Text目录下了,而将“mymath.Dll”拷贝到了Text\Debug目录下了:<BR>动态连接库测试Test工程文件:<BR>//头文件:<BR>// TestDlg.h : header file<BR>//<BR><BR>#if !defined(AFX_TESTDLG_H__683A8F40_7C93_4180_AFC3_099CB4BC745E__INCLUDED_)<BR>#define AFX_TESTDLG_H__683A8F40_7C93_4180_AFC3_099CB4BC745E__INCLUDED_<BR><BR>#if _MSC_VER > 1000<BR>#pragma once<BR>#endif // _MSC_VER > 1000<BR><BR>/////////////////////////////////////////////////////////////////////////////<BR>// CTestDlg dialog<BR><BR>class CTestDlg : public CDialog<BR>{<BR>// Construction<BR>public:<BR> CTestDlg(CWnd* pParent = NULL); // standard constructor<BR><BR>protected:<BR> void LoadDll();<BR><BR>// Dialog Data<BR> //{{AFX_DATA(CTestDlg)<BR> enum { IDD = IDD_TEST_DIALOG };<BR> // NOTE: the ClassWizard will add data members here<BR> //}}AFX_DATA<BR><BR> // ClassWizard generated virtual function overrides<BR> //{{AFX_VIRTUAL(CTestDlg)<BR> protected:<BR> virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support<BR> //}}AFX_VIRTUAL<BR><BR>// Implementation<BR>protected:<BR> HICON m_hIcon;<BR><BR> // Generated message map functions<BR> //{{AFX_MSG(CTestDlg)<BR> virtual BOOL OnInitDialog();<BR> afx_msg void OnSysCommand(UINT nID, LPARAM lParam);<BR> afx_msg void OnPaint();<BR> afx_msg HCURSOR OnQueryDragIcon();<BR> afx_msg void OnFact();<BR> afx_msg void OnSum();<BR> //}}AFX_MSG<BR> DECLARE_MESSAGE_MAP()<BR>};<BR><BR>//{{AFX_INSERT_LOCATION}}<BR>// Microsoft Visual C++ will insert additional declarations immediately before the previous line.<BR><BR>#endif // !defined(AFX_TESTDLG_H__683A8F40_7C93_4180_AFC3_099CB4BC745E__INCLUDED_)<BR><BR>//实现文件:<BR>// TestDlg.cpp : implementation file<BR>//<BR><BR>#include "stdafx.h"<BR>#include "Test.h"<BR>#include "TestDlg.h"<BR><BR>//#include "mymaths.h"<BR><BR>#ifdef _DEBUG<BR>#define new DEBUG_NEW<BR>#undef THIS_FILE<BR>static char THIS_FILE[] = __FILE__;<BR>#endif<BR>....<BR>....<BR>....<BR>void CTestDlg::OnSum() //求和消息<BR>{<BR> // TODO: Add your control notification handler code here<BR> LoadDll();<BR> int nSum=Summary(10);<BR> CString sResult;<BR> sResult.Format("Sum(10)=%d",nSum);<BR> AfxMessageBox(sResult);<BR><BR>}<BR><BR>void CTestDlg::OnFact() //求积消息<BR>{<BR> // TODO: Add your control notification handler code here<BR> LoadDll();<BR> int nFact=Factorial(10);<BR> CString sResult;<BR> sResult.Format("Fact(10)=%d",nFact);<BR> AfxMessageBox(sResult);<BR><BR>}<BR><BR>void CTestDlg::LoadDll()<BR>{<BR> //如果Dll已经载入,则返回<BR> if(ghMathsDll!=NULL)<BR> {<BR> return;<BR> }<BR> //载入mymaths.dll文件<BR> ghMathsDll=LoadLibrary("mymaths.Dll");<BR> //如果载入Dll失败,则提示用户<BR> if(ghMathsDll==NULL)<BR> {<BR> AfxMessageBox("载入Dll失败");<BR> }<BR> //获得Dll中Summary函数的地址<BR> Summary=(SUMMARY)GetProcAddress(ghMathsDll,"Summary");<BR> //获得Dll中Factorial函数的地址<BR> Factorial=(FACTORIAL)GetProcAddress(ghMathsDll,"Factorial");<BR><BR>}<BR><BR>上述测试工程编译都通过了,但是运行时只要单击求和或者求积按钮就出现错误,弹出“程序发生错误,关闭,调试”对话框,不知错在哪了?<BR><BR><BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:jackyxio 回复日期:2003-10-01 23:41:19
<br>内容:你BEBUG单步看看是哪一句错了!!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:鹏程 回复日期:2003-10-02 03:58:59
<br>内容:在下面函数前加上extern "C"就正常了,但是书里没加。<BR><BR>/////////////////////////////////////////////////////////////////////<BR>//加入导出函数的导出原形定义<BR>MYMATHS_API int Summary(int);<BR><BR>MYMATHS_API int Factorial(int);<BR><BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:鹏程 回复日期:2003-10-08 16:35:32
<br>内容:哪位朋友给解释一下,谢谢了!!!<BR><BR><BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:MoonKeey猴子 回复日期:2003-10-08 17:34:17
<br>内容:朋友:<BR>你好。。<BR>关于这个问题,不要太计较了。。这种问题非常普遍。写书人不一定就是将完整的程序全都复制粘贴给读者的。。他的程序有自己的运行环境。。其实每个人的程序都有自己的运行环境。。很可能是因为没有包含哪个头文件而出错的。。MYMATHS_API很显然是一个宏定义。。就相当于extern "C"。。。您可能没有包含那个宏定义的头文件。。在自己的头文件中定义也可以。。<BR>#define MYMATHS_API extern "C"这样就可以了。。。祝您好运。。<BR><BR>小猴
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:鹏程 回复日期:2003-10-08 20:09:41
<br>内容:朋友见教的是,谢谢。<BR><BR><BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -