Advertisement

06.08.2008 at 10:27AM PDT, ID: 23467412
[x]
Attachment Details

Problem with VB version of C++ Shared Memory routine

Asked by stelmat in Microsoft Visual Basic.Net

Tags: VB .Net

Attached is the C++ code and my feable attempt at a VB.Net version.  My code executes but it has two problems:
1. It does not return anything
2. The semaphore always returns zero (and I though it would not unless the semaphore name was found)

The data I am trying to get is put there by another application.  I am sure I am off in left field, but this is the only part of the code I am writing that I don't have a handle on.Start Free Trial
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)
[+][-]06.09.2008 at 01:47PM PDT, ID: 21746537

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.09.2008 at 04:58PM PDT, ID: 21747581

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.10.2008 at 04:43AM PDT, ID: 21750187

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Microsoft Visual Basic.Net
Tags: VB .Net
Sign Up Now!
Solution Provided By: graye
Participating Experts: 1
Solution Grade: B
 
 
[+][-]06.10.2008 at 04:32PM PDT, ID: 21755868

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628