(筆記) 如何使用Visual C++ 6.0開發Win32 DLL? (C/C++) (VC++)

Abstract
本文討論使用Visual Studio 6.0的Visaul C++開發Win32 DLL。

Introduction
使用環境:Visual Studio 6.0, Visual C++ 6.0

為什麼要使用1998年的Visual C++ 6.0呢?因為很多MFC coder還是使用Visual C++ 6.0,很多企業也還是用Visual C++ 6.0繼續維護以前的程式,儘管新的程式陸續改用.NET,要與舊的程式溝通時,仍然是用Visual C++ 6.0將以前的Business Rule包成DLL給.NET使用。

Step 1:

New Project

win32_sum00

選擇Win32 Dynamic-Link Library

Step 2:

Select project template

win32_sum01

Step 3:

Project Summary

win32_sum02

Step 4:

win32_sum.cpp / C++

1 // win32_sum.cpp : Defines the entry point for the DLL application.
2  //
3  
4 #include "stdafx.h"
5
6 BOOL APIENTRY DllMain( HANDLE hModule,
7 DWORD ul_reason_for_call,
8 LPVOID lpReserved
9 )
10 {
11 return TRUE;
12 }
13
14  // our function to export from dll
15  int _stdcall sum(int x, int y) {
16 return x + y;
17 }

14行

// our function to export from dll
int _stdcall sum(int x, int y) {
return x + y;
}

 為我們dll所要export的function,一定要加上_stdcall,其他就如同我們寫一般C/C++的function一樣。

Step 5:

DEF files

我們需要另外寫DEF檔,告訴Visual C++哪些function想要export出去。

win32_sum03

New一個文字檔,檔名與project一樣,但副檔名改為.def。

win32_sum.def

1 LIBRARY win32_sum
2
3 EXPORTS
4 sum @1

Step 6:

編譯DLL

win32_sum04

編譯結果

--------------------Configuration: win32_sum - Win32 Debug--------------------
Compiling...
StdAfx.cpp
Compiling...
win32_sum.cpp
Linking...
Creating library Debug/win32_sum.lib and object Debug/win32_sum.exp

win32_sum.dll -
0 error(s), 0 warning(s)

將win32_sum.dll複製到c:\windows\目錄下,則其他應用程式就可以使用你開發的DLL了。

完整程式碼下載
win32_sum.7z

Reference
MSDN Exporting from a DLL Using DEF Files

See Also

(筆記) 如何使用C#使用Win32 DLL? (.NET) (C#) (Windows Form)

全文完。

posted on 2011-02-13 19:05  真 OO无双  阅读(8858)  评论(0编辑  收藏  举报

导航