我在Vb中写了一个Com,现在在Dephi中调用。我在VB中声明用object,
用Dephi导入,发现Dephi中用IDispatch与其对应,但是该类型不对。请问Dephi中什么类型与它对应,OleVariant不行。其它用什么?

解决方案 »

  1.   

    Q:  How Do I pass the following struct from VB to a Delphi created DLL **by reference** would be appreciated (what should the parameters on the Delphi side look like?).  I chose this structure to sort of represent most scenarios (if I can successfully pass this one, anything should be OK)Type MyType
       Value1 As Double   Value2 As Integer
       Value3 As String * 20
       Value4 As String
    End Type
    A:  You will have some trouble with Value4. Passing VB strings to/from DLLs takes special handling. The easiest way to do it is to pass a pre-declared VB string to the DLL and have the DLL return the length and the resulting string back.The others are easy.  Value1 As Double      ->  Value1 : Longint;
      Value2 AS Integer     ->  Value2 : integer;
      Value3 As String * 20 ->  Value3 : array[0..19] of char;User defined structures in Pascal are called records.  type
        myStructure = record
          Value1 : longint;
          Value2 : integer;
          Value3 : array [0..19] of char;
        end;I think you could define Value4 as a PChar in Pascal, but when you got it back in VB, you would have to search it for the ASCII 0 end byte and change the length of your string to the number of characters before the zero.If I remeber correctly, from my evaluation 'beta' copy with source code, there were some functions in the VCL that handle VB strings. Can't say if they are in the shipping version because I havn't recieved the VCL source yet. A quick search of the online docs don't reveal them.Here's some general type conversions from VB to Pascal (in table form; I hope this formats correctly on CIS.): VB Declare As        VB Call With               Translates to Pascal Type
     -------------        ------------               -------------------------
     By Val S As String   Any String or Variant      PChar
     I As Integer         Any Integer                ^Integer (pointer to integer)
     L As Long            Any Long                   ^Longint (pointer to longint) S As Rect            Any Variable of same type  ^TRect   (pointer to TRect)
     By Val As Integer    Any Integer                BOOL (word boolean)
      "   "   "                                      Word
      "   "   "                                      Integer
      "   "   "                                      hWnd
      "   "   "                                      hDC
      "   "   "                                      ...and so on; all word types.
     By Val As Long       Any Long                   Longint I As Integer         the first element of I(0)  ^array of integer
     As Any               Any Variable (By Val when
                          String)                    Pointer (PChar when string)
     As Any               By Val 0&                  nil
    Q:  This would probably work, but I'd still really like to know how to return a string (even a null-terminated one) from a DLL function to VB.A:  The problem with returning PChars from DLLs is that the DLL has to be responsible for cleaning up the memory, but it doesn't know when the caller finishes.  Most people use the following style, which is NOT bullet proof, but works in most instances: Var ReturnBuffer :Array [0..255] of Char;  { Must be outside function! } function Dir_Get(ACaption, AInitDir, DirText: PChar ) : PChar;
     Begin
      ...
      Result := StrPCopy(ReturnBuffer, 'Result Text');
     End;The only time you have a problem is when someone calls the function while someone else is still using the ReturnBuffer.  (Just about guaranteed not to happen, except maybe under Windows 95, but could under 3.1 if VB doesn't copy the string).
      

  2.   

    我声明的是Object,在Dephi中该怎么对应。Dephi引入的时候,它对Com中的参数声明为Object用IDispatch代替。
    public function AddCtl(byref ctl as Object) as boolenend function在Dephi中
    function AddCtl(var ctl : IDispatch): WordBool; safecall;
    我该怎么办?
      

  3.   

    在VB中声明为ByVal的数值传递已经可以了
    public function AddCtl(ByVal ctl as Object) as boolenend function在Dephi中
    function AddCtl(const ctl : IDispatch): WordBool; safecall;
    在VB中声明为ByRef的地址传递还是不行。
      

  4.   

    如果在VB中用ByRef来声明变量,那么在Dephi中就会出现
    [Error] Unit1.pas(36): Types of actual and formal var parameters must be identical
    我该怎么办?请大家帮忙。