[Gambas-user] External C function usage error - IPC queue usage

Yahoo olivier.cruilles at yahoo.fr
Mon Jan 22 19:41:29 CET 2018


I think I’m becoming crazy with this little code to use an external function in the ‘libc’ library.

I try to manipulate the 'IPC queue' over gambas using 'msgsnd(...)' function but after hours I'm not able to pass it the necessary parameters.

Does anyone can help me please ?

This is the Gambas code:

' Gambas module file

' Error return codes
Public Enum
  IPC_OK = 0,
  IPC_ERROR = -1

Public Enum
  IPC_CREAT = 512,
  IPC_EXCL = 1024,
  IPC_NOWAIT = 2048,
  IPC_PRIVATE = 234564


' IPC Message structure
Public Struct msg_buf
  mtype As Integer       ' Message type
  mtext As String        ' Message text
End Struct

Library "libc:6"
Public Extern ftok(pathName As String, proj_id As String) As Pointer
Public Extern msgget(key As Integer, msgflg As Integer) As Integer
Public Extern msgsnd(msqid As Integer, MsgPointer As Msg_buf, msgsz As Integer, msgflg As Integer) As Integer
' int msgsnd (int __msqid, const void *__msgp, size_t __msgsz,int __msgflg)

Public Extern msgrcv(msqid As Integer, MsgPointer As Msg_buf, msgsz As Integer, type As Long)


Public MSGPERM As Integer = 0600
Public MSGTXTLEN As Integer = 40
Public MSG_NOERROR As Integer = 4096

Public msgqid As Integer
Public rc As Integer
Public done As Integer

Public msg As New Msg_buf


' ------------------------------------------------------------------------------------------------------------------

Public Sub Main()

  Dim pMesskey As Pointer

  Print "Cleanup IPC queue IPC..."
  Shell "for i in $(ipcs -q | awk '{print $2}' | egrep -v 'msqid|Files|^$'); do ipcrm -q $i; done ; ipcs -q" Wait

  ' Creation of the IPC queue
  pMesskey = ftok(".", "m")

  msgqid = msgget(pMesskey, (MSGPERM Or IPC_CREAT))
  If msgqid < 0 Then
    Print "Failed to create message queue with msgqid = " & msgqid
    Quit 1
  End If

  Print "Message queue " & msgqid & " created"

  ' Fill the Structure
  msg.mtype = 1
  msg.mtext = "A message..."

  ' Send the message to the IPC queue
  rc = send_message(msgqid, msg, 1) 
  If rc < 0 Then
    Print "msgsnd failed, rc=" & rc
    Quit 1
  Endif

End


Private Function send_message(qid As Integer, msg As Msg_buf, type As Integer) As Integer

  ' Function to send a message to an IPC queue by it 'qid'

  Dim res As Integer
  Dim LenText As Integer

  Print "Sending a message to queue[" & qid & "] Lenght[" & LenText & "]"

  res = msgsnd(qid, msg, LenText, 0) ' The last param can be: 0, IPC_NOWAIT, MSG_NOERROR, or IPC_NOWAIT|MSG_NOERROR.

  If res < 0 Then
    Print "Error to send message. Code result: " & res
    Quit 1
  Endif

  Return IPC_OK

End

================================

Thank you


Olivier Cruilles

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20180122/5d995341/attachment.html>


More information about the User mailing list