关于DLL的问题 Topic is solved

这是wxWidgets论坛的中文版本。在这里,您可以用您的母语汉语讨论上面任一子论坛所涉及的所有关于wxWidgets的话题。欢迎大家参与到对有价值的帖子的中英互译工作中来!
Post Reply
smallwolf
Experienced Solver
Experienced Solver
Posts: 69
Joined: Sun Aug 26, 2007 11:02 pm

关于DLL的问题

Post by smallwolf »

大家好!
我用C++ 写不了DLL,总是有linker errors,我找到的都是不完整的代码,无参照.初始化很复杂,可能我没有#include 正确的 header file.

能给一个完正的 DLL代码吗?(dll.h , dllmain.cpp;DLL只出口一个涵数就好)
nemok
Earned a small fee
Earned a small fee
Posts: 17
Joined: Thu Mar 06, 2008 12:44 am

a simple object about create DLL in windows

Post by nemok »

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;
from: shanghai
language: C++/C
subject: GIS GPS
platform:all
MSN:[email protected]

also you can call me nemo;
smallwolf
Experienced Solver
Experienced Solver
Posts: 69
Joined: Sun Aug 26, 2007 11:02 pm

Post by smallwolf »

非常感激nemok!

我按照你的例子写,成功篇译了.
后来我想出口一个 window, 老问题又来了,有很多linker errors.
我看了一个不完整的代码,发现如出口window,就要做很多的初始化在
dll main 里面.

我有尝试,还是失败.

可不可以再给个完整的代码.( dll.h,dllmain.cpp; DLL出口一个
wxMessageBox).

have a nice day!:)
nemok
Earned a small fee
Earned a small fee
Posts: 17
Joined: Thu Mar 06, 2008 12:44 am

Post by nemok »

smallwolf wrote:非常感激nemok!

我按照你的例子写,成功篇译了.
后来我想出口一个 window, 老问题又来了,有很多linker errors.
我看了一个不完整的代码,发现如出口window,就要做很多的初始化在
dll main 里面.

我有尝试,还是失败.

可不可以再给个完整的代码.( dll.h,dllmain.cpp; DLL出口一个
wxMessageBox).

have a nice day!:)
我想你可以去看下VC技术内幕 22章节,动态链接库的介绍.从原理到应用都有比较详细的说明.针对WIN32平台.
from: shanghai
language: C++/C
subject: GIS GPS
platform:all
MSN:[email protected]

also you can call me nemo;
hats
Experienced Solver
Experienced Solver
Posts: 70
Joined: Wed Sep 16, 2009 3:50 pm
Location: China
Contact:

Post by hats »

谢谢了,我编译成功了。以前在网上找好多代码都没用。
Post Reply