Advertisement
Advertisement
| 06.08.2008 at 10:27AM PDT, ID: 23467412 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: |
==== C++ Implementation ====
//Header
tatic const LONG BufferSize=100;
static const LONG Buffers=1;
static const char MemoryName[]="NXFGIPC_SHMEM_0822931589443_238731_GATE0";
//static const char MemoryName[]="NXFGIPC_SHMEM_5216659004766_091723_GATE0";
static char (*Memory)[Buffers][BufferSize];
static const char WriterSemaphoreName[]="Writer Semaphore";
static const char ReaderSemaphoreName[]="Reader Semaphore";
static HANDLE hWriterSemaphore;
static HANDLE hReaderSemaphore;
static HANDLE hMemory;
//Routine to get data
hMemory=CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0
,sizeof(char [Buffers][BufferSize]),MemoryName);
Memory=(char (*)[Buffers][BufferSize])MapViewOfFile(hMemory
,FILE_MAP_WRITE,0,0,sizeof(char [Buffers][BufferSize]));
char recvData[1024];
DWORD retVal;
hWriterSemaphore=CreateSemaphore(NULL,Buffers,Buffers,WriterSemaphoreName);
if (hWriterSemaphore==NULL) {
Output->Lines->Append("Couldn't create writer semaphore");
return;
}
hReaderSemaphore=CreateSemaphore(NULL,0,Buffers,ReaderSemaphoreName);
if (hReaderSemaphore==NULL) {
Output->Lines->Append("Couldn't create reader semaphore");
CloseHandle(hWriterSemaphore);
return;
}
//In Timer loop
retVal=WaitForSingleObject(hReaderSemaphore,100);
if (retVal==0) { //Object signaled!
//Move shared memory data to our local stringbuffer
sprintf(recvData,"%s",(*Memory)[0]);
//Populate the record structure with data
sscanf(recvData,"%u,%u,%u,%u,%u,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
&isd.count,
&isd.Y, &isd.M, &isd.D,
&isd.timestamp,
&isd.TRACBearing,
&isd.TRACDistance,
&isd.RawBearing,
&isd.RawDistance,
&isd.TRACX,
&isd.TRACY,
&isd.RawX,
&isd.RawY,
&isd.StrikeType,
&isd.StrikePolarity);
}
=== VB.Net Implementation ===
Public Const MemoryName As String = "NXFGIPC_SHMEM_0822931589443_238731_GATE0"
Public Const BufferSize As Long = 100
Public Const Buffers As Long = 1
Public Const WriterSemaphoreName As String = "Writer Semaphore"
Public Const ReaderSemaphoreName As String = "Reader Semaphore"
Public hWriterSemaphore As IntPtr
Public hReaderSemaphore As IntPtr
Dim intCnt As Integer
Dim CurrentStrikeData As String 'Holds current strike infor to send to client
Private INVALID_HANDLE_VALUE As New IntPtr(&HFFFFFFFF)
Private Const PAGE_READONLY As Integer = 2
Private Const PAGE_READWRITE As Integer = 4
Private Const FILE_MAP_WRITE As Integer = 2
Private Const FILE_MAP_READ As Integer = 4
Private hMemory, hFileMap, lPointer As IntPtr
Declare Function CreateFileMapping Lib "kernel32" Alias "CreateFileMappingA" (ByVal hFile As IntPtr, _
ByRef lpFileMappigAttributes As SECURITY_ATTRIBUTES, ByVal flProtect As Integer, ByVal dwMaximumSizeHigh As Integer, _
ByVal dwMaximumSizeLow As Integer, ByVal lpName As String) As IntPtr
Declare Function MapViewOfFile Lib "kernel32" (ByVal hFileMappingObject As IntPtr, ByVal dwDesiredAccess As Long, _
ByVal dwFileOffsetHigh As Long, ByVal dwFileOffsetLow As Long, _
ByVal dwNumberOfBytesToMap As Long) As IntPtr
Public Const WAIT_TIMEOUT As Long = &H102
Public Declare Function CreateSemaphore Lib "kernel32" Alias "CreateSemaphoreA" (<MarshalAs(UnmanagedType.Struct)> ByRef lpSemaphoreAttributes As SECURITY_ATTRIBUTES, _
ByVal lInitialCount As Integer, ByVal lMaximumCount As Integer, ByVal lpName As String) As IntPtr
Public Declare Function OpenSemaphore Lib "kernel32" Alias "OpenSemaphoreA" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, _
ByVal lpName As String) As Integer
Public Declare Function WaitForSingleObject Lib "kernel32" Alias "WaitForSingleObject" (ByVal hHandle As IntPtr, ByVal dwMilliseconds As Integer) As Integer
Public Declare Function ReleaseSemaphore Lib "kernel32" Alias "ReleaseSemaphore" (ByVal hSemaphore As IntPtr, ByVal lReleaseCount As Integer, _
ByVal lpPreviousCount As Integer) As Integer
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal dest As String, _
ByVal source As IntPtr, ByVal bytes As Long)
'//In a form
hMemory = CreateFileMapping(INVALID_HANDLE_VALUE, Nothing, PAGE_READWRITE, 0, 100, MemoryName)
If hMemory = Nothing Or hMemory = IntPtr.Zero Then
Console.WriteLine("hMemory is nothing")
End If
lPointer = MapViewOfFile(hMemory, FILE_MAP_WRITE, 0, 0, 100)
If lPointer = Nothing Or hMemory = IntPtr.Zero Then
Console.WriteLine("lPointer is nothing")
End If
'//In timer routine
Dim retVal As Integer
Dim ipc As New IPC_STRIKEDATA 'a Structure (in modMain)
Dim cb As IntPtr
Dim somestring As String
Dim MemString As String
somestring = " "
MemString = somestring.PadRight(99)
hWriterSemaphore = CreateSemaphore(Nothing, 100, 100, WriterSemaphoreName)
If hWriterSemaphore = Nothing Or hWriterSemaphore = IntPtr.Zero Then
Console.WriteLine("Couldn't create writer semaphore")
Exit Sub
End If
hReaderSemaphore = CreateSemaphore(Nothing, 0, 100, ReaderSemaphoreName)
If hReaderSemaphore = Nothing Or hReaderSemaphore = IntPtr.Zero Then
Console.WriteLine("Couldn't create reader semaphore")
Exit Sub
End If
'Wait 100 ms for state signal
retVal = WaitForSingleObject(hReaderSemaphore, 100)
If retVal = 0 Then
Try
CopyMemory(MemString, lPointer, 100)
tbResults.Text = tbResults.Text & "MemString=" & MemString & vbCrLf
Catch ex As ExternalException
Console.WriteLine("Copy mem problem")
End Try
End If
'Release semaphores just used...
ReleaseSemaphore(hReaderSemaphore, 1, Nothing)
ReleaseSemaphore(hWriterSemaphore, 1, Nothing)
|