|
![]() | 作者: programlive [coolcall]
![]() |
登录 |
下面这段是DLL的测试程序; #include "stdafx.h" #include <stdio.h> #pragma comment(lib,"ImageViewer") extern "C"_declspec(dllimport) int Max(int a,int b); void main() { int s=Max(8,10); printf("相加的结果为%d\n",s); } -------------------------------------------------------------- 编译出错: --------------------Configuration: testdll - Win32 Debug-------------------- Compiling... testdll.cpp D:\vc++\MSDev98\MyProjects\testdll\testdll.cpp(13) : error C2018: unknown character '0xa3' D:\vc++\MSDev98\MyProjects\testdll\testdll.cpp(13) : error C2018: unknown character '0xac' D:\vc++\MSDev98\MyProjects\testdll\testdll.cpp(13) : error C2146: syntax error : missing ')' before identifier 's' D:\vc++\MSDev98\MyProjects\testdll\testdll.cpp(13) : error C2059: syntax error : ')' Error executing cl.exe. testdll.exe - 4 error(s), 0 warning(s) ----------------------------------------------------------- 下面这段是DLL的代码,可以运行。 ImageViewer.def: ; ImageViewer.def : Declares the module parameters for the DLL. LIBRARY "ImageViewer" DESCRIPTION 'ImageViewer Windows Dynamic Link Library' EXPORTS Max ; Explicit exports can go here ImageViewer.cpp: #include "stdafx.h" #include <afxdllx.h> #include<windows.h> #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif static AFX_EXTENSION_MODULE ImageViewerDLL = { NULL, NULL }; extern "C" int APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) { UNREFERENCED_PARAMETER(lpReserved); if (dwReason == DLL_PROCESS_ATTACH) { TRACE0("IMAGEVIEWER.DLL Initializing!\n"); if (!AfxInitExtensionModule(ImageViewerDLL, hInstance)) return 0; new CDynLinkLibrary(ImageViewerDLL); } else if (dwReason == DLL_PROCESS_DETACH) { TRACE0("IMAGEVIEWER.DLL Terminating!\n"); AfxTermExtensionModule(ImageViewerDLL); } return 1; // ok } int Max(int a, int b) { int x=a+b; return x; } 请问我那里写错了? [此贴被 programlive(coolcall) 在 06月18日14时20分 编辑过] [此贴被 programlive(coolcall) 在 06月18日16时44分 编辑过] |
地主 发表时间: 04-06-18 14:19 |
![]() | 回复: 286 [unique] ![]() |
登录 |
1 把文件名改为testdll.c,而不是testdll.cpp。 2 第十三行是什么东西,刚开始没明白,后来才知道,呵呵,逗号错了。把","改成","。 |
B1层 发表时间: 04-06-18 16:30 |
![]() | 回复: programlive [coolcall] ![]() |
登录 |
我把“,”改成“,"之后把一个错误消除了。 没把testdll.cpp改成testdll.c,编译时: --------------------Configuration: testdll - Win32 Debug-------------------- Compiling... testdll.cpp Linking... testdll.obj : error LNK2001: unresolved external symbol __imp__Max Debug/testdll.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. testdll.exe - 2 error(s), 0 warning(s) ------------- 把 int s=Max(8,10); printf("相加的结果为%d\n",s); 删了,编译成功。 -------------------------------------- 把testdll.cpp,改成testdll.c又出现另一个错误 --------------------Configuration: testdll - Win32 Debug-------------------- Compiling... testdll.c d:\vc++\msdev98\myprojects\testdll\testdll.c(2) : fatal error C1853: 'Debug/testdll.pch' is not a precompiled header file created with this compiler Error executing cl.exe. testdll.exe - 1 error(s), 0 warning(s) |
B2层 发表时间: 04-06-18 16:57 |
![]() | 回复: programlive [coolcall] ![]() |
登录 |
上面的隐式连接,还是搞不得,反到搞得了个显式连接的(书上不是说隐式比较容易吗?郁闷) 下面的是显式连接的程序.测试通过(又学会一样 ![]() #include "stdafx.h" #include <stdio.h> #include <windows.h> int main(int argc, char* argv[]) { int A; HINSTANCE HDLL = LoadLibrary("ImageViewer.dll");//通过LoadLibray()将DLL加载到当前的应用程序中并返回当前DLL文件的句柄 typedef int (*pmax)(int a,int b);//定义指向和DLL中相同的函数原型指针 pmax Max = (pmax)GetProcAddress(HDLL,"Max");//获取导入到应用程序中的函数指针 A = Max(2,3); printf("%d",A); FreeLibrary(HDLL);卸载DLL文件 return 0; } |
B3层 发表时间: 04-06-19 22:31 |
![]() | 回复: w3srv [w3srv] ![]() |
登录 |
DLL文件在加裁时是是regsvr32安装还是直接copy到system32目录?不怎么清楚 |
B4层 发表时间: 04-06-20 21:57 |
![]() | 回复: w3srv [w3srv] ![]() |
登录 |
DLL文件的后缀名是c还是cpp,了解的解答 |
B5层 发表时间: 04-06-20 22:05 |
![]() | 回复: Frankiez [frankiez84] ![]() |
登录 |
你说的regsvr32安装是activex的dll 普通dll并不是这样,在隐式声明后程序启动后自动搜索当前目录,程序执行目录和系统目录 显式载入,也就是所谓的函数指针后期绑定则是在显式的使用loadlibrary后才载入内存,然后由你来指定所需的函数名并返回相应的地址指针再绑定给你的指针变量,最后调用的 |
B6层 发表时间: 04-06-20 22:06 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号