大家好!
我用C++ 写不了DLL,总是有linker errors,我找到的都是不完整的代码,无参照.初始化很复杂,可能我没有#include 正确的 header file.
能给一个完正的 DLL代码吗?(dll.h , dllmain.cpp;DLL只出口一个涵数就好)
关于DLL的问题 Topic is solved
a simple object about create DLL in windows
it's similar like create a lib file;
at first we will create a dll named test.dll
you should have code like this;
VC++
Create project; type "Win32 Dynamic-link library"
add two files
/* filename:test.h */
#ifndef TEST_H
#define TEST_H
/* here you declare the interface*/
extern "C" int __declspec(dllexport)add(int x, int y);
#endif
/* filename:TEST.cpp */
#include "TEST.h"
int add(int x, int y)
{
return x + y;
}
complier it...
en...
create another project use the DLL
it will be like..
#include <stdio.h>
#include <windows.h>
/* declare the fuction.like ADD()...*/
typedef int(*lpAddFun)(int, int);
int main(int argc, char *argv[])
{
HINSTANCE hDll; //DLL wnd
lpAddFun addFun; //point to the fuciton
/* load the dll*/
hDll = LoadLibrary("..path\Test.dll");
if (hDll != NULL)
{
/* get the fuction address*/
addFun = (lpAddFun)GetProcAddress(hDll, "add");
if (addFun != NULL)
{
/* use the fuction in the dll*/
int result = addFun(2, 3);
printf("%d", result);
}
FreeLibrary(hDll);
}
return 0;
}
__declspec(dllexport) is the key, i think;
at first we will create a dll named test.dll
you should have code like this;
VC++
Create project; type "Win32 Dynamic-link library"
add two files
/* filename:test.h */
#ifndef TEST_H
#define TEST_H
/* here you declare the interface*/
extern "C" int __declspec(dllexport)add(int x, int y);
#endif
/* filename:TEST.cpp */
#include "TEST.h"
int add(int x, int y)
{
return x + y;
}
complier it...
en...
create another project use the DLL
it will be like..
#include <stdio.h>
#include <windows.h>
/* declare the fuction.like ADD()...*/
typedef int(*lpAddFun)(int, int);
int main(int argc, char *argv[])
{
HINSTANCE hDll; //DLL wnd
lpAddFun addFun; //point to the fuciton
/* load the dll*/
hDll = LoadLibrary("..path\Test.dll");
if (hDll != NULL)
{
/* get the fuction address*/
addFun = (lpAddFun)GetProcAddress(hDll, "add");
if (addFun != NULL)
{
/* use the fuction in the dll*/
int result = addFun(2, 3);
printf("%d", result);
}
FreeLibrary(hDll);
}
return 0;
}
__declspec(dllexport) is the key, i think;
from: shanghai
language: C++/C
subject: GIS GPS
platform:all
MSN:[email protected]
also you can call me nemo;
language: C++/C
subject: GIS GPS
platform:all
MSN:[email protected]
also you can call me nemo;
我想你可以去看下VC技术内幕 22章节,动态链接库的介绍.从原理到应用都有比较详细的说明.针对WIN32平台.smallwolf wrote:非常感激nemok!
我按照你的例子写,成功篇译了.
后来我想出口一个 window, 老问题又来了,有很多linker errors.
我看了一个不完整的代码,发现如出口window,就要做很多的初始化在
dll main 里面.
我有尝试,还是失败.
可不可以再给个完整的代码.( dll.h,dllmain.cpp; DLL出口一个
wxMessageBox).
have a nice day!:)
from: shanghai
language: C++/C
subject: GIS GPS
platform:all
MSN:[email protected]
also you can call me nemo;
language: C++/C
subject: GIS GPS
platform:all
MSN:[email protected]
also you can call me nemo;