C 是否可以将附加ruby ffi的函数设置为私有?
我有一个共享对象库,使用ruby从中附加函数。我想给每个函数附加一个别名,并将别名设置为私有,因为调用它们可能会很危险。我将每个函数包装在自己的ruby模块函数中,下面是一个快速示例:C 是否可以将附加ruby ffi的函数设置为私有?,c,ruby,shared-libraries,ffi,ruby-ffi,C,Ruby,Shared Libraries,Ffi,Ruby Ffi,我有一个共享对象库,使用ruby从中附加函数。我想给每个函数附加一个别名,并将别名设置为私有,因为调用它们可能会很危险。我将每个函数包装在自己的ruby模块函数中,下面是一个快速示例: module LibC extend FFI::Library ffi_lib FFI::Library::LIBC attach_function :free, [:pointer], :void end module MyModule class << sel
module LibC
extend FFI::Library
ffi_lib FFI::Library::LIBC
attach_function :free, [:pointer], :void
end
module MyModule
class << self
extend FFI::Library
ffi_lib '../my_shared_lib.so'
def function(str)
is_string(str)
ptr = ffi_function(str)
result = String.new(ptr.read_string)
LibC.free(ptr)
result
end
private
# attach function
attach_function :ffi_function, :function, [:string], :pointer
def is_string(object)
unless object.kind_of? String
raise TypeError,
"Wrong argument type #{object.class} (expected String)"
end
end
end
end
模块LibC
扩展FFI::库
ffi_lib ffi::Library::LIBC
附加函数:free,[:指针],:void
终止
模块MyModule
类将attach_函数放在类之外
require 'ffi'
module Win32
extend FFI::Library
ffi_lib 'Psapi'
ffi_convention :stdcall
=begin
BOOL WINAPI EnumProcesses(
_Out_ DWORD *pProcessIds,
_In_ DWORD cb,
_Out_ DWORD *pBytesReturned
);
=end
attach_function :EnumProcesses, [:pointer, :uint32, :pointer], :int
class << self
def process_ids
# Allocate room for the windows process ids.
windows_process_ids = FFI::MemoryPointer.new(:uint32, 1024)
# Allocate room for windows to tell us how many process ids there were.
bytes_returned = FFI::MemoryPointer.new(:uint32)
# Ask for the process ids
if EnumProcesses(windows_process_ids, windows_process_ids.size, bytes_returned) != 0
# Determine the number of ids we were given
process_ids_returned = bytes_returned.read_int / bytes_returned.size
# Pull all the ids out of the raw memory and into a local ruby array.
windows_process_ids.read_array_of_type(:uint32, :read_uint32, process_ids_returned)
end
end
# Hide EnumProcesses from the outside
private :EnumProcesses
end
end
Win32.process_ids # This will work
Win32.EnumProcesses # This throws an exception: private method `EnumProcesses' called for Win32:Module (NoMethodError)