hicollins
December 19th, 2007, 07:45 AM
I am using a shared memory DLL which contains data and functions to access it. I have inherited this code from another programmer and am having difficulty utilizing it.
I have three files, the first contains the code for the shared dll and the functions to access it. The second file contains code that writes to the shared memory. The third file contains code that reads the shared memory.
The first file, shared dll utilizes a flag "IsLabviewReady" that is set to false after data has been written to the shared memory and is set in the put function.
Code for first file:
#include "stdafx.h"
#include <iostream>
using namespace std;
#define STRING_SIZE 12
// externals
extern "C" __declspec(dllexport) void put(char aemString[]);
extern "C" __declspec(dllexport) bool isAEMMessageReady();
extern "C" __declspec(dllexport) char get();
extern "C" __declspec(dllexport) bool getReady();
extern "C" __declspec(dllexport) int getMyCounter();
// define shared memory (no complex types)
#pragma data_seg(".SHARED")
bool isLabviewReady = true;
char sharedAEMString[STRING_SIZE] = "";
int count = 0;
int myCounter = 0;
#pragma data_seg()
#pragma comment(linker, "/section:.SHARED,rws")
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
return TRUE;
}
char get()
{
count++;
if(count == 12)
{
isLabviewReady = true;
count = 0;
return sharedAEMString[11];
}
return sharedAEMString[count-1];
}
bool isAEMMessageReady()
{
return !isLabviewReady;
}
bool getReady()
{
return isLabviewReady;
}
void put(char aemString[])
{
for(int i=0; i<STRING_SIZE; i++)
{
sharedAEMString[i] = aemString[i];
}
isLabviewReady = false;
myCounter++;
}
int getMyCounter()
{
return myCounter;
}
The second file writes a 12 character string to the shared memory and sets the "isLabviewReady" flag to false.
Code for the second file:
#include "stdafx.h"
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
// dll functions
typedef bool (*ready)();
typedef void (*put)(char*);
// dll
HINSTANCE hinstLib;
put putFunction;
ready getReady;
void DLLFreeLibrary()
{
FreeLibrary(hinstLib);
}
void main(int argc, char* argv[])
{
char message[12] = "abcdfeghijk";
hinstLib = LoadLibrary("C:\\WINDOWS\\system32\\AEM2LV.dll");
if (hinstLib == NULL)
{
printf("ERROR: unable to load DLL \n");
return ;
}
getReady = (ready)GetProcAddress(hinstLib, "getReady");
if (getReady == NULL)
{
printf("ERROR: unable to find getIsLabviewReady() DLL function\n");
return;
}
putFunction = (put)GetProcAddress(hinstLib, "put");
if (putFunction == NULL)
{
printf("ERROR: unable to find put() DLL function\n");
return;
}
if(getReady())
{
putFunction(message);
printf("Message sent with: %s\n", message);
}
return;
}
The third file attempts to read the data written to the shared memory by the second file. The problem is that the "isLabviewReady" flag is not set to false as it should have been by the second file. It seems as if the third file gets a fresh new copy of the shared dll code.
Code for the third file:
#include "stdafx.h"
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <iostream>
using namespace std;
// dll functions
typedef bool (*MessageReady)();
typedef char (*get)();
typedef bool (*ready)();
// dll
HINSTANCE hinstLib;
get getFunction;
MessageReady isAEMMessageReady;
ready getReady;
void DLLFreeLibrary()
{
FreeLibrary(hinstLib);
}
void main(int argc, char* argv[])
{
bool doContinue = true;
char message[12] = "\0";
int i;
printf("Entering ReadData2\n");
hinstLib = LoadLibrary("C:\\WINDOWS\\system32\\AEM2LV.dll");
if (hinstLib == NULL)
{
printf("ERROR: unable to load DLL \n");
return ;
}
isAEMMessageReady = (MessageReady)GetProcAddress(hinstLib, "isAEMMessageReady");
if (isAEMMessageReady == NULL)
{
printf("ERROR: unable to find isAEMMessageReady() DLL function\n");
return;
}
getFunction = (get)GetProcAddress(hinstLib, "get");
if (getFunction == NULL)
{
printf("ERROR: unable to find get() DLL function\n");
return;
}
getReady = (ready)GetProcAddress(hinstLib, "getReady");
if (getReady == NULL)
{
printf("ERROR: unable to find getIsLabViewReady() DLL function\n");
return;
}
if (getReady())
printf("\nGR The value is true\n");
else
printf("\nGR The value is false\n");
if (isAEMMessageReady())
printf("\nMR The value is true\n");
else
printf("\nMR The value is false\n");
if (isAEMMessageReady())
{
printf("Message is ready\n");
for (i=0; i<12; i++)
{
message[i] = getFunction();
cout<< "Character was:" << message[i] << endl;
printf("Character was %c\n", message[i]);
}
}
return;
}
Both the second and third files are compiles as console applications. I run the second file, which should write to the shared memory and exits. I then try to run the third file to read the data from the shared memory, but it fails.
Any idea's what I'm doing wrong. In both applications, I load the library with the LoadLibrary call and I also use the GetProcAddress function to map the functions within the shared dll.
Any help would be appreciated!
I have three files, the first contains the code for the shared dll and the functions to access it. The second file contains code that writes to the shared memory. The third file contains code that reads the shared memory.
The first file, shared dll utilizes a flag "IsLabviewReady" that is set to false after data has been written to the shared memory and is set in the put function.
Code for first file:
#include "stdafx.h"
#include <iostream>
using namespace std;
#define STRING_SIZE 12
// externals
extern "C" __declspec(dllexport) void put(char aemString[]);
extern "C" __declspec(dllexport) bool isAEMMessageReady();
extern "C" __declspec(dllexport) char get();
extern "C" __declspec(dllexport) bool getReady();
extern "C" __declspec(dllexport) int getMyCounter();
// define shared memory (no complex types)
#pragma data_seg(".SHARED")
bool isLabviewReady = true;
char sharedAEMString[STRING_SIZE] = "";
int count = 0;
int myCounter = 0;
#pragma data_seg()
#pragma comment(linker, "/section:.SHARED,rws")
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
return TRUE;
}
char get()
{
count++;
if(count == 12)
{
isLabviewReady = true;
count = 0;
return sharedAEMString[11];
}
return sharedAEMString[count-1];
}
bool isAEMMessageReady()
{
return !isLabviewReady;
}
bool getReady()
{
return isLabviewReady;
}
void put(char aemString[])
{
for(int i=0; i<STRING_SIZE; i++)
{
sharedAEMString[i] = aemString[i];
}
isLabviewReady = false;
myCounter++;
}
int getMyCounter()
{
return myCounter;
}
The second file writes a 12 character string to the shared memory and sets the "isLabviewReady" flag to false.
Code for the second file:
#include "stdafx.h"
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
// dll functions
typedef bool (*ready)();
typedef void (*put)(char*);
// dll
HINSTANCE hinstLib;
put putFunction;
ready getReady;
void DLLFreeLibrary()
{
FreeLibrary(hinstLib);
}
void main(int argc, char* argv[])
{
char message[12] = "abcdfeghijk";
hinstLib = LoadLibrary("C:\\WINDOWS\\system32\\AEM2LV.dll");
if (hinstLib == NULL)
{
printf("ERROR: unable to load DLL \n");
return ;
}
getReady = (ready)GetProcAddress(hinstLib, "getReady");
if (getReady == NULL)
{
printf("ERROR: unable to find getIsLabviewReady() DLL function\n");
return;
}
putFunction = (put)GetProcAddress(hinstLib, "put");
if (putFunction == NULL)
{
printf("ERROR: unable to find put() DLL function\n");
return;
}
if(getReady())
{
putFunction(message);
printf("Message sent with: %s\n", message);
}
return;
}
The third file attempts to read the data written to the shared memory by the second file. The problem is that the "isLabviewReady" flag is not set to false as it should have been by the second file. It seems as if the third file gets a fresh new copy of the shared dll code.
Code for the third file:
#include "stdafx.h"
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <iostream>
using namespace std;
// dll functions
typedef bool (*MessageReady)();
typedef char (*get)();
typedef bool (*ready)();
// dll
HINSTANCE hinstLib;
get getFunction;
MessageReady isAEMMessageReady;
ready getReady;
void DLLFreeLibrary()
{
FreeLibrary(hinstLib);
}
void main(int argc, char* argv[])
{
bool doContinue = true;
char message[12] = "\0";
int i;
printf("Entering ReadData2\n");
hinstLib = LoadLibrary("C:\\WINDOWS\\system32\\AEM2LV.dll");
if (hinstLib == NULL)
{
printf("ERROR: unable to load DLL \n");
return ;
}
isAEMMessageReady = (MessageReady)GetProcAddress(hinstLib, "isAEMMessageReady");
if (isAEMMessageReady == NULL)
{
printf("ERROR: unable to find isAEMMessageReady() DLL function\n");
return;
}
getFunction = (get)GetProcAddress(hinstLib, "get");
if (getFunction == NULL)
{
printf("ERROR: unable to find get() DLL function\n");
return;
}
getReady = (ready)GetProcAddress(hinstLib, "getReady");
if (getReady == NULL)
{
printf("ERROR: unable to find getIsLabViewReady() DLL function\n");
return;
}
if (getReady())
printf("\nGR The value is true\n");
else
printf("\nGR The value is false\n");
if (isAEMMessageReady())
printf("\nMR The value is true\n");
else
printf("\nMR The value is false\n");
if (isAEMMessageReady())
{
printf("Message is ready\n");
for (i=0; i<12; i++)
{
message[i] = getFunction();
cout<< "Character was:" << message[i] << endl;
printf("Character was %c\n", message[i]);
}
}
return;
}
Both the second and third files are compiles as console applications. I run the second file, which should write to the shared memory and exits. I then try to run the third file to read the data from the shared memory, but it fails.
Any idea's what I'm doing wrong. In both applications, I load the library with the LoadLibrary call and I also use the GetProcAddress function to map the functions within the shared dll.
Any help would be appreciated!