主题:关于BCB和VB之间通过DLL进行数据交换的问题
现在与人合作,需要写一个BCB的DLL.
我的BCB的程序将通过这DLL和对方的一个VB程序交换数据.
因为我不懂VB.对DLL也是刚开始,尤其对BCB和VB程序如何共同工作很是模糊.
对方也不懂BCB.所以感到有困难.
我的BCB和DLL之间传递数据没有问题,但对方总无法读取DLL
不知我的DLL是否有问题.请高手指点.
程序如下:
1) DLL程序.shallvalue.cpp
extern "C" __declspec(dllexport)void SetValue(int val);
extern "C" __declspec(dllexport)int GetValue();
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
这是一个共享数据的class
class SharedMemory
{
int *_data;
HANDLE m_hMap;
HANDLE m_hMutex;
public:
SharedMemory();
virtual ~SharedMemory();
inline int* GetData() { return _data; };
inline void SetData(int d) { *_data = d; };
// Lock with mutex
void Lock() { ::WaitForSingleObject(m_hMutex, INFINITE); };
// Unlock with mutex
void Unlock() { ::ReleaseMutex(m_hMutex); };
};
static const char *SHMEM = "MyShMemory"; // the name of sharememory object
static const char *SHMEMMUTEX = "MyMutex"; // name of the mutex
SharedMemory::SharedMemory()
{
// Try to create file mapping object (assume that this is the server)
m_hMap = ::CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,
sizeof(int), // shared memory size
SHMEM);
// Check if file mapping object already exists. If it does, then this is a
// client and in this case open existing file mapping object. Client also
// needs to create a mutex object to synchronize access to the server
if (GetLastError() == ERROR_ALREADY_EXISTS) {
m_hMap = ::OpenFileMapping(FILE_MAP_WRITE,FALSE,SHMEM);
}
m_hMutex = ::CreateMutex(NULL,FALSE,SHMEMMUTEX);
// Obtain a pointer from the handle to file mapping object
_data = (int*)::MapViewOfFile(m_hMap,FILE_MAP_WRITE,0,0,sizeof(int));
}
SharedMemory::~SharedMemory()
{
if (m_hMutex)
::CloseHandle(m_hMutex);
::UnmapViewOfFile(_data);
::CloseHandle(m_hMap);
}
SharedMemory sMemory;
int GetValue(){
int *val;
sMemory.Lock();
val = sMemory.GetData();
sMemory.Unlock();
return *val;
}
void SetValue(int val){
sMemory.Lock();
sMemory.SetData(val);
sMemory.Unlock();
}
在VB中该如何调用SetValue(int val) 这个函数?
我在BCB中直接include这个C文件.工作正常.但似乎没有通过DLL.而是
两个C程序,直接交换数据.
但我用以下的方法调用DLL时,总是得不到MyDllGetValue函数.
HINSTANCE hLib;
int ( __stdcall *MyDllGetValue )( );
hLib = LoadLibrary ( "ShareValueProject.DLL" );
if ( hLib == 0 ){
MessageBox ( NULL, "DLL not found.", "Error ", MB_SYSTEMMODAL );
return;
}
MyDllGetValue = (int ( __stdcall *)( ) ) GetProcAddress ( hLib, "GetValue" );
if ( MyDllGetValue == NULL ){
MessageBox ( NULL, "error.", "dddd", MB_SYSTEMMODAL );
}
请问我的DLL写法是否有错.还是调用有错.
我的BCB的程序将通过这DLL和对方的一个VB程序交换数据.
因为我不懂VB.对DLL也是刚开始,尤其对BCB和VB程序如何共同工作很是模糊.
对方也不懂BCB.所以感到有困难.
我的BCB和DLL之间传递数据没有问题,但对方总无法读取DLL
不知我的DLL是否有问题.请高手指点.
程序如下:
1) DLL程序.shallvalue.cpp
extern "C" __declspec(dllexport)void SetValue(int val);
extern "C" __declspec(dllexport)int GetValue();
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
这是一个共享数据的class
class SharedMemory
{
int *_data;
HANDLE m_hMap;
HANDLE m_hMutex;
public:
SharedMemory();
virtual ~SharedMemory();
inline int* GetData() { return _data; };
inline void SetData(int d) { *_data = d; };
// Lock with mutex
void Lock() { ::WaitForSingleObject(m_hMutex, INFINITE); };
// Unlock with mutex
void Unlock() { ::ReleaseMutex(m_hMutex); };
};
static const char *SHMEM = "MyShMemory"; // the name of sharememory object
static const char *SHMEMMUTEX = "MyMutex"; // name of the mutex
SharedMemory::SharedMemory()
{
// Try to create file mapping object (assume that this is the server)
m_hMap = ::CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,
sizeof(int), // shared memory size
SHMEM);
// Check if file mapping object already exists. If it does, then this is a
// client and in this case open existing file mapping object. Client also
// needs to create a mutex object to synchronize access to the server
if (GetLastError() == ERROR_ALREADY_EXISTS) {
m_hMap = ::OpenFileMapping(FILE_MAP_WRITE,FALSE,SHMEM);
}
m_hMutex = ::CreateMutex(NULL,FALSE,SHMEMMUTEX);
// Obtain a pointer from the handle to file mapping object
_data = (int*)::MapViewOfFile(m_hMap,FILE_MAP_WRITE,0,0,sizeof(int));
}
SharedMemory::~SharedMemory()
{
if (m_hMutex)
::CloseHandle(m_hMutex);
::UnmapViewOfFile(_data);
::CloseHandle(m_hMap);
}
SharedMemory sMemory;
int GetValue(){
int *val;
sMemory.Lock();
val = sMemory.GetData();
sMemory.Unlock();
return *val;
}
void SetValue(int val){
sMemory.Lock();
sMemory.SetData(val);
sMemory.Unlock();
}
在VB中该如何调用SetValue(int val) 这个函数?
我在BCB中直接include这个C文件.工作正常.但似乎没有通过DLL.而是
两个C程序,直接交换数据.
但我用以下的方法调用DLL时,总是得不到MyDllGetValue函数.
HINSTANCE hLib;
int ( __stdcall *MyDllGetValue )( );
hLib = LoadLibrary ( "ShareValueProject.DLL" );
if ( hLib == 0 ){
MessageBox ( NULL, "DLL not found.", "Error ", MB_SYSTEMMODAL );
return;
}
MyDllGetValue = (int ( __stdcall *)( ) ) GetProcAddress ( hLib, "GetValue" );
if ( MyDllGetValue == NULL ){
MessageBox ( NULL, "error.", "dddd", MB_SYSTEMMODAL );
}
请问我的DLL写法是否有错.还是调用有错.