Announcement

Collapse
No announcement yet.

help with simple C-to-PB port - its finished, but somethings not right

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

  • help with simple C-to-PB port - its finished, but somethings not right

    I'm trying to port some simple C++ code over to PB. It uses file-mapping, and according to POFFS2, nobody has used the OpenEvent() API call before!
    Here is the code - very simple:
    Code:
    	HANDLE hEventGuard = OpenEvent(EVENT_ALL_ACCESS, FALSE, PB_EVENT_NAME);
    	HANDLE hFileMapping = OpenFileMapping(FILE_MAP_READ, FALSE, PB_NAME);
    	if(hEventGuard == NULL | | hFileMapping == NULL)
    	{
    		printf("Error: \n");
    		CloseHandle(hEventGuard);
    		CloseHandle(hFileMapping);
    		return;
    	}
    I was able to port it over, but the call to OpenEvent() from PB keeps returning %NULL - 0
    Does anybody know why? Here is my port:
    Code:
    %EVENT_ALL_ACCESS = &H001F0003
    hEventGuard = OpenEvent(%EVENT_ALL_ACCESS, %FALSE, PB_EVENT_NAME)
    hFileMapping = OpenFileMapping(%FILE_MAP_READ, %FALSE, PB_NAME)
    STDOUT "hEventGuard=" & STR$(hEventGuard)   'always returns 0
    STDOUT "hFileMapping=" & STR$(hFileMapping) 'also returns 0 because the call to OpenEvent failed
    CloseHandle hEventGuard
    CloseHandle hFileMapping
    Is the win32api.inc declaration ok? -
    Code:
    DECLARE FUNCTION OpenEvent LIB "KERNEL32.DLL" ALIAS "OpenEventA" (BYVAL dwDesiredAccess AS LONG, BYVAL bInheritHandle AS LONG, lpName AS ASCIIZ) AS LONG
    Here is MS VC6's declaration:
    Code:
    WINBASEAPI
    HANDLE
    WINAPI
    OpenEventA(
        DWORD dwDesiredAccess,
        BOOL bInheritHandle,
        LPCSTR lpName
        );
    WINBASEAPI
    HANDLE
    WINAPI
    OpenEventW(
        DWORD dwDesiredAccess,
        BOOL bInheritHandle,
        LPCWSTR lpName
        );
    #ifdef UNICODE
    #define OpenEvent  OpenEventW
    #else
    #define OpenEvent  OpenEventA
    #endif // !UNICODE
    Im using NT4 and Win2K so I have a feeling I should be calling the Unicode version of OpenEvent (which isn't defined)
    Any help is much appreciated!


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

  • #2
    Looks ok. Did you check GetLastError?

    OpenEventW is the Unicode definition, but I'm not clear why you want Unicode here?


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

    Comment


    • #3
      Did you use CreateEvent before trying OpenEvent? (Or else OpenEvent will fail and return 0)

      ------------------
      Peter.
      mailto[email protected][email protected]</A>
      Regards,
      Peter

      Comment


      • #4
        There's a pre-compiled C DLL that is an always-running base provider. I dont believe I need to call CreateEvent, as the event is created by the DLL not my program. My program just needs to open and read from the shared memory map
        Ive got demo source showing how to call it, and this is how it starts:
        Code:
        #define THE_EVENT_NAME		TEXT("TheSharedEvent")
         
        void _cdecl mainCRTStartup(void)
        {
        	HANDLE hEventGuard = OpenEvent((EVENT_ALL_ACCESS, FALSE, THE_EVENT_NAME);
        }
        So as soon as it starts, it immediately calls OpenEvent, and it works! OpenEvent returned a number (84 in this instance) according to the C version, but keeps returning 0 when I call it from PB, I dont understand why
        If I can get the call to OpenEvent working, then the call immediately below it to OpenFileMapping should also work which will solve all my problems

        Thankyou!


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

        Comment


        • #5
          Normally a few things can cause this:

          1) The event name isn't correct (case sensitive!)
          2) The event isn't created yet by CreateEvent.
          3) Something with the access rights isn't ok

          GetLastError should tell you what it is....

          ------------------
          Peter.
          mailto[email protected][email protected]</A>
          Regards,
          Peter

          Comment


          • #6
            GetLastError = 2
            Error Description = "The system cannot find the file specified."

            1) The event name isn't correct (case sensitive!)
            Its correct
            2) The event isn't created yet by CreateEvent.
            The event is definately created
            3) Something with the access rights isn't ok
            Im logged in as administrator so I should be able to obtain proper access

            Im spelling it correctly - case sensitively, Ive defined it as an ASCIIZ * 50
            any ideas ?



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

            Comment


            • #7
              If GetLastError returns 2 (ERROR_FILE_NOT_FOUND) then the kernel object with the specified
              name does not exist.
              Following example should work. If you start it several times, only the first one should
              create an event, the rest should open the event...
              Code:
              #Compile Exe
              #Include "win32api.inc"
              %EVENT_ALL_ACCESS = &h0001F0003
              
              Function PbMain() As Long
                Local lRet As Long, lError As Long, lEventName As Asciiz * %MAX_PATH
                lEventName = "TestEvent"
                lRet = CreateEvent (ByVal %Null, %False, %False, lEventName)
                lError =  GetLastError ()
                If IsFalse lRet Then
                  MsgBox "Unable to create Event, error:" + Str$(lError),,"CreateEvent error"
                Else
                  If lError = %ERROR_ALREADY_EXISTS Then
                    lRet = OpenEvent(ByVal %EVENT_ALL_ACCESS, %False, lEventName)
                    lError = GetLastError()
                    If lRet Then
                      MsgBox "Opened existing Event, handle:" + Str$(lRet),,"OpenEvent:"
                    Else
                      MsgBox "Unable to open Event, error:" + Str$(lError),,"OpenEvent error" : Exit Function
                    End If
                  Else
                    MsgBox "Created new Event, handle:" + Str$(lRet),,"CreateEvent:"
                  End If
                End If
              End Function
              ------------------
              Peter.
              mailto[email protected][email protected]</A>



              [This message has been edited by Peter Lameijn (edited October 16, 2001).]
              Regards,
              Peter

              Comment


              • #8
                [problem solved]

                Strewth, its working! Im not sure why though , something very subtle was obviously out. Ive tried to re-break it and now I can't, it just keeps working, sheesh
                Anyway Peter the minor mods I made were based on your code below so thanks very much, Ive never done any file mapping before, it should be interesting!
                Cheers


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

                Comment

                Working...
                X