AI muốn lập trình DLL cho Excel và các loại bằng Delphi thì xem video này nhé!

Liên hệ QC

Nguyễn Duy Tuân

Nghị Hách
Thành viên danh dự
Tham gia
13/6/06
Bài viết
4,649
Được thích
10,138
Giới tính
Nam
Nghề nghiệp
Giáo viên, CEO tại Bluesofts
Lần chỉnh sửa cuối:
Thế mà tui thấy cậu Kiều Mạnh đòi nhảy thẳng đi nhậu luôn đó.
Đô được 2 3 lon kg Mạnh, hôm nào đi nhậu với tui ?
Từ xưởng tui xuống nhà cậu khoảng vài cây, 5 10 phút à
 
Lần chỉnh sửa cuối:
Upvote 0
Vì mình dùng hàm TypeName trong DLL nên bắt buộc phải loadlibrary mới lấy được con trỏ hàm đó. Nếu tự viết hàm này đùng như TypeName mình chưa tự viết được :)
Cái vụ này em cũng đi theo hướng IDispatch và vartypeinfo không biết mò ra không ( đôi với yêu cầu của em thì em ăn gian ok rồi )
 
Upvote 0
Cái vụ này em cũng đi theo hướng IDispatch và vartypeinfo không biết mò ra không ( đôi với yêu cầu của em thì em ăn gian ok rồi )

A cũng có hướng dùng cái đó nhưng chưa thử làm. Hàm em viết cí lấy được kiểu biến như String, Double (không chỉ tên class) không?
 
Upvote 0
Thế mà tui thấy cậu Kiều Mạnh đòi nhảy thẳng đi nhậu luôn đó.
Đô được 2 3 lon kg Mạnh, hôm nào đi nhậu với tui ?
Từ xưởng tui xuống nhà cậu khoảng vài cây, 5 10 phút à
OK đón tiếp nhiệt Tình
Mạnh ko sống vì code ... nhưng đam mê code
Mạnh ko có trí thông Minh nhưng có lòng kiên nhẫn
bất cứ ai trạm tới cái tôi của mạnh ... bất cứ giá nào phải xử nó !
Thế thôi

Mong vui vẻ và hiểu nhau hơn
Thân
 
Upvote 0
A cũng có hướng dùng cái đó nhưng chưa thử làm. Hàm em viết cí lấy được kiểu biến như String, Double (không chỉ tên class) không?

The solution below gets the actual type name of such an object in three steps:
  1. Cast the object to the IDispatch type.
  2. Get the ITypeInfo interface via IDispatch.GetTypeInfo().
  3. Get the type name using ITypeInfo.GetDocumentation().
Mã:
using System;
using System.Runtime.InteropServices;
using ComTypes = System.Runtime.InteropServices.ComTypes;

namespace ComUtils
{
    public class ComHelper
    {
        /// <summary>
        /// Returns a string value representing the type name of the specified COM object.
        /// </summary>
        /// <param name="comObj">A COM object the type name of which to return.</param>
        /// <returns>A string containing the type name.</returns>
        public static string GetTypeName(object comObj)
        {

            if (comObj == null)
                return String.Empty;

            if (!Marshal.IsComObject(comObj))
                //The specified object is not a COM object
                return String.Empty;

            IDispatch dispatch = comObj as IDispatch;
            if (dispatch == null)
                //The specified COM object doesn't support getting type information
                return String.Empty;

            ComTypes.ITypeInfo typeInfo = null;
            try
            {
                try
                {
                    // obtain the ITypeInfo interface from the object
                    dispatch.GetTypeInfo(0, 0, out typeInfo);
                }
                catch (Exception ex)
                {
                    //Cannot get the ITypeInfo interface for the specified COM object
                    return String.Empty;
                }

                string typeName = "";
                string documentation, helpFile;
                int helpContext = -1;

                try
                {
                    //retrieves the documentation string for the specified type description
                    typeInfo.GetDocumentation(-1, out typeName, out documentation,
                        out helpContext, out helpFile);
                }
                catch (Exception ex)
                {
                    // Cannot extract ITypeInfo information
                    return String.Empty;
                }
                return typeName;
            }
            catch (Exception ex)
            {
                // Unexpected error
                return String.Empty;
            }
            finally
            {
                if (typeInfo != null) Marshal.ReleaseComObject(typeInfo);
            }
        }
    }

