-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2643ccb
commit fc0f0d8
Showing
3 changed files
with
145 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
CodeBlocks������ʹ�ù����� | ||
2012-11-16 10:19 1641���Ķ� ����(0) �ղ� �ٱ� | ||
���ࣺ c���ԣ�2�� | ||
��Ȩ����������Ϊ����ԭ�����£�δ��������������ת�ء� | ||
����һ�����ֻ��һ��get_id() ������DLL�� | ||
һ������C���Թ�����1.�½�һ����̬��Ĺ��� | ||
File - New - Project - Shared library - Go | ||
�༭main.c | ||
main.c | ||
int add(int i1, int i2) | ||
{ | ||
return i1 + i2; | ||
} | ||
|
||
Ȼ����룬�ɹ�����bin\DebugĿ¼������3���ļ���libtest6.dll,libtest6.dll.a,libtest6.dll.def | ||
������̬���ӿ���� | ||
1����ʽ���� | ||
1������һ��test�Ĺ���File - New - Project - Console application - Go - ѡ�� cɾ��main.h���ѿ��test.h���Ƶ������У����ھ���main.c ��test.h | ||
main.c | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
int add(int, int);//ֱ����������ԭ�� | ||
int main() | ||
{ | ||
printf("%d\n",add(1,2)); | ||
system("pause"); | ||
return 0; | ||
} | ||
|
||
2����dll�����ӵ������� | ||
���ո����ɵ������ļ�libtest6.dll.a�� libtest6.dll���Ƶ�test���̵�bin\DebugĿ¼�� | ||
Project - Build options - ���Ͻ�Ĭ����Debug����ѡ�����ѡ�����Ǹ�test - Linker settings - Add ѡ�� bin\Debug\libtest6.dll.a - ȷ��,����ɹ����ɡ� | ||
2����ʾ���� | ||
|
||
1������һ��test1�Ĺ���File - New - Project - Console application - Go - ѡ�� �༭main.h�� | ||
main.c | ||
#include <stdio.h> | ||
#include <windows.h> | ||
typedef int(*lpAdd)(int,int); //���庯������ | ||
HINSTANCE hDll; //DLL��� | ||
lpAdd add; | ||
int main() | ||
{ | ||
hDll = LoadLibrary(" libtest6.dll"); //���� dll | ||
add = (lpAdd)GetProcAddress(hDll, "add");//ͨ��ָ���ȡ�������� | ||
printf("id = %d\n", add(1,2) );//���ú��� | ||
FreeLibrary(hDll);//�ͷ�Dll��� | ||
system("pause"); | ||
return 0; | ||
} | ||
|
||
simple.h������һ�� | ||
2����dll�����ӵ������� | ||
���ո����ɵ������ļ� libtest6.dll���Ƶ�test���̵�bin\DebugĿ¼�� | ||
����ɹ����ɡ� |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
Codeblocks�����͵���DLL��̬���ӿ⣨C���ԣ� | ||
2012-11-15 16:39 12841���Ķ� ����(1) �ղ� �ٱ� | ||
���ࣺ c���ԣ�2�� | ||
��Ȩ����������Ϊ����ԭ�����£�δ��������������ת�ء� | ||
��Դ��http://hi.baidu.com/hellosim/item/9ae4317168f4a74bee1e53cb | ||
����һ�����ֻ��һ��get_id() ������DLL�� | ||
һ������C���Զ�̬���ӿ� | ||
1.�½�һ����̬��Ĺ��� | ||
File - New - Project - DLL - Go | ||
�½��Ĺ���ԭ����main.cpp��main.hɾ�����½������ļ�simple.h, simple.c���ӽ����� | ||
ע��Ĭ����cpp�ļ���������C�⣬Ҫ��C�ļ� | ||
|
||
simple.h | ||
#ifndef SIMPLE_H_INCLUDED | ||
#define SIMPLE_H_INCLUDED | ||
#ifdef BUILD_DLL | ||
#define DLL_EXPORT __declspec(dllexport) | ||
#else | ||
#define DLL_EXPORT __declspec(dllimport) | ||
#endif | ||
int DLL_EXPORT get_id(void); | ||
int DLL_EXPORT add(int,int); | ||
#endif // SIMPLE_H_INCLUDED | ||
simple.c | ||
#include "simple.h" | ||
int DLL_EXPORT get_id(void) | ||
{ | ||
return 10; | ||
} | ||
int DLL_EXPORT add(int x,int y) | ||
{ | ||
return x+y; | ||
} | ||
|
||
Ȼ����룬�ɹ�����bin\DebugĿ¼������3���ļ���libsimple.dll.a�� simple.dll��libsimple.dll.def | ||
������̬���ӿ���� | ||
1����ʽ���� | ||
1������һ��test�Ĺ���File - New - Project - Console application - Go - ѡ�� cɾ��main.h���ѿ��test.h���Ƶ������У����ھ���main.c ��test.h | ||
main.c | ||
#include <stdio.h> | ||
#include "simple.h" | ||
int main() | ||
{ | ||
printf("id = %d\n", get_id() ); | ||
printf("id = %d\n", add(1,2) ); | ||
system("pause"); | ||
return 0; | ||
} | ||
|
||
|
||
simple.h������һ�� | ||
2����dll�����ӵ������� | ||
���ո����ɵ������ļ�libsimple.a�� libsimple.dll���Ƶ�test���̵�bin\DebugĿ¼�� | ||
Project - Build options - ���Ͻ�Ĭ����Debug����ѡ�����ѡ�����Ǹ�test - Linker settings - Add ѡ�� bin\Debug\libsimple.dll.a - ȷ��,����ɹ����ɡ� | ||
2����ʾ���� | ||
|
||
1������һ��test1�Ĺ���File - New - Project - Console application - Go - ѡ�� �༭main.h�� | ||
main.c | ||
#include <stdio.h> | ||
#include <windows.h> | ||
typedef int(*lpGet_id)(void); //���庯������ | ||
typedef int(*lpAdd)(int,int); //���庯������ | ||
HINSTANCE hDll; //DLL��� | ||
lpGet_id get_id; | ||
lpAdd add; | ||
int main() | ||
{ | ||
hDll = LoadLibrary("simple.dll"); //���� dll | ||
get_id = (lpGet_id)GetProcAddress(hDll, "get_id");//ͨ��ָ���ȡ�������� | ||
add = (lpAdd)GetProcAddress(hDll, "add");//ͨ��ָ���ȡ�������� | ||
printf("id = %d\n", get_id() );//���ú��� | ||
printf("id = %d\n", add(1,2) );//���ú��� | ||
FreeLibrary(hDll);//�ͷ�Dll��� | ||
system("pause"); | ||
return 0; | ||
} | ||
|
||
simple.h������һ�� | ||
2����dll�����ӵ������� | ||
���ո����ɵ������ļ�libsimple.dll���Ƶ�test���̵�bin\DebugĿ¼�� | ||
����ɹ����ɡ� |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters