Delphi Assigned函数使用

在Delphi中,Assigned函数用来判断一个指针是否已经指向了一个对象。

语法:

function Assigned(const P: Pointer): Boolean;

参数:

  • P:要判断的指针变量。

返回值:

  • True:指针不为空,且已经指向了一个对象。
  • False:指针为空,或者指针指向的对象为nil。

示例用法:

var
  Obj: TObject;
begin
  Obj := TObject.Create;
  try
    if Assigned(Obj) then
      ShowMessage('Obj is assigned.')
    else
      ShowMessage('Obj is not assigned.');
  finally
    Obj.Free;
  end;
end;

上述代码中,首先创建了一个TObject对象,并将其赋值给Obj变量。然后使用Assigned函数判断Obj是否已经指向了一个对象,如果是,则显示"Obj is assigned.“;否则显示"Obj is not assigned.”。最后通过Obj.Free释放对象的内存空间。

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的例子,演示如何在 Delphi 中调用 Python 编写的 DLL 中的自定义函数: 首先,我们需要有一个 Python 脚本,其中包含我们想要在 Delphi 中调用的自定义函数。例如,我们可以创建一个名为 `my_module.py` 的文件,其中包含以下代码: ```python def add_numbers(a, b): return a + b ``` 接下来,我们需要使用 Python 的 `ctypes` 模块将该脚本转换为 DLL。我们可以在 Python 中使用以下代码完成: ```python from ctypes import * # Load the Python DLL python_dll = CDLL("python37.dll") # Load our Python module my_module = python_dll.PyImport_ImportModule("my_module") # Get a reference to our function add_numbers_func = python_dll.PyObject_GetAttrString(my_module, "add_numbers") # Define the argument types for our function add_numbers_func.argtypes = [c_int, c_int] # Define the return type for our function add_numbers_func.restype = c_int # Export our function as a DLL add_numbers_dll = add_numbers_func._as_parameter_ ``` 现在我们已经将 Python 脚本转换为 DLL,并且我们有了对自定义函数的引用。接下来,我们可以在 Delphi 代码中使用以下代码来调用该函数: ```delphi type TAddNumbersFunc = function(a, b: Integer): Integer; stdcall; var PythonDLL: THandle; AddNumbersFunc: TAddNumbersFunc; ResultValue: Integer; begin // Load the Python DLL PythonDLL := LoadLibrary('my_module.dll'); if PythonDLL = 0 then raise Exception.Create('Failed to load Python DLL'); try // Get a reference to our function @AddNumbersFunc := GetProcAddress(PythonDLL, 'add_numbers'); if not Assigned(AddNumbersFunc) then raise Exception.Create('Failed to get function address'); // Call our function ResultValue := AddNumbersFunc(1, 2); ShowMessageFmt('Result: %d', [ResultValue]); finally FreeLibrary(PythonDLL); end; end; ``` 这个例子演示了如何在 Delphi 中调用 Python 编写的 DLL 中的自定义函数。需要注意的是,我们在 Delphi使用函数名必须与 Python 中使用函数名相同。此外,我们还需要正确设置参数类型和返回类型,以便 Delphi 可以正确地调用 Python 函数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值