    /// <summary>
    /// Exposes objects, methods and properties to programming tools and other
    /// applications that support Automation.
    /// </summary>
    [ComImport()]
    [Guid("00020400-0000-0000-C000-000000000046")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IDispatch
    {
        [PreserveSig]
        int GetTypeInfoCount(out int Count);

        [PreserveSig]
        int GetTypeInfo(
            [MarshalAs(UnmanagedType.U4)] int iTInfo,
            [MarshalAs(UnmanagedType.U4)] int lcid,
            out ComTypes.ITypeInfo typeInfo);

        [PreserveSig]
        int GetIDsOfNames(
            ref Guid riid,
            [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr)]
            string[] rgsNames,
            int cNames,
            int lcid,
            [MarshalAs(UnmanagedType.LPArray)] int[] rgDispId);

        [PreserveSig]
        int Invoke(
            int dispIdMember,
            ref Guid riid,
            uint lcid,
            ushort wFlags,
            ref ComTypes.DISPPARAMS pDispParams,
            out object pVarResult,
            ref ComTypes.EXCEPINFO pExcepInfo,
            IntPtr[] pArgErr);
    }
}
Variant có cái type đó anh. String, double, inter,... Range cung co cách, tên class thi chưa anh. Giống như đợt trước em lấy field type đó anh
 
Lần chỉnh sửa cuối:
Upvote 0
Ghét cái ông Sì sáp này quá, dùng API gì cũng phải copy paster "đì cờ le"...
 
Upvote 0
Upvote 0
Hì hì, bé nhầm giữa TypeInfo RTTI của Delphi và TypeInfo của COM/OLE rồi :p
 
Upvote 0
Cho chú xem source Delphi của bé thử ?
 
Upvote 0
Mã:
  OleType := VarType(V) and VarTypeMask;
          //if VarType(V) and VarArray<>0
          //then VArray:='Array of'
          //else VArray:='';
  case OleType of
    varEmpty     : typeString := 'Empty';
    varNull      : typeString := 'Null';
    varSmallInt  : typeString := 'SmallInt';
    varInteger   : typeString := 'Integer';
    varSingle    : typeString := 'Single';
    varDouble    : typeString := 'Double';
    varCurrency  : typeString := 'Currency';
    varDate      : typeString := 'Date';
    varOleStr    : typeString := 'OleStr';
    varDispatch  : typeString := 'Range';//'varDispatch'; 'OLE Object';
    varError     : typeString := 'Error';
    varBoolean   : typeString := 'Boolean';
    varVariant   : typeString := 'Variant';
    varUnknown   : typeString := 'Unknown';
    varByte      : typeString := 'Byte';
    varWord      : typeString := 'Word';
    varLongWord  : typeString := 'LongWord';
    varInt64     : typeString := 'Int64';
    varStrArg    : typeString := 'StrArg';
    varString    : typeString := 'String';
    varAny       : typeString := 'Any';
    varTypeMask  : typeString := 'TypeMask';
    varArray    : typeString :='[Array]';//   (VArray: PVarArray);
  end;
  Result:=typeString;
 
Upvote 0
Hì hì, bé phát minh lại bánh xe rồi, Delphi nó có sẵn hàm VarTypeAsText trong unit Variants.pas rồi, sao không dùng mà đi viết lại.
Trong file bác @kieu manh gởi đây :)
Mã:
function VarTypeAsText(const AType: TVarType): string;
const
  CText: array [varEmpty..varUInt64] of string = ('Empty', 'Null', 'Smallint', //Do not localize
    'Integer', 'Single', 'Double', 'Currency', 'Date', 'OleStr', 'Dispatch', //Do not localize
    'Error', 'Boolean', 'Variant', 'Unknown', 'Decimal', '$0F', 'ShortInt', //Do not localize
    'Byte', 'Word', 'Cardinal', 'Int64', 'UInt64'); //Do not localize
var
  LHandler: TCustomVariantType;
begin
  if AType and varTypeMask <= varUInt64 then
    Result := CText[AType and varTypeMask]
  else if AType = varString then
    Result := 'String' //Do not localize
  else if AType = varUString then
    Result := 'UnicodeString' //Do not localize
  else if AType = varAny then
    Result := 'Any' //Do not localize
  else if FindCustomVariantType(AType, LHandler) then
    Result := Copy(LHandler.ClassName, 2, High(Integer))
  else
    Result := HexDisplayPrefix + IntToHex(AType and varTypeMask, 4);

  if AType and varArray <> 0 then
    Result := 'Array ' + Result; //Do not localize
  if AType and varByRef <> 0 then
    Result := 'ByRef ' + Result; //Do not localize
end;
 
