Windows Mobile和Wince(Windows Embedded CE)下封装Native DLL进一步探讨

之前写过一篇关于Windows Mobile和Wince(Windows Embedded CE)下封装Native DLL的文章,原文如下:

Windows Mobile和Wince(Windows Embedded CE)下如何封装Native DLL提供给.NET Compact Framework进行调用

 

原文主要针对如何封装的Native DLL提供给.NET Compact Framework的程序来调用。但是如果按照原文的方法进行封装,使用Native C++调用该封装DLL会有一些麻烦,需要动态加载该DLL,关于动态加载,我之前也写过一篇文章,原文如下:

如何在Windows Mobile下使用Native C++动态加载DLL

 

动态加载有其好处,但是如果封装的DLL和调用方都是内部的程序,使用初始化时候加载(也叫做静态加载),在开发时会更加方便和简单。下面讲述如何封装DLL为调用方提供静态加载服务。

1.建立输出定义头文件

// The following ifdef block is the standard way of creating macros which make exporting 
// from a DLL simpler. All files within this DLL are compiled with the MY32FEETWIDCOMM_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// MY32FEETWIDCOMM_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef MY32FEETWIDCOMM_EXPORTS
#define MY32FEETWIDCOMM_API __declspec(dllexport)
#else
#define MY32FEETWIDCOMM_API __declspec(dllimport)
#endif

// This class is exported from the 32feetWidcommWM.dll
class MY32FEETWIDCOMMWM_API CMy32feetWidcommWM {
public:
CMy32feetWidcommWM(void);
// TODO: add your methods here.
};

extern MY32FEETWIDCOMMWM_API int nMy32feetWidcommWM;

MY32FEETWIDCOMMWM_API int fnMy32feetWidcommWM(void);

调用方需要#include该头文件。

 

2.实现头文件提供的接口

// 32feetWidcommWM.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "32feetWidcommWM.h"
#include <windows.h>
#include <commctrl.h>

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

// This is an example of an exported variable
MY32FEETWIDCOMMWM_API int nMy32feetWidcommWM=0;

// This is an example of an exported function.
MY32FEETWIDCOMMWM_API int fnMy32feetWidcommWM(void)
{
return 42;
}

// This is the constructor of a class that has been exported.
// see 32feetWidcommWM.h for the class definition
CMy32feetWidcommWM::CMy32feetWidcommWM()
{
return;
}

 

3.配置预编译宏(Preprocessor Definitions)

 export-dll-2

封装DLL的项目会输出接口 __declspec(dllexport) ,而调用方会输入接口 __declspec(dllimport) ,见头文件定义。

4.配置输出lib文件

export-dll-1

调用方需要链接该lib文件。

 

完成了。

posted @ 2009-11-18 08:44  Jake Lin  阅读(1261)  评论(4编辑  收藏  举报