Announcement

Collapse
No announcement yet.

Accessing C array type from PB - looks easy enough?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing C array type from PB - looks easy enough?

    I think this is a simple problem , but Ill include full source to help explain it better.

    Im porting some C source over to PB, Ive basically finished and am proud to say its working well so far but it was frustrating to find a barrier at the very end! - it successfully opens a mapped file and gets the correct address etc, now all I need to do is display the result. It looks very simple to do in C, but my attempts to do it in PB have not been successful
    The part im stuck on is highlighted in bold. I need to retrieve the data (a resultcode), each piece of data is in an array of that type though so Im not sure how to access it!
    Can anybody help me to get this port finished please? This is all I need to do now

    Code:
    typedef struct _SHARED_INFO
    {
     LONG lResultCode;
    } SHARED_INFO;
    typedef SHARED_INFO * PSHARED_INFO;
    typedef SHARED_INFO * LPSHARED_INFO;
     
    void DisplayResultCode(PSHARED_INFO pElement)
    {
     kprintf("%d", pElement->lResultCode);
    }
     
    void _cdecl mainCRTStartup(void)
    {
    	HANDLE hEventGuard = OpenEvent(EVENT_ALL_ACCESS, FALSE, SHAREDINFO_EVENT_NAME);
    	HANDLE hFileMapping = OpenFileMapping(FILE_MAP_READ, FALSE, SHAREDINFO_NAME);
    	if(hEventGuard == NULL | | hFileMapping == NULL)
    	{
    		kprintf("Error: Program not started. Start and try again.\n");
    		CloseHandle(hEventGuard);
    		CloseHandle(hFileMapping);
    		return;
    	}
     	PSHARED_INFO pLocalInformation = (PSHARED_INFO)HeapAlloc(GetProcessHeap(), 0, SHAREDINFO_BASE_SIZE);
     	if(pLocalInformation)
    	{
    		PVOID pRemoteInformation = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
    		if(pRemoteInformation)
    		{
    			if(WaitForSingleObject(hEventGuard, INFINITE) == WAIT_OBJECT_0)
    			{
    				ResetEvent(hEventGuard);
    				//
    				// here is a replacement of RTL memcpy(..)
    				// memcpy(pLocalInformation, pRemoteInformation, SHAREDINFO_BASE_SIZE);
    				//
    				__asm {
    					align 4
    					mov ecx, SOCKETMON_BASE_SIZE
    					mov ebx, ecx
    					and ebx, 3
    					shr ecx, 2
    					mov esi, pRemoteInformation
    					mov edi, pLocalInformation
    					cld
    					rep movsd
    					mov ecx, ebx
    					rep movsb
    				}
    				SetEvent(hEventGuard);
    			}
    			UnmapViewOfFile(pRemoteInformation);
    		}
    		for(int i = 0; i < SHAREDINFO_BASE_COUNT; i++)
    		{
    				DisplayResultCode(&pLocalInformation[i]);
    		}
    		HeapFree(GetProcessHeap(), 0, pLocalInformation);
    	}
    	else
    	kprint("Error allocating memory.\n");
    	CloseHandle(hEventGuard);
    	CloseHandle(hFileMapping);
    }
    Also, I should mention that I used MoveMemory rather than the memcpy asm version in the C source, is that ok to use?

    Many thanks!

    [This message has been edited by Wayne Diamond (edited October 16, 2001).]
    -

  • #2
    Code:
        LOCAL i AS LONG
        DO WHILE i < %SHAREDINFO_BASE_COUNT
            DisplayResultCode BYVAL VARPTR(pLocalInformation(i))
            INCR i
        LOOP
    MoveMemory is a fine replacement for memcpy.

    You may be able to do without the BYVAL VARPTR, depending on how
    you declared DisplayResultCode.

    ------------------
    Tom Hanlin
    PowerBASIC Staff

    Comment


    • #3
      Wayne,

      The thing to remember in dealing with C/C++ arrays is that
      they ALWAYS start at zero (ie 0).

      Thus, a five element long array would look like this:

      long whatever[5];

      This describes a five element array with the starting
      descripter of 0 and ending at 4. With that in mind, one
      can also use Tom's code as a FOR/NEXT loop:

      Code:
          LOCAL i AS LONG
          FOR i = 0 TO %SHAREDINFO_BASE_COUNT -1 
              DisplayResultCode BYVAL VARPTR(pLocalInformation(i))
          NEXT
      Either way works just fine!!!

      Cheers,
      Cecil



      ------------------

      Comment


      • #4
        Hrmm thanks very much Tom, Cecil, that looks about right
        Cecil what is your email address? I have something to send you that i think youll like


        ------------------
        -

        Comment


        • #5


          [email protected]



          ------------------

          Comment

          Working...
          X