Upvote 0
Hì hì, bé phát minh lại bánh xe rồi, Delphi nó có sẵn hàm VarTypeAsText trong unit Variants.pas rồi, sao không dùng mà đi viết lại.
Trong file bác @kieu manh gởi đây :)
Mã:
function VarTypeAsText(const AType: TVarType): string;
const
  CText: array [varEmpty..varUInt64] of string = ('Empty', 'Null', 'Smallint', //Do not localize
    'Integer', 'Single', 'Double', 'Currency', 'Date', 'OleStr', 'Dispatch', //Do not localize
    'Error', 'Boolean', 'Variant', 'Unknown', 'Decimal', '$0F', 'ShortInt', //Do not localize
    'Byte', 'Word', 'Cardinal', 'Int64', 'UInt64'); //Do not localize
var
  LHandler: TCustomVariantType;
begin
  if AType and varTypeMask <= varUInt64 then
    Result := CText[AType and varTypeMask]
  else if AType = varString then
    Result := 'String' //Do not localize
  else if AType = varUString then
    Result := 'UnicodeString' //Do not localize
  else if AType = varAny then
    Result := 'Any' //Do not localize
  else if FindCustomVariantType(AType, LHandler) then
    Result := Copy(LHandler.ClassName, 2, High(Integer))
  else
    Result := HexDisplayPrefix + IntToHex(AType and varTypeMask, 4);

  if AType and varArray <> 0 then
    Result := 'Array ' + Result; //Do not localize
  if AType and varByRef <> 0 then
    Result := 'ByRef ' + Result; //Do not localize
end;
em biết chứ hihi, tự cái hàm này em còn làm cái khác nữa, cũng có thể em nhìn không ra...
 
Upvote 0
Mã:
  OleType := VarType(V) and VarTypeMask;
          //if VarType(V) and VarArray<>0
          //then VArray:='Array of'
          //else VArray:='';
  case OleType of
    varEmpty     : typeString := 'Empty';
    varNull      : typeString := 'Null';
    varSmallInt  : typeString := 'SmallInt';
    varInteger   : typeString := 'Integer';
    varSingle    : typeString := 'Single';
    varDouble    : typeString := 'Double';
    varCurrency  : typeString := 'Currency';
    varDate      : typeString := 'Date';
    varOleStr    : typeString := 'OleStr';
    varDispatch  : typeString := 'Range';//'varDispatch'; 'OLE Object';
    varError     : typeString := 'Error';
    varBoolean   : typeString := 'Boolean';
    varVariant   : typeString := 'Variant';
    varUnknown   : typeString := 'Unknown';
    varByte      : typeString := 'Byte';
    varWord      : typeString := 'Word';
    varLongWord  : typeString := 'LongWord';
    varInt64     : typeString := 'Int64';
    varStrArg    : typeString := 'StrArg';
    varString    : typeString := 'String';
    varAny       : typeString := 'Any';
    varTypeMask  : typeString := 'TypeMask';
    varArray    : typeString :='[Array]';//   (VArray: PVarArray);
  end;
  Result:=typeString;

Cái này a biết. Em đã thử hàm của em
myTypeName(range(“a1”)
kết quả đúng trả về “Range” chưa?
 
Upvote 0
Bé viết theo code C# hả ? Hì hì, chưa chính xác đâu :)
Code của MS coder cho hàm rtcTypeName, cho VT_DISPATCH đây, bonus mọi người viết theo đúng vậy trên ngôn ngữ của mình.
Chú mới "rờ em" nó ra full, đầy đủ rồi đó.
Theo bé thì nên viết theo code C# hay nên viết theo code C/C++ này ? :p
Tự like cho mình 1 cái, hổng ai like hết ! Cho cái vỗ tay đê !

Untitled.png
 
Lần chỉnh sửa cuối:
Upvote 0
Bé viết theo code C# hả ? Hì hì, chưa chính xác đâu :)
Code của MS coder cho hàm rtcTypeName, cho VT_DISPATCH đây, bonus mọi người viết theo đúng vậy trên ngôn ngữ của mình.
Chú mới "rờ em" nó ra full, đầy đủ rồi đó.
Theo bé thì nên viết theo code C# hay nên viết theo code C/C++ này ? :p
Tự like cho mình 1 cái, hổng ai like hết ! Cho cái vỗ tay đê !

View attachment 223222
:clap::clap::clap::clap::clap::clap::clap::clap::clap::clap:
Code C đâu phải của em của Addmin trên diễn đàn nước ngoài, anh chuyển giùm qua Delphi cho em đi /-*+//-*+//-*+//-*+//-*+/
 
Upvote 0
Chú không biết code Delphi bé, và cũng không có cài Delphi.
 
Upvote 0
Cũng thua luôn bé. Hỏi bác Tuân đi. C++ chú cũng không biết.
 
Upvote 0
Web KT
Back
Top Bottom