我如何(或者我不能)使用简单的DLL中的变体? [英] How do I (or if I can't) use Variants on simple DLLs?

查看:162
本文介绍了我如何(或者我不能)使用简单的DLL中的变体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将内部对象的一些功能公开为一个DLL,但是这个功能使用变体。但是我需要知道:我可以使用Variant参数和/或返回导出一个函数 - 或者更好地转到一个仅字符串的表示?



从语言不可知的POV(消费者不是用Delphi制作的,但都会在Windows中运行),更好的是什么?

解决方案

您可以使用OleVariant,它是COM使用的变体值类型。
确保不将其作为函数结果返回为stdcall,复杂的结果类型很容易导致问题。



一个简单的例子
库DelphiLib ;

 使用
SysUtils,
DateUtils,
变体;

procedure GetVariant(aValueKind:Integer; out aValue:OleVariant); STDCALL;出口;
var
doubleValue:Double;
begin
case aValueKind of
1:aValue:= 12345;
2:
begin
doubleValue:= 13984.2222222222;
aValue:= doubleValue;
结束
3:aValue:= EncodeDateTime(2009,11,3,15,30,21,40);
4:aValue:= WideString('Hello');
else
aValue:= Null();
结束
结束

export
GetVariant;






如何从C# p>

  public enum ValueKind:int 
{
Null = 0,
Int32 = 1,
Double = 2,
DateTime = 3,
String = 4
}

[DllImport(YourDelphiLib,
EntryPoint =GetVariant )]
static extern void GetDelphiVariant(ValueKind valueKind,out Object value);

static void Main()
{
对象delphiInt,delphiDouble,delphiDate,delphiString;

GetDelphiVariant(ValueKind.Int32,out delphiInt);
GetDelphiVariant(ValueKind.Double,out delphiDouble);
GetDelphiVariant(ValueKind.DateTime,out delphiDate);
GetDelphiVariant(ValueKind.String,out delphiString);
}


I want to expose some functionality of a internal object as a DLL - but that functionality uses variants. But I need to know: I can export a function with Variant parameters and/or return - or is better to go to an string-only representation?

What is better, from language-agnostic POV (the consumer is not made with Delphi - but all will run in Windows)?

解决方案

You could use OleVariant, which is the variant value type that is used by COM. Make sure not to return it as a function result as stdcall and complex result types can easily lead to problems.

A simple example library DelphiLib;

uses
  SysUtils,
  DateUtils,
  Variants;

procedure GetVariant(aValueKind : Integer; out aValue : OleVariant); stdcall; export;
var
  doubleValue : Double;
begin
  case aValueKind of
    1: aValue := 12345;
    2:
    begin
      doubleValue := 13984.2222222222;
      aValue := doubleValue;
    end;
    3: aValue := EncodeDateTime(2009, 11, 3, 15, 30, 21, 40);
    4: aValue := WideString('Hello');
  else
    aValue := Null();
  end;
end;

exports
  GetVariant;


How it could be consumed from C#:

public enum ValueKind : int
{
   Null = 0,
   Int32 = 1,
   Double = 2,
   DateTime = 3,
   String = 4
}

[DllImport("YourDelphiLib",
           EntryPoint = "GetVariant")]
static extern void GetDelphiVariant(ValueKind valueKind, out Object value);

static void Main()
{
   Object delphiInt, delphiDouble, delphiDate, delphiString;

   GetDelphiVariant(ValueKind.Int32, out delphiInt);
   GetDelphiVariant(ValueKind.Double, out delphiDouble);
   GetDelphiVariant(ValueKind.DateTime, out delphiDate);
   GetDelphiVariant(ValueKind.String, out delphiString);
}

这篇关于我如何(或者我不能)使用简单的DLL中的变体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