⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_54792.htm

📁 vc
💻 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>&nbsp;&nbsp;&nbsp;&nbsp;CMymaths(void);<BR>&nbsp;&nbsp;&nbsp;&nbsp;// 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 &#34;stdafx.h&#34;<BR>#include &#34;mymaths.h&#34;<BR><BR>BOOL APIENTRY DllMain( HANDLE hModule, <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DWORD&nbsp;&nbsp;ul_reason_for_call, <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LPVOID lpReserved<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;switch (ul_reason_for_call)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case DLL_PROCESS_ATTACH:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case DLL_THREAD_ATTACH:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case DLL_THREAD_DETACH:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case DLL_PROCESS_DETACH:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;return; <BR>}<BR><BR>/////////////////////////////////////////////////////////////////////<BR>//加入导出函数的定义<BR>MYMATHS_API int Summary(int n)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;int sum=0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;int i;<BR>&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;i&lt;=n;i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum+=i;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;return sum;<BR>}<BR><BR>MYMATHS_API int Factorial(int n)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;int fact=1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;int i;<BR>&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;i&lt;=n;i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fact*=i;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;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 &gt; 1000<BR>#pragma once<BR>#endif // _MSC_VER &gt; 1000<BR><BR>/////////////////////////////////////////////////////////////////////////////<BR>// CTestDlg dialog<BR><BR>class CTestDlg : public CDialog<BR>{<BR>// Construction<BR>public:<BR>&nbsp;&nbsp;&nbsp;&nbsp;CTestDlg(CWnd* pParent = NULL);&nbsp;&nbsp;&nbsp;&nbsp;// standard constructor<BR><BR>protected:<BR>&nbsp;&nbsp;&nbsp;&nbsp;void LoadDll();<BR><BR>// Dialog Data<BR>&nbsp;&nbsp;&nbsp;&nbsp;//{{AFX_DATA(CTestDlg)<BR>&nbsp;&nbsp;&nbsp;&nbsp;enum { IDD = IDD_TEST_DIALOG };<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// NOTE: the ClassWizard will add data members here<BR>&nbsp;&nbsp;&nbsp;&nbsp;//}}AFX_DATA<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;// ClassWizard generated virtual function overrides<BR>&nbsp;&nbsp;&nbsp;&nbsp;//{{AFX_VIRTUAL(CTestDlg)<BR>&nbsp;&nbsp;&nbsp;&nbsp;protected:<BR>&nbsp;&nbsp;&nbsp;&nbsp;virtual void DoDataExchange(CDataExchange* pDX);&nbsp;&nbsp;&nbsp;&nbsp;// DDX/DDV support<BR>&nbsp;&nbsp;&nbsp;&nbsp;//}}AFX_VIRTUAL<BR><BR>// Implementation<BR>protected:<BR>&nbsp;&nbsp;&nbsp;&nbsp;HICON m_hIcon;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;// Generated message map functions<BR>&nbsp;&nbsp;&nbsp;&nbsp;//{{AFX_MSG(CTestDlg)<BR>&nbsp;&nbsp;&nbsp;&nbsp;virtual BOOL OnInitDialog();<BR>&nbsp;&nbsp;&nbsp;&nbsp;afx_msg void OnSysCommand(UINT nID, LPARAM lParam);<BR>&nbsp;&nbsp;&nbsp;&nbsp;afx_msg void OnPaint();<BR>&nbsp;&nbsp;&nbsp;&nbsp;afx_msg HCURSOR OnQueryDragIcon();<BR>&nbsp;&nbsp;&nbsp;&nbsp;afx_msg void OnFact();<BR>&nbsp;&nbsp;&nbsp;&nbsp;afx_msg void OnSum();<BR>&nbsp;&nbsp;&nbsp;&nbsp;//}}AFX_MSG<BR>&nbsp;&nbsp;&nbsp;&nbsp;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 &#34;stdafx.h&#34;<BR>#include &#34;Test.h&#34;<BR>#include &#34;TestDlg.h&#34;<BR><BR>//#include &#34;mymaths.h&#34;<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>&nbsp;&nbsp;&nbsp;&nbsp;// TODO: Add your control notification handler code here<BR>&nbsp;&nbsp;&nbsp;&nbsp;LoadDll();<BR>&nbsp;&nbsp;&nbsp;&nbsp;int nSum=Summary(10);<BR>&nbsp;&nbsp;&nbsp;&nbsp;CString sResult;<BR>&nbsp;&nbsp;&nbsp;&nbsp;sResult.Format(&#34;Sum(10)=%d&#34;,nSum);<BR>&nbsp;&nbsp;&nbsp;&nbsp;AfxMessageBox(sResult);<BR><BR>}<BR><BR>void CTestDlg::OnFact() //求积消息<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;// TODO: Add your control notification handler code here<BR>&nbsp;&nbsp;&nbsp;&nbsp;LoadDll();<BR>&nbsp;&nbsp;&nbsp;&nbsp;int nFact=Factorial(10);<BR>&nbsp;&nbsp;&nbsp;&nbsp;CString sResult;<BR>&nbsp;&nbsp;&nbsp;&nbsp;sResult.Format(&#34;Fact(10)=%d&#34;,nFact);<BR>&nbsp;&nbsp;&nbsp;&nbsp;AfxMessageBox(sResult);<BR><BR>}<BR><BR>void CTestDlg::LoadDll()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;//如果Dll已经载入,则返回<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(ghMathsDll!=NULL)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;//载入mymaths.dll文件<BR>&nbsp;&nbsp;&nbsp;&nbsp;ghMathsDll=LoadLibrary(&#34;mymaths.Dll&#34;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;//如果载入Dll失败,则提示用户<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(ghMathsDll==NULL)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AfxMessageBox(&#34;载入Dll失败&#34;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;//获得Dll中Summary函数的地址<BR>&nbsp;&nbsp;&nbsp;&nbsp;Summary=(SUMMARY)GetProcAddress(ghMathsDll,&#34;Summary&#34;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;//获得Dll中Factorial函数的地址<BR>&nbsp;&nbsp;&nbsp;&nbsp;Factorial=(FACTORIAL)GetProcAddress(ghMathsDll,&#34;Factorial&#34;);<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 &#34;C&#34;就正常了,但是书里没加。<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 &#34;C&#34;。。。您可能没有包含那个宏定义的头文件。。在自己的头文件中定义也可以。。<BR>#define MYMATHS_API&nbsp;&nbsp; extern &#34;C&#34;这样就可以了。。。祝您好运。。<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 + -