RT
// #pragma comment(lib, "../Debug/ShareDll.lib")
// __declspec(dllimport) char g_szSynch[32];
// __declspec(dllimport) char g_szBuff[1024];
// __declspec(dllimport) ULONG g_uBuffLen;
隐式调用可以写共享缓冲区
HMODULE hMod = LoadLibrary("../Debug/ShareDll.dll");
if (hMod)
{
char* g_szSynchW = (char*)GetProcAddress(hMod, "g_szSynch");
char* g_szBuffW = (char*)GetProcAddress(hMod, "g_szBuff");
ULONG* g_puBuffLenW = (ULONG*)GetProcAddress(hMod, "g_uBuffLen");
if (g_szSynchW && g_szBuffW && g_puBuffLenW)
{
...
显式调用写不了共享缓冲区
附上小例VS2008,求科普!感谢
dll.cpp
#include <windows.h>
//define data segment
#pragma data_seg("ShareDll")
//...
char g_szBuff[1024] = "Some Shared Global Variable";//share data
char g_szSynch[32] = "Synchronized Event";//synchronized event name
ULONG g_uBuffLen = 1023;
//...
#pragma data_seg()
#pragma comment(linker, "/SECTION:ShareDll,RWS")
dll.def
EXPORTS
g_szSynch
g_uBuffLen
g_szBuff
CxxDlg.cpp
void CxxDlg::OnBnClickedButton4()
{
CString strSend;
GetDlgItemText(IDC_EDIT1, strSend);
ULONG uWriteLen = strSend.GetLength();
if (!uWriteLen)
{
return;
}
//share dll write
HMODULE hMod = LoadLibrary("../Debug/ShareDll.dll");
if (hMod)
{
char* g_szSynchW = (char*)GetProcAddress(hMod, "g_szSynch");
char* g_szBuffW = (char*)GetProcAddress(hMod, "g_szBuff");
ULONG* g_puBuffLenW = (ULONG*)GetProcAddress(hMod, "g_uBuffLen");
if (g_szSynchW && g_szBuffW && g_puBuffLenW)
{
ULONG uBuffLen = *g_puBuffLenW;
HANDLE hEvent = ::OpenEvent( EVENT_ALL_ACCESS, FALSE, g_szSynchW );
if ( !hEvent )
{
//create synchronized object
hEvent = ::CreateEvent( NULL, FALSE/*auto*/, TRUE/*init*/, g_szSynchW );
if ( hEvent )
{
WaitForSingleObject( hEvent, INFINITE );
ULONG u = uWriteLen>uBuffLen ? uBuffLen : uWriteLen;
memmove(g_szBuffW, strSend.GetBuffer(0), u);
SetEvent( hEvent );
CloseHandle( hEvent );
hEvent = NULL;
}
}
}
FreeLibrary(hMod);
hMod = NULL;
}
// #pragma comment(lib, "../Debug/ShareDll.lib")
// __declspec(dllimport) char g_szSynch[32];
// __declspec(dllimport) char g_szBuff[1024];
// __declspec(dllimport) ULONG g_uBuffLen;
// HANDLE hEvent = ::OpenEvent( EVENT_ALL_ACCESS, FALSE, g_szSynch );
// if ( !hEvent )
// {
// //create synchronized object
// hEvent = ::CreateEvent( NULL, FALSE/*auto*/, TRUE/*init*/, g_szSynch );
// if ( hEvent )
// {
// WaitForSingleObject( hEvent, INFINITE );
// ULONG u = uWriteLen>g_uBuffLen ? g_uBuffLen : uWriteLen;
// memset(g_szBuff, 0, g_uBuffLen);
// memmove(g_szBuff, strSend.GetBuffer(0), u);
// SetEvent( hEvent );
//
// CloseHandle( hEvent );
// hEvent = NULL;
// }
// }
}
MFC工程真特么大....
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!