使用DLL通信(摘自《Windows应用程序捆绑核心编程》)

//DllObj.h
#ifndef _DLLOBJ_H_INCLUDED
#define _DLLOBJ_H_INCLUDED
#include
<afxmt.h>

#ifdef _cplusplus
#define _DLLCOM_ extern "C" _declspec (dllexport)
#else
#define _DLLCOM_ _declspec (dllexport)
#endif

#endif

_DLLCOM_ LPSTR GetValueString();
_DLLCOM_
void setValueString(LPCSTR);

 

//DllObj.cpp
#include "stdafx.h"
#include
"DllObj.h"

#pragma data_seg("MyShared")
char m_str[256];
volatile bool bInCriticalSection = FALSE;
#pragma data_seg()

#pragma comment(linker, "/SECTION:MyShared,RWS")



CCriticalSection cs;

_DLLCOM_ LPSTR GetValueString()
{
while(bInCriticalSection)
Sleep(
1);
return m_str;
}
_DLLCOM_
void setValueString(LPCSTR str)
{
cs.Lock();
bInCriticalSection
= TRUE;
strcpy(m_str, str);
bInCriticalSection
= FALSE;
cs.Unlock();
}

把动态库文件头DllObj.h和动态库MyShared.lib分别包含在要通信的两个程序中

 

#include "DllObj.h"

#pragma comment(lib, "MyShared.lib")

 

posted @ 2010-09-02 21:01  penink  阅读(407)  评论(0)    收藏  举报