    # File lib/ffi/library.rb, line 305
        def attach_variable(mname, a1, a2 = nil)
          cname, type = a2 ? [ a1, a2 ] : [ mname.to_s, a1 ]
          address = nil
          ffi_libraries.each do |lib|
            begin
              address = lib.find_variable(cname.to_s)
              break unless address.nil?
            rescue LoadError
            end
          end

          raise FFI::NotFoundError.new(cname, ffi_libraries) if address.nil? || address.null?
          if type.is_a?(Class) && type < FFI::Struct
            # If it is a global struct, just attach directly to the pointer
            s = type.new(address)
            self.module_eval "@@ffi_gvar_\#{mname} = s\ndef self.\#{mname}\n@@ffi_gvar_\#{mname}\nend\n", __FILE__, __LINE__

          else
            sc = Class.new(FFI::Struct)
            sc.layout :gvar, find_type(type)
            s = sc.new(address)
            #
            # Attach to this module as mname/mname=
            #
            self.module_eval "@@ffi_gvar_\#{mname} = s\ndef self.\#{mname}\n@@ffi_gvar_\#{mname}[:gvar]\nend\ndef self.\#{mname}=(value)\n@@ffi_gvar_\#{mname}[:gvar] = value\nend\n", __FILE__, __LINE__

          end

          address
        